如何手动控制Camera2的手动对焦?

时间:2019-06-03 05:13:22

标签: java android android-camera2

问题

我的手机支持所有手动配置。经过(https://github.com/pinguo-yuyidong/Camera2)的测试,使我们可以使用搜索栏进行调整。

我想在开始预览之前在“镜头焦距”中设置一个固定值,直到关闭应用程序。

将自动对焦模式设置为关闭后,将镜头焦距设置为一个值。预览没有显示任何效果,但是Focus 2值已更改。

我该如何克服这个问题?

示例代码(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;
                }
            }
        }
    };

输出

焦点1 ====== 0.0

焦点2 ====== 7.5

更改值时看不到差异。

1 个答案:

答案 0 :(得分:0)

找到答案

不确定为什么我需要两次mPreviewRequest和setRepeatingRequest,但是它可以工作。

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