androidcameraautofocusfront-cameracamera-api

Android Camera API 2 Auto Focus on Front Camera


I am working with Camera API 2 in Android and trying to apply the auto focus feature. The auto focus is working fine with back camera but not the front camera.

(I am using Android 5.1 and 6.0 devices)

Detail:

This is the trigger to start auto focus:

mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START);

and this is the CameraCaptureSession.CaptureCallback

 Integer afState = result.get(CaptureResult.CONTROL_AF_STATE);

The value of afState is always 0 (INACTIVE) if I use front camera. But with the back camera, that value is changed from 2 (CONTROL_AF_STATE_PASSIVE_FOCUSED - trying to focus) to 4 (CONTROL_AF_STATE_FOCUSED_LOCKED when focus successful) or 5 (CONTROL_AF_STATE_NOT_FOCUSED_LOCKED when focus unsuccessful).

Do you guys have any idea about why I can not use auto focus on front camera?


Solution

  • I believe your front camera does not have autofocus. To check this you can execute

    int[] afAvailableModes = cameraCharacteristics.get(CameraCharacteristics.CONTROL_AF_AVAILABLE_MODES);
    

    If the only available AF mode is CameraMetadata.CONTROL_AF_MODE_OFF, then it means your camera has fixed focus lens. And according to documentation when android.control.afMode is AF_MODE_OFF or AF_MODE_EDOF the state will always remain INACTIVE.