Android Camera2 focus state stuck

I need to use Camera2 API in my application. (Api21 +) I found the following sample: https://github.com/googlesamples/android-Camera2Basic.

I downloaded it and started on my phone. When I clicked the "Image" button, he called the takePhoto method.

private void takePicture() {
    lockFocus();
}

This is a state machine. Sometimes this car hangs on STATE_WAITING_LOCK. My device is waiting for Focus, but nothing happens! (Yes, my device supports autofocus)

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

What is a good solution to this problem? And this program sometimes crashes here:

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

Why can't I focus my device?

+5
source share
2 answers

, CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED, " . ".

AF_TRIGGER . - :

 if (afState == CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED) {
          getRidOfNotFocusedLock();
}

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();
        }
    }
+1

, , .
CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED, AF_TRIGGER:

if (afState == CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED) {
    restartFocus();
}

private void restartFocus(){
        try {
            mPreviewRequestBuilder.set(
                CaptureRequest.CONTROL_AF_TRIGGER,
                CaptureRequest.CONTROL_AF_TRIGGER_CANCEL);
            mCaptureSession.capture(
                captureRequestBuilder.build(),
                captureSessionCaptureCallback,
                mBackgroundHandler);
            lockFocus();

        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }

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

, , , , . .

0

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


All Articles