1

我正在构建一个基于 Camera2 的相机应用程序,但我保存的图片与我在表面视图中看到的最新图片不匹配。预览会话似乎有效,但是当我要求捕获时,新请求会停止预览并捕获图像。表面视图冻结在最新图片上,并且在我按下快门按钮(预览运行和请求捕获)与捕获请求的 onCaptureCompleted 之间产生了间隙。

这是预览会话

private void createCameraPreviewSession() {

    try {

        SurfaceTexture texture = mTextureView.getSurfaceTexture();
        assert texture != null;

        texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());

        Log.d(TAG, "here is the width of texture" + mPreviewSize.getWidth());
        Log.d(TAG, "here is the height of texture" +mPreviewSize.getHeight());

        Surface surface = new Surface(texture);

        mPreviewRequestBuilder
                = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        mPreviewRequestBuilder.addTarget(surface);

        mCameraDevice.createCaptureSession(Arrays.asList(surface, mImageReader.getSurface()),
                new CameraCaptureSession.StateCallback() {

                    @Override
                    public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
                        if (null == mCameraDevice) {
                            return;
                        }

                        mCaptureSession = cameraCaptureSession;
                        try {
                            mPreviewRequest = mPreviewRequestBuilder.build();
                            mCaptureSession.setRepeatingRequest(mPreviewRequest,
                                    mCaptureCallback, mBackgroundHandler);
                        } catch (CameraAccessException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onConfigureFailed(
                            @NonNull CameraCaptureSession cameraCaptureSession) {
                        showToast("Failed");
                    }
                }, null
        );
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

mCaptureCallback 定义如下:

私人CameraCaptureSession.CaptureCallback mCaptureCallback =新CameraCaptureSession.CaptureCallback(){

    private void process(CaptureResult result) {
        switch (mState) {
            case STATE_PREVIEW:
                break;

            case STATE_CAPTURE:
                mState = STATE_PREVIEW;
                capturePicture();
                break;
        }
    }

    @Override
    public void onCaptureProgressed(@NonNull CameraCaptureSession session,
                                    @NonNull CaptureRequest request,
                                    @NonNull CaptureResult partialResult) {

        process(partialResult);
    }


    @Override
    public void onCaptureCompleted(@NonNull CameraCaptureSession session,
                                   @NonNull CaptureRequest request,
                                   @NonNull TotalCaptureResult result) {
        TotalCaptureResult iResult = result;
        Log.d(TAG, "Frame on Completed: "+result.getFrameNumber());
        process(result);
    }
}

发生的事情是我重复预览并且它有效。该过程仅用于保持其运行,并且在将 mState 设置为 CAPTURE 之前什么都没有发生。

当我们点击快门按钮时,它被设置为捕捉。当我单击按钮时,我调用:

private void takePicture(){
        try {
            mFile = ImageSaver.generateNewFileImage();
            mState = STATE_CAPTURE;
            mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback,
                    mBackgroundHandler);
        } catch (CameraAccessException e) {
            Log.e(TAG,"Camera exception",e);
        }
    }

然后我调用 CapturePicture,因为 mState 在 Capture 中,如 mCaptureCallback 中所定义

private void capturePicture() {
    mTakePictureRunnable = new Runnable() {
        @Override
        public void run() {
            takePictureNow();
        }
    };
    mBackgroundHandler.post(mTakePictureRunnable);
}

定义了 takePicutreNow

private void takePictureNow() {

    Log.d(TAG, "Running captureStillPicture");

    try {
        if (null == mCameraDevice) {
            return;
        }
        // This is the CaptureRequest.Builder that we use to take a picture.
        final CaptureRequest.Builder captureBuilder =
                mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);

        captureBuilder.addTarget(mImageReader.getSurface());

        SurfaceTexture texture = mTextureView.getSurfaceTexture();
        assert texture != null;

        texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());

        Log.d(TAG, "here is the width of texture" + mPreviewSize.getWidth());
        Log.d(TAG, "here is the height of texture" + mPreviewSize.getHeight());

        Surface surface = new Surface(texture);
        captureBuilder.addTarget(surface);


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

        //Location if needed
        boolean Location_Saved = CameraSettings.Instance().getBoolean(CameraSettings.SAVE_LOCATION,
                getResources().getBoolean(R.bool.action_camera_settings_dflt_location));

        if(Location_Saved == true) {
            captureBuilder.set(CaptureRequest.JPEG_GPS_LOCATION, mLocationManager.getCurrentLocation());
        } else {
            captureBuilder.set(CaptureRequest.JPEG_GPS_LOCATION, null);
        }


        CameraCaptureSession.CaptureCallback CaptureCallback
                = new CameraCaptureSession.CaptureCallback() {

            @Override
            public void onCaptureStarted(@NonNull CameraCaptureSession session,
                                         @NonNull CaptureRequest request,
                                         @NonNull long timestamp,
                                         @NonNull long framenumber) {
                playShutterSound();
                showShutterAnimation();

            }

            @Override
            public void onCaptureCompleted(@NonNull CameraCaptureSession session,
                                           @NonNull CaptureRequest request,
                                           @NonNull TotalCaptureResult result) {
                Log.d(TAG, mFile.toString());
                mState = STATE_PREVIEW;
            }
        };

        mCaptureSession.capture(captureBuilder.build(), CaptureCallback, null);
        mCaptureSession.stopRepeating();

    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

这个过程是有效的,但我在印刷机和图像保存之间有一个滞后,如果我快速移动,我在预览屏幕上看到的最新图片与保存的图片不完全相同。

似乎捕获不更新表面,表面仅在我们处于预览时显示

知道我保存了什么吗?谢谢

4

1 回答 1

0

拍摄图像时表面会冻结,这可能不是保存的图像。如果您仍然想在屏幕上显示相机预览,您只需通过调用您第一次创建预览时使用的函数来重新启动相机预览。

于 2019-07-25T09:52:20.830 回答