簡體   English   中英

捕獲圖像camera2時預覽鎖定

[英]Preview locked while Capturing Image camera2

我已經用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();
    }
}

我在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);

        }

    };

但是,Camera2Basic有很多回調,我發現當您開始測試其中活動或片段已暫停並恢復的場景時,尤其是如果您的應用程序還具有其他異步回調時,很容易陷入可能導致意外情況的競爭狀態行為或崩潰。

如果您只需要一個簡單的相機示例,它可以在拍攝照片時更快地返回預覽,那么基本的FotoApparat示例可能也值得一看:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM