Issues when capturing Multiple Photos: app stops responding, camera preview turns to green and no picture is saved

后端 未结 3 1693
谎友^
谎友^ 2020-12-10 08:39

I have successfully captured the single photo using camera in Android. But when I tried to capture 5 photos at once, app stops responding, camera preview turns to green and

相关标签:
3条回答
  • 2020-12-10 09:00

    I didn't find any API support for this. And repeatedly calling the takePicture method from the loop is not working. So, I get a workaround. The code is still in loop but I'm now using Thread;

    private class CaptureThread extends Thread {
    
            @Override
            public void run() {
    
                int count = 0;
    
                while(count < mNo) {
                    mFileName = mLocation + "/pic" + count + ".jpg";
    
                    mCamera.takePicture(null, mPictureCallback, mPictureCallback);
    
                    count++;
    
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException exception) {
                        exception.printStackTrace();
                    }
                }
            }
    }
    
    0 讨论(0)
  • 2020-12-10 09:12

    I'm not sure the android api even supports a burst mode. One thing's for sure, you can't just call takePicture() in a loop like that. That is just abusing those poor api's.

    How about taking the next picture in onPictureTaken() ? (Obviously you have to keep a track of the number of pictures taken etc ..)

    Also like the documentation says, don't assume it will work on every device.

    I wrote the above answer in 2011, since then Camera has evolved

    EDIT : Burst mode is now supported in Camera2 : https://developer.android.com/reference/android/hardware/camera2/package-summary.html

    See https://developer.android.com/reference/android/hardware/camera2/CameraCaptureSession.html

    0 讨论(0)
  • 2020-12-10 09:24

    You can take next photo only after callback method Camera.PictureCallback.onPictureTaken is called and camera preview is restarted.

    private void takePhoto() {
        if (!(cameraPreview.isRunning())) {
            Log.i(LOG_TAG, "Camera is not ready. Skip frame");
            return;
        }
    
        camera.takePicture(null, null, this);
        cameraPreview.onPictureTook();
    }
    
    public void onPictureTaken(byte[] data, Camera camera) {
        // save photo
        cameraPreview.start();
    }
    
    
    public class CameraPreview extends SurfaceView {
    
        private boolean previewRunning = false;
    
        /**
         * Should be called after calling camera.takePicture
         */
        public void onPictureTook() {
            previewRunning = false;
        }
    
        public boolean isRunning() {
            return previewRunning;
        }
    
        public void start() {
            try {
                camera.setPreviewDisplay(surfaceHolder);
                camera.startPreview();
                previewRunning = true;
            } catch (Exception ignored) {
                Log.e(LOG_TAG, "Error starting camera preview", ignored);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题