• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

using Camera2APi for continously Capturing photo, Could not retrieve native Surface from surface.

Abhilash K

Lurker
Oct 6, 2017
1
0
I am using Android Camera2 API and would like to take 15 pictures continuously. The problem is that it successfully takes a few pictures but crashes after 6~8 pictures.

Here are the logs:

Legacy-CameraDevice-JNI: LegacyCameraDevice_nativeGetSurfaceId: Could not retrieve native Surface from surface.
E/AndroidRuntime: FATAL EXCEPTION: Thread-1095 Process: com.example.grobomac.traindriver, PID: 11690 java.lang.IllegalArgumentException: Surface had no valid native Surface.
at android.hardware.camera2.legacy.LegacyCameraDevice.nativeGetSurfaceId(Native Method) at android.hardware.camera2.legacy.LegacyCameraDevice.getSurfaceId(LegacyCameraDevice.java:658) at android.hardware.camera2.legacy.LegacyCameraDevice.containsSurfaceId(LegacyCameraDevice.java:678) at android.hardware.camera2.legacy.RequestThreadManager$2.onPictureTaken(RequestThreadManager.java:225) at android.hardware.Camera$EventHandler.handleMessage(Camera.java:1272) at android.os.Handler.dispatchMessage(Handler.java:111) at android.os.Looper.loop(Looper.java:207) at android.hardware.camera2.legacy.CameraDeviceUserShim$CameraLooper.run(CameraDeviceUserShim.java:136) at java.lang.Thread.run(Thread.java:818) 10-04 14:26:59.566

And the code:

public void onImageAvailable(ImageReader reader) {
Image image = null;
try {
image = reader.acquireLatestImage();
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.capacity()];
buffer.get(bytes);
save(bytes);
mBitmapToSave1 = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
mBitmapToSave = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
Bitmap scaled = Bitmap.createScaledBitmap(mBitmapToSave, width, height, true);
int w = scaled.getWidth();
int h = scaled.getHeight();
// Setting post rotate to 90
Matrix mtx = new Matrix();
mtx.postRotate(-180);
// Rotating Bitmap
mBitmapToSave = Bitmap.createBitmap(scaled, 0, 0, w, h, mtx, true);
// mBitmapToSave = Bitmap.createBitmap(width+rowPadding/pixelStride,height, Bitmap.Config.RGB_565);
// mBitmapToSave.copyPixelsToBuffer(buffer);

if (detector.isOperational() && mBitmapToSave != null) {
Frame frame = new Frame.Builder().setBitmap(mBitmapToSave).build();
SparseArray<Face> faces = detector.detect(frame);

for (index = 0; index < faces.size(); ++index) {
Face face = faces.valueAt(index);
}
if (faces.size() == 0) {
MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.not);
mediaPlayer.start();
//Toast.makeText(AndroidCamera2API.this, "Face Not detected Adjust Camera Properly", Toast.LENGTH_SHORT).show();
} else {
c++;
Toast.makeText(AndroidCamera2API.this, "Face Found " + "\n", Toast.LENGTH_SHORT).show();
//Toast.makeText(AndroidCamera2API.this, "Saved:" + file, Toast.LENGTH_SHORT).show();
setFileToUpload();
//file.delete();
// if(file.exists()){
//file.getCanonicalFile().delete();
// if(file.exists()){
// getApplicationContext().deleteFile(file.getName());
// }
// }
a++;

Toast.makeText(AndroidCamera2API.this, "" +c, Toast.LENGTH_SHORT).show();


// Toast.makeText(AndroidCamera2API.this, "completed" , Toast.LENGTH_SHORT).show();


}
}
}catch(FileNotFoundException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
} finally{
if (image != null) {
image.close();
}
}
}


i will take picture in OnResume:

protected void onResume() {
final Intent intent = new Intent(AndroidCamera2API.this, Completed.class);
super.onResume();
Log.e(TAG, "onResume");

startBackgroundThread();
if (textureView.isAvailable()) {
openCamera();
} else {
textureView.setSurfaceTextureListener(textureListener);
}

final int PICTURES_LIMIT = 15;

final Timer timer = new Timer();

timer.schedule(new TimerTask() {
int pictureNo=0;
public void run() {
if (pictureNo>PICTURES_LIMIT) {
timer.cancel();
startActivity(intent);


}else {
pictureNo++;
takePicture();


}
}


}, 10, 7500);


}
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones