Android Camera2焦点状态卡住了

ket*_*tom 8 android android-camera2

我需要在我的应用程序上使用Camera2 API.(Api21 +)我找到了下一个样本:https: //github.com/googlesamples/android-Camera2Basic

我下载了它并开始使用我的手机.当我按下"图片"按钮时,它会调用takePhoto方法.

private void takePicture() {
    lockFocus();
}
Run Code Online (Sandbox Code Playgroud)

这是一台国家机器.有时这台机器卡住了STATE_WAITING_LOCK.我的设备正在等待Focus,但没有任何反应!(是的,我的设备支持自动对焦)

case STATE_WAITING_LOCK: {
        Integer afState = result.get(CaptureResult.CONTROL_AF_STATE);
        if (afState == null) {
            captureStillPicture();
        } else if (CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED == afState ||
                CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED == afState) {
            // CONTROL_AE_STATE can be null on some devices
            Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
            if (aeState == null ||
                    aeState == CaptureResult.CONTROL_AE_STATE_CONVERGED) {
                mState = STATE_PICTURE_TAKEN;
                captureStillPicture();
            } else {
                runPrecaptureSequence();
            }
        }
        break;
    }
Run Code Online (Sandbox Code Playgroud)

这个问题有什么好的解决方案?这个程序有时会在这里崩溃:

private void unlockFocus() {
    try {
        // Reset the auto-focus trigger
        mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
                CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
        setAutoFlash(mPreviewRequestBuilder);
/*HERE*/ mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback,
                mBackgroundHandler);
        // After this, the camera will go back to the normal state of preview.
        mState = STATE_PREVIEW;
        mCaptureSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback,
                mBackgroundHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么无法关注我的设备?

小智 2

看来您的程序有时会卡在 CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED 中,根据文档“AF 算法无法对焦。镜头没有移动。” 关联

尝试取消 AF_TRIGGER 并重新启动。像这样的东西:

 if (afState == CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED) {
          getRidOfNotFocusedLock();
}
Run Code Online (Sandbox Code Playgroud)

和:

private void getRidOfNotFocusedLock(){
        try {
            mPreviewRequestBuilder.set(
                    CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_CANCEL);
            mCaptureSession.capture(
                    captureRequestBuilder.build(), captureSessionCaptureCallback, backgroundHandler);
            mPreviewRequestBuilder.set(
                    CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_START);

        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }
Run Code Online (Sandbox Code Playgroud)