How to manually control Camera2 manual focus?

吃可爱长大的小学妹 提交于 2019-12-02 15:52:53

问题


Problem

My phone support all manual configuration. Tested with (https://github.com/pinguo-yuyidong/Camera2) which allow us to adjust using seekbar.

I want to set a fix value in Lens Focus Distance when preview is started until I close the app.

After I set AF mode to off and set the Lens Focus Distance with a value. The preview does not show any effect but the Focus 2 value is changed.

How can I overcome this problem?

Sample Code (Java)

    private CaptureRequest mPreviewRequest;
    private void startCameraPreview() throws CameraAccessException {
        SurfaceTexture texture = textureview.getSurfaceTexture();
        texture.setDefaultBufferSize(imageDimensions.getWidth(), imageDimensions.getHeight());
        Surface surface = new Surface(texture);
        mPreviewRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        mPreviewRequestBuilder.addTarget(surface);
        System.out.println("Focus 1 ====== "+mPreviewRequestBuilder.get(CaptureRequest.LENS_FOCUS_DISTANCE));
        cameraDevice.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback() {
            @Override
            public void onConfigured(@NonNull CameraCaptureSession session) {
                if (cameraDevice == null) {
                    return;
                }
                cameraSession = session;

                try {
                    mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,CaptureRequest.CONTROL_AF_MODE_OFF);
                    mPreviewRequestBuilder.set(CaptureRequest.LENS_FOCUS_DISTANCE, 7.5f);
                    mPreviewRequest = mPreviewRequestBuilder.build();
                    cameraSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback, backgroundHandler);
                    System.out.println("Focus 2 ====== "+mPreviewRequestBuilder.get(CaptureRequest.LENS_FOCUS_DISTANCE));
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onConfigureFailed(@NonNull CameraCaptureSession session) {
                Toast.makeText(getApplicationContext(),"Failed",Toast.LENGTH_SHORT).show();
            }
        }, null);
    }

    private static final int STATE_PREVIEW = 0;
    private int mState = STATE_PREVIEW;
    private CameraCaptureSession.CaptureCallback mCaptureCallback = new CameraCaptureSession.CaptureCallback() {
        private void process(CaptureResult result){
            switch(mState){
                case STATE_PREVIEW:{
                    break;
                }
            }
        }
    };

Output

Focus 1 ====== 0.0

Focus 2 ====== 7.5

I can't see the difference when I change the value.


回答1:


Found the answer

Not sure why I need to mPreviewRequest and setRepeatingRequest 2 times but it works.

mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,CaptureRequest.CONTROL_AF_MODE_OFF);
mPreviewRequest = mPreviewRequestBuilder.build();
cameraSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback, backgroundHandler);
mPreviewRequestBuilder.set(CaptureRequest.LENS_FOCUS_DISTANCE, 7.5f);
mPreviewRequest = mPreviewRequestBuilder.build();
cameraSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback, backgroundHandler);



来源:https://stackoverflow.com/questions/56421471/how-to-manually-control-camera2-manual-focus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!