Front Camera in Camera2 not capturing image

前端 未结 5 914
一向
一向 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();
        }
    }
    
    0 讨论(0)
  • 2021-02-04 12:28

    @ArvindSingh's solution is not the best one, because you would also disable the focus functionality for the back camera. A better solution would be to do a camera facing check inside the takePicture like that:

    private void takePicture() {
        if (CameraCharacteristics.LENS_FACING_FRONT == mSelectedFacing) {
            // front camera selected, so take a picture without focus
            captureStillPicture();
        } else {
            // back camera selected, trigger the focus before creating an image
            lockFocus();
        }
    }
    

    For this solution you only need to save the current used facing somewhere like here in mSelectedFacing

    0 讨论(0)
  • 2021-02-04 12:43

    Plz try this code for capture

    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 ||
                             CaptureResult.CONTROL_AF_STATE_INACTIVE == afState /*add this*/) {
                        // 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;
                }
    
    0 讨论(0)
  • 2021-02-04 12:48

    For Camera2 Api, Google's example for video works great for both front and back camera, but for Image capturing, Google's example works only for back camera and not for front camera.
    Solution which works for me is

    In lockFocus() method, replace line

    mCaptureSession.capture(mPreviewRequestBuilder.build(),
                                                      mCaptureCallback, mBackgroundHandler);
    

    with

    captureStillPicture();
    

    Hope this will help!!

    0 讨论(0)
  • 2021-02-04 12:48

    In lockFocus() method, replace line

    mCaptureSession.capture(mPreviewRequestBuilder.build(),
                                                  mCaptureCallback,    mBackgroundHandler);
    

    with

    captureStillPicture();
    

    And to prevent Unwanted orientation you can use

    /** * Conversion from screen rotation to JPEG orientation. */

       static {
           ORIENTATIONS.append(Surface.ROTATION_0, 270);
           ORIENTATIONS.append(Surface.ROTATION_90, 0);
           ORIENTATIONS.append(Surface.ROTATION_180, 180);
           ORIENTATIONS.append(Surface.ROTATION_270, 270);
       }
    
    0 讨论(0)
提交回复
热议问题