Lipu Hossain
Lipu Hossain

Reputation: 79

Preview locked while Capturing Image camera2

i have implemented burst image capture with camera2 Api,its working fine taking 6 fps..bt my problem is when its taking picture it trigers focus lock thats why preview is locked for a small ammount of time,i want to remove that preview lock,i want the preview always enable,here is my still capture burst,i am following googles camera2 example

private void captuteStillImage() {
    try {
        count = 0;
        CaptureRequest.Builder captureBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);

        int rotation = getWindowManager().getDefaultDisplay().getRotation();
        captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, ORIENTATIONS.get(rotation));

        CameraCaptureSession.CaptureCallback captureCallback = new CameraCaptureSession.CaptureCallback() {
            @Override
            public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
                super.onCaptureCompleted(session, request, result);
                //unlockFocus();
                count++;
                Log.e("count",count+"");
                runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tv_count.setText(count+"");

                }
            });

                if (count >= MAX_CAPTURE) {
                   unlockFocus();
                }
                Log.e("Image Capture", "Successfully");
            }
        };

        // mCameraCaptureSession.capture(captureBuilder.build(), captureCallback, null);

        List<CaptureRequest> captureList = new ArrayList<CaptureRequest>();
        captureBuilder.addTarget(mImageReader.getSurface());
        for (int i = 0; i < MAX_CAPTURE; i++) {
            captureList.add(captureBuilder.build());
        }
        //mCameraCaptureSession.stopRepeating();
        mCameraCaptureSession.captureBurst(captureList, captureCallback, null);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

Upvotes: 0

Views: 1452

Answers (1)

Mick
Mick

Reputation: 25511

I experimented a little with this on camera2basic (https://github.com/googlesamples/android-Camera2Basic) and found I could get the preview back much quicker if I called unlock just after acquiring the image and before saving it - I also removed the original call to unLockFocus in the captureCallback.

    /**
     * This a callback object for the {@link ImageReader}. "onImageAvailable" will be called when a
     * still image is ready to be saved.
     */
    private final ImageReader.OnImageAvailableListener mOnImageAvailableListener
            = new ImageReader.OnImageAvailableListener() {

        @Override
        public void onImageAvailable(ImageReader reader) {
            Log.d(TAG,"onImageAvailable");

            //Get the image
            Image cameraImage = reader.acquireNextImage();

            //Now unlock the focus so the UI does not look locked - note that this is a much earlier point than in the
            //original Camera2Basic example from google as the original place was causing the preview to lock during any
            //image manipulation and saving.
            unlockFocus();

            //Save the image file in the background - note check you have permissions granted by user or this will cause an exception.
            mBackgroundHandler.post(new ImageSaver(getActivity().getApplicationContext(), cameraImage, outputPicFile);

        }

    };

However, Camera2Basic has many callbacks and I found that when you start testing with scenarios where the activity or fragment is paused and resumed, and especially if your app has other asynchronous callbacks also, it is very easy to get into race conditions that can cause unexpected behaviour or crashes.

If you just want a simple example of a camera that returns the preview quicker when taking a photo, then the basic FotoApparat example might be worth looking at also:

Upvotes: 1

Related Questions