Android Camera2 API - 在我们有焦点时检测

时间:2015-04-29 15:18:09

标签: android android-camera camera2

所以,我设法用我想要的方式用旧相机创建我想要的功能。

使用mCamera.autoFocus(autoFocusCallback);我检测到焦点并在预览模式下运行所需的代码。

现在我很难掌握如何在camera2 API中做同样的事情。 我的第一个想法是我使用

        private void process(CaptureResult result) {
        switch (mState) {
            case STATE_PREVIEW: {
                // We have nothing to do when the camera preview is working normally.
                int afState = result.get(CaptureResult.CONTROL_AF_STATE);
                //if (CaptureResult.CONTROL_AF_STATE == afState) {
                    Log.d("SOME KIND OF FOCUS", "WE HAVE");
                //}

                break;
            }
}

但是我没有找到某种状态告诉我我们已经集中注意力了。有人知道如何使用Camera2 API完成这项工作吗?

2 个答案:

答案 0 :(得分:5)

对于那些感兴趣的人,我最后得到了这个:

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

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

                int afState = result.get(CaptureResult.CONTROL_AF_STATE);
                if (CaptureResult.CONTROL_AF_TRIGGER_START == afState) {
                    if (areWeFocused) {
                        //Run specific task here
                    }
                }
                if (CaptureResult.CONTROL_AF_STATE_PASSIVE_FOCUSED == afState) {
                    areWeFocused = true;
                } else {
                    areWeFocused = false;
                }

                break;
            }
        }
    }

   @Override
    public void onCaptureProgressed(CameraCaptureSession session, CaptureRequest request,
                                    CaptureResult partialResult) {
        process(partialResult);
    }

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

它足够好:))

答案 1 :(得分:3)

你基本上得到了它。您可以查看可以检查的状态列表及其转换here

这取决于您使用的CONTROL_AF_MODE,但通常会检查FOCUSED_LOCKEDPASSIVE_FOCUSED,但您可能希望查看NOT_FOCUSED_LOCKED和{{{ 1}}以防相机无法对焦于场景。