Front Camera in Camera2 not capturing image

前端 未结 5 910
一向
一向 2021-02-04 12:18

I have the code for the Camera2 from https://github.com/googlesamples/android-Camera2Basic.

The problem i\'m facing is that when i turn to my Front Camera, i cannot capt

5条回答
  •  臣服心动
    2021-02-04 12:27

    The problem is that many front-facing cameras have a fixed focus distance. So after the autofocus trigger in lockFocus() the autofocus state (CONTROL_AF_STATE) remains INACTIVE and the autofocus trigger does nothing.

    So in order to make it work you need to check whether autofocus is supported or not. To do so, add the following to setUpCameraOutputs():

    int[] afAvailableModes = characteristics.get(CameraCharacteristics.CONTROL_AF_AVAILABLE_MODES);
    
    if (afAvailableModes.length == 0 || (afAvailableModes.length == 1
            && afAvailableModes[0] == CameraMetadata.CONTROL_AF_MODE_OFF)) {
        mAutoFocusSupported = false;
    } else {
        mAutoFocusSupported = true;
    }
    

    Finally don't lock focus if its not supported when you want to take a picture:

    private void takePicture() {
        if (mAutoFocusSupported) {
            lockFocus();
        } else {
            captureStillPicture();
        }
    }
    

提交回复
热议问题