2

I know i am asking question is possibly duplicate. but i tried all things but not succeeded in any . i have try the following things to switch camera :

numberOfCamera = Camera.getNumberOfCameras();
            if(camId == Camera.CameraInfo.CAMERA_FACING_BACK){
                camId = Camera.CameraInfo.CAMERA_FACING_FRONT;
                Toast.makeText(getApplicationContext(), "BACK TO FRONT" ,
    1000).show();
                    try {
                        camera.release();
                        camera = Camera.open(camId);
                        camera.setPreviewDisplay(surfaceHolder);
                        camera.startPreview();
                        previewing = true;
                    } catch (RuntimeException e) {

                } catch (IOException e) {}
            }else if(camId == Camera.CameraInfo.CAMERA_FACING_FRONT){
                camId = Camera.CameraInfo.CAMERA_FACING_BACK;
                Toast.makeText(getApplicationContext(), "FRONT TO BACK" , 



     1000).show();
                        try {
                            camera.release();
                            camera = Camera.open(camId);
                            camera.setPreviewDisplay(surfaceHolder);
                            camera.startPreview();
                        } catch (RuntimeException e) {

                } catch (IOException e) {}
            }

--this code is not working..it is showing toast..but not switching camera.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • In your code, for each exception call e.printStackTrace(), and get the logs and post it here. – Eric B. Nov 30 '15 at 06:18
  • try to call `camera.stopPreview()` before `camera.release()` – calvinfly Nov 30 '15 at 06:19
  • @calvinify .. not working –  Nov 30 '15 at 06:22
  • Please add `Log.e("CameraSwitch", "failed to open camera " + camId, e)` to `catch (RuntimeException e)` and let us see the runtime exception. – Alex Cohn Nov 30 '15 at 08:05
  • Related question: *[How to open “front camera” on android platform?](http://stackoverflow.com/questions/2779002/how-to-open-front-camera-on-android-platform)* – Alex Cohn May 14 '17 at 08:03

1 Answers1

1

This is how I did it on a side project. It's a custom surface view which defines a method for switching camera.

Note: as per Alex's comment, my initial answer was incorrect. I have updated the answer but I haven't tested it nor updated the file linked above.

Here's a summary of what is done:

You need your current Camera ID in a class variable

int currentCameraId;

Backup the Camera.Parameters

You need to reuse the height and width information later on.

Camera.Parameters parameters = camera.getParameters();

Stop preview

if (inPreview) {
  camera.stopPreview();
}

Release Camera

camera.release();

Get the new camera ID

int cameraCount = Camera.getNumberOfCameras();
if (cameraCount == 2) {
    // simply switch between 0 and 1 if there are only 2 cameras
    currentCameraId = currentCameraId == 0 ? 1 : 0;
} else {
    Camera.CameraInfo currentCameraInfo = new Camera.CameraInfo();
    Camera.getCameraInfo(currentCameraId, currentCameraInfo);
    // switch between front and back facing
    int wantedCameraFacing = currentCameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT ? Camera.CameraInfo.CAMERA_FACING_BACK : Camera.CameraInfo.CAMERA_FACING_FRONT;
    currentCameraId = switchCamera(cameraCount, wantedCameraFacing);
}

switchCamera method (untested)

int switchCamera(int cameraCount, int wantedCameraFacing) {
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    for (int camId = 0; camId < cameraCount; camId++) {
        Camera.getCameraInfo(camId, cameraInfo);
        if (cameraInfo.facing == wantedCameraFacing) {
            // selects the 1st camera which faces the correct way
            return camId;
        }
    }
}

Open the Camera

camera = Camera.open(currentCameraId);

Init Preview with saved parameters

camera.setPreviewDisplay(sHolder);
// define parameters, e.g. parameters.setPreviewSize()
camera.setParameters(parameters);

Start Preview

camera.startPreview();

Usage

Set this custom view in your activity layout. Create a callback function for your button's onClick action. Then you can simply call:

public void switchCamera(View v) {
      preview.switchCamera();
}
Philippe A
  • 1,252
  • 9
  • 22