简体   繁体   English

捕获图像camera2时预览锁定

[英]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 我已经用camera2 Api实现了连拍图像捕获,以6 fps的速率可以正常工作。我希望预览始终启用,这是我仍在捕捉的连拍,我正在关注googles camera2示例

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();
    }
}

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. 我在camera2basic( https://github.com/googlesamples/android-Camera2Basic )上对此进行了一些试验,发现如果我在获取图像后并保存之前调用了解锁,则可以更快地恢复预览。在captureCallback中对unLockFocus的原始调用。

    /**
     * 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. 但是,Camera2Basic有很多回调,我发现当您开始测试其中活动或片段已暂停并恢复的场景时,尤其是如果您的应用程序还具有其他异步回调时,很容易陷入可能导致意外情况的竞争状态行为或崩溃。

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: 如果您只需要一个简单的相机示例,它可以在拍摄照片时更快地返回预览,那么基本的FotoApparat示例可能也值得一看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM