Android Camera2 API - Detect when we have focus

左心房为你撑大大i 提交于 2019-12-04 09:16:21

You've basically got it. The list of states you can check for and their transitions can be found here.

It depends on what CONTROL_AF_MODE you are using, but generally you check for FOCUSED_LOCKED or perhaps PASSIVE_FOCUSED, though you may want to have cases for NOT_FOCUSED_LOCKED and PASSIVE_UNFOCUSED in case the camera just cannot focus on the scene.

For those interested I ended up with a mix of this:

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

It works good enough :)

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