Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Front Camera in Camera2 not capturing image

Tags:

android

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 capture pictures but works fine with Back Camera.

Has anyone implemented the Camera2 Api please help!

Here's the code snippet:

private void setUpCameraOutputs(int width, int height) {
    Activity activity = getActivity();
    CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
    try {
        for (String cameraId : manager.getCameraIdList()) {
            CameraCharacteristics characteristics
                    = manager.getCameraCharacteristics(cameraId);

            // We don't use a front facing camera in this sample.
            int facing = characteristics.get(CameraCharacteristics.LENS_FACING);
            Log.i(TAG,"Front Cam ID: "+ facing);
            if (characteristics.get(CameraCharacteristics.LENS_FACING)==CameraCharacteristics.LENS_FACING_FRONT)
            {



                StreamConfigurationMap map = characteristics.get(
                        CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);


                // For still image captures, we use the largest available size.
                Size largest = Collections.max(
                        Arrays.asList(map.getOutputSizes(ImageFormat.JPEG)),
                        new CompareSizesByArea());
                mImageReader = ImageReader.newInstance(largest.getWidth(), largest.getHeight(),
                        ImageFormat.JPEG, /*maxImages*/2);
                mImageReader.setOnImageAvailableListener(
                        mOnImageAvailableListener, mBackgroundHandler);

                // Danger, W.R.! Attempting to use too large a preview size could  exceed the camera
                // bus' bandwidth limitation, resulting in gorgeous previews but the storage of
                // garbage capture data.
                mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class),
                        width, height, largest);

                // We fit the aspect ratio of TextureView to the size of preview we picked.
                int orientation = getResources().getConfiguration().orientation;
                if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
                    mTextureView.setAspectRatio(
                            mPreviewSize.getWidth(), mPreviewSize.getHeight());
                } else {
                    mTextureView.setAspectRatio(
                            mPreviewSize.getHeight(), mPreviewSize.getWidth());
                }

                mCameraId = cameraId;
                return;
            }
            else
            {
                onActivityCreated(Bundle.EMPTY);
            }
        }
    } catch (CameraAccessException e) {
        e.printStackTrace();
    } catch (NullPointerException e) {
        // Currently an NPE is thrown when the Camera2API is used but not supported on the
        // device this code runs.
        ErrorDialog.newInstance(getString(R.string.camera_error))
                .show(getChildFragmentManager(), FRAGMENT_DIALOG);
    }
}
like image 400
Veeresh Avatar asked Oct 14 '15 13:10

Veeresh


2 Answers

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();
    }
}
like image 82
D-rk Avatar answered Oct 26 '22 15:10

D-rk


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!!

like image 34
Arvind Singh Avatar answered Oct 26 '22 15:10

Arvind Singh