8

In my camera app i have a button to change the camera facing in to front or back ,I can capture and save images using back camera ,but when i switch to front camera i could not capture images . This is how i am switching camera to front or back .

   ImageView switch_camera =(ImageView) rootview.findViewById(R.id.imageView7);
        switch_camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


         //       facing = characteristics.get(CameraCharacteristics.LENS_FACING);

                if (facing != null && facing == CameraCharacteristics.LENS_FACING_FRONT) {
                    //isfrontcam=true;
                    try {

                        //manager.openCamera(getBackFacingCameraId(manager), mStateCallback, mBackgroundHandler);
                        closeCamera();
                        openCamera(mTextureView.getWidth(), mTextureView.getHeight(),"0");
                        Log.e("opening ","BackCam");
                        facing = 1;

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

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

                    }
                } else if (facing != null && facing == CameraCharacteristics.LENS_FACING_BACK) {
                    //  isfrontcam = true;
                    try {
                        //manager.openCamera(getFrontFacingCameraId(manager), mStateCallback, mBackgroundHandler);

                      //  characteristics = manager.getCameraCharacteristics("1");

                        closeCamera();
                        openCamera(mTextureView.getWidth(), mTextureView.getHeight(),"1");

                        Log.e("opening ", "FrontCam");
                        String str = getBackFacingCameraId(manager);
                        facing= 0;
                        Log.e("str", "id" + str);

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

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

On capture button click ,i am calling this function to capture images ;

 private void lockFocus() {
        try {
            // This is how to tell the camera to lock focus.
            mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
                    CameraMetadata.CONTROL_AF_TRIGGER_START);
            // Tell #mCaptureCallback to wait for the lock.
            mState = STATE_WAITING_LOCK;
            mCaptureSession.capture(mPreviewRequestBuilder.build(),mCaptureCallback,
                    mBackgroundHandler);
        } catch (CameraAccessException e) {

            e.printStackTrace();
        }
    }
Tom
  • 296
  • 6
  • 22

1 Answers1

18

Check your CameraCaptureSession.CaptureCallback : probably the camera has the state CONTROL_AF_STATE_INACTIVE. And as it is waiting for the focus...the picture will never been taken.

it should be like this

            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;
            }
Gigi
  • 359
  • 2
  • 16
  • Works for me :) Thanks – Al Wld Oct 07 '16 at 19:16
  • Maybe you know how to solve my issue, how can i take a picture only when camera in focus? There is my question http://stackoverflow.com/questions/40185407/how-to-take-picture-only-if-image-in-focus-camera-2-api Thanks in advance! – Sirop4ik Oct 22 '16 at 09:46
  • 3
    This answer allowed me to run the Camera2basic sample within the standard emulator. Thank you :) – Isolin Aug 20 '17 at 14:08