2

I am following the sample available at: http://developer.android.com/guide/topics/media/camera.html

for developing an camera application.

This app shows a camera preview and a button and when user clicks on the button picture is taken and saved and camera preview is shown again.

But I want to switch to the front camera now. Any idea on how to do this? It is possible to start with either front or back camera but I could not change it while running as if I call a new camera instance the app crashes.

Any idea how this can be done?

Thanks In Advance, Perumal

mychemicalro
  • 232
  • 3
  • 21
perumal316
  • 1,159
  • 6
  • 17
  • 36

4 Answers4

3

You can only have one Camera open at a time. That is, you will have to release() the back camera before you open(1) the front camera.

Jon Shemitz
  • 1,235
  • 13
  • 29
1

You can open any camera that you want, see below link

http://developer.android.com/reference/android/hardware/Camera.html#open(int)

As shown from the above link, you shoud use

c = Camera.open(int) instead of c = Camera.open()

Chirag_CID
  • 2,224
  • 1
  • 24
  • 33
  • Hi. Yes, I have tried it, this can be used to switch camera. But what I am trying to do is after taking a picture the application should switch to the front camera. I tried calling a new camera instance with the front camera parameter (Camera.open(1)), but it crashes. – perumal316 Apr 19 '12 at 01:22
1

Here is how I've done:

private Camera mCamera=null;
private CameraPreview mPreview;
public FrameLayout preview;

        @Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.camera_preview);
    View myView= (View) findViewById(R.id.camera_previeww);
    myView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

    cameraID= Camera.CameraInfo.CAMERA_FACING_FRONT;
    mCamera=openCamera(cameraID);
    mCamera.startPreview();

    // Create our Preview view and set it as the content of our activity.
    mPreview = new CameraPreview(this, mCamera);
    FrameLayout preview = (FrameLayout) this.findViewById(R.id.camera_previeww);
    preview.addView(mPreview);
}

public void switchCamera(){

    mCamera.stopPreview();
    releaseCamera();
    if (cameraID==Camera.CameraInfo.CAMERA_FACING_BACK){
        cameraID=Camera.CameraInfo.CAMERA_FACING_FRONT;
    }else{
        cameraID=Camera.CameraInfo.CAMERA_FACING_BACK;
    }

    mCamera=openCamera(cameraID);
    mCamera.startPreview();

    CameraPreview mPreview = new CameraPreview(this, mCamera);
    preview = (FrameLayout) this.findViewById(R.id.camera_previeww);
    preview.removeAllViews();
    preview.addView(mPreview);

}
public  Camera openCamera(int cameraIDD){
    Camera c=null;
    try{
        c=Camera.open(cameraIDD);
    }catch (Exception e){
        Log.d("Camera Activity", e.getMessage());
    }
    return c;

}

You have only to call switch() method to change from back to front or front to back. The activity was starting by calling startActivity(this, Class) from another activity.

mychemicalro
  • 232
  • 3
  • 21
0

A possible solution is getting holder of SurfaceView (on the scenario I was attempting to fix was Switching from front camera to back camera and viceversa).

    public void switchCamera(){
    Log.i(TAG, "Switching Camera");
    if (mCamera != null) {
        mCamera.stopPreview();
        mCamera.release();
        //mCamera = null;
    }

    //swap the id of the camera to be used
    if (camId == Camera.CameraInfo.CAMERA_FACING_BACK) {
        camId = Camera.CameraInfo.CAMERA_FACING_FRONT;
    }else {
        camId = Camera.CameraInfo.CAMERA_FACING_BACK;
    }
    try {
        mCamera = Camera.open(camId);
        //mCamera.setDisplayOrientation(90);
        //You must get the holder of SurfaceView!!!
        mCamera.setPreviewDisplay(mView.getHolder());
        //Then resume preview...
        mCamera.startPreview();
    }
    catch (Exception e) { e.printStackTrace(); }
}

Hope it helps for anyone running into similar issue.

Eduardo Gomez
  • 425
  • 3
  • 12