【问题标题】:Preview locked while Capturing Image camera2捕获图像时锁定预览 camera2
【发布时间】:2017-10-12 05:20:38
【问题描述】:

我已经使用 camera2 Api 实现了连拍图像捕捉,它的工作正常,速度为 6 fps..bt 我的问题是当它拍照时它会触发焦点锁定,这就是为什么预览会锁定一小段时间,我想删除它预览锁定,我希望预览始终启用,这是我的静态捕获突发,我正在关注谷歌的 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();
    }
}

【问题讨论】:

    标签: java android camera android-camera2


    【解决方案1】:

    我在 camera2basic (https://github.com/googlesamples/android-Camera2Basic) 上对此进行了一些实验,发现如果我在获取图像之后和保存之前调用解锁,我可以更快地恢复预览 - 我还删除了对 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);
    
            }
    
        };
    

    但是,Camera2Basic 有很多回调,我发现当您开始测试 Activity 或 Fragment 暂停和恢复的场景时,特别是如果您的应用也有其他异步回调,很容易陷入竞争条件可能会导致意外行为或崩溃。

    如果您只是想要一个简单的相机示例,它可以在拍照时更快地返回预览,那么基本的 FotoApparat 示例可能也值得一看:

    【讨论】:

    • 我无法保存从阅读器获取的图像文件,因为它将在捕获新图像时关闭..我正在捕获 6 个图像突发,所以,我有。试过了,我试图将图像保存在一个数组中,然后当捕获完成然后尝试一个一个保存它时崩溃,因为图像已经关闭@Mick
    • @mick 在 Camera2Basic 中的 unlockFocus 在 captureStillPicture 中的 captureCallback 中被调用。你是说 imageAvailableListener 会在 captureCallback 之前被调用吗?
    • @Siva - 自从我看到它已经有一段时间了,它在不断发展,但我的经验是,这个项目中的许多任务都是异步完成的,因此可能会出现竞争条件和事情发生的顺序与您预期的不同是很高的。
    猜你喜欢
    • 2017-06-18
    • 1970-01-01
    • 2020-04-06
    • 1970-01-01
    • 1970-01-01
    • 2017-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多