Android Camera2 API - detection when we have focus

So, I managed to create the functionality that I wanted with the old camera, as I wanted.

With mCamera.autoFocus (autoFocusCallback); I detect when I have focus and run the required code in preview mode.

Now it’s hard for me to figure out how to do the same in Camera2 API. My first idea was that I would use

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

but I can’t find any condition that tells me that we got the focus. Does anyone know how to do this using the Camera2 API?

+3
source share
2 answers

. , , .

, CONTROL_AF_MODE, FOCUSED_LOCKED , , PASSIVE_FOCUSED, NOT_FOCUSED_LOCKED PASSIVE_UNFOCUSED, .

+2

, , :

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

:)

+3

Source: https://habr.com/ru/post/1658502/


All Articles