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

后端 未结 3 1705
谎友^
谎友^ 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: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);
            }
        }
    }
    

提交回复
热议问题