为什么相机焦距没有锁定?

时间:2017-07-17 20:02:52

标签: android android-camera2

我正在使用Camera2 API。当用户按下按钮时,它会拍照:

    mStillImageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(!(mIsTimelapse || mIsRecording)) {
                checkWriteStoragePermission();
            }
            lockFocus();
        }
    });

我尝试触发自动对焦:

private void  lockFocus() {
    mCaptureState = STATE_WAIT_LOCK;
    mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_START);
    try {
        mPreviewCaptureSession.capture(mCaptureRequestBuilder.build(), mPreviewCaptureCallback, mBackgroundHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

但是当我的捕获会话回调被调用时:

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

            private void process(CaptureResult captureResult) {
                switch (mCaptureState) {
                    case STATE_PREVIEW:
                        // Do nothing
                        break;
                    case STATE_WAIT_LOCK:
                        mCaptureState = STATE_PREVIEW;
                        Integer afState = captureResult.get(CaptureResult.CONTROL_AF_STATE);
                        if(afState == CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED ||
                                afState == CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED) {
                            Toast.makeText(getApplicationContext(), "AF Locked!", Toast.LENGTH_SHORT).show();
                            startStillCaptureRequest();
                        }
                        break;
                }
            }

            @Override
            public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
                super.onCaptureCompleted(session, request, result);

                process(result);
            }
        }; 

CaptureResult.CONTROL_AF_STATE保持返回1而不是4. 1指的是CONTROL_AF_MODE_AUTO(基本自动对焦模式),4表示CONTROL_AF_STATE_FOCUSED_LOCKED(AF认为它已正确聚焦且锁定焦点)。 90%的时间它返回1,只有10%的时间返回4.为什么会发生这种情况?

0 个答案:

没有答案