Android: Switch Camera When Button Clicked

Android: Switch camera when button clicked

Button otherCamera = (Button) findViewById(R.id.OtherCamera);

otherCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (inPreview) {
camera.stopPreview();
}
//NB: if you don't release the current camera before switching, you app will crash
camera.release();

//swap the id of the camera to be used
if(currentCameraId == Camera.CameraInfo.CAMERA_FACING_BACK){
currentCameraId = Camera.CameraInfo.CAMERA_FACING_FRONT;
}
else {
currentCameraId = Camera.CameraInfo.CAMERA_FACING_BACK;
}
camera = Camera.open(currentCameraId);

setCameraDisplayOrientation(CameraActivity.this, currentCameraId, camera);
try {

camera.setPreviewDisplay(previewHolder);
} catch (IOException e) {
e.printStackTrace();
}
camera.startPreview();
}

If you want to make the camera image show in the same orientation as
the display, you can use the following code.

public static void setCameraDisplayOrientation(Activity activity,
int cameraId, android.hardware.Camera camera) {
android.hardware.Camera.CameraInfo info =
new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
int rotation = activity.getWindowManager().getDefaultDisplay()
.getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0: degrees = 0; break;
case Surface.ROTATION_90: degrees = 90; break;
case Surface.ROTATION_180: degrees = 180; break;
case Surface.ROTATION_270: degrees = 270; break;
}

int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(result);
}

Switching Front and Back Camera Automatically

Your CameraView is not synchronized with the MainActivity. Both hold reference to mCamera, but when the Activity swaps the camera, the View is not informed.

The minimal change to your code would be:

  1. Move the code that creates mCameraView to startCam().
  2. In cleanup(), instead of mCamera.stopPreview() etc, remove mCameraView from the FrameLayout.

Now when the timer event happens, the framework will call CameraView.SurfaceDestroyed() to release mCamera, and after that you will create a new CameraView for the front-facing camera.

Few additional notes:

  1. You can keep the same CameraView if you swap its mCamera.
  2. It is a bad practice to run Camera.open() on the UI thread, this call may be slow on some devices, and even cause ANR.
  3. The preferred way is to use a background HandlerThread so that the Camera callbacks also happen on the background.
  4. Google fixed this in the new camera2 API (available on Lollipop and higher). Your minSdkVersion being 21, you have all reasons to use this new API.
  5. Use FLAG_KEEP_SCREEN_ON while camera preview is live.

Fliping the Front Camera to Back Camera in Button Click using android

Finally, i found the solution for the problem. Here is my modification to flip the camera

private void flipcamera() {

if (camera != null)
{
System.out.println("flipcamera");
camera.stopPreview();
camera.release();
camera = null;

}

camera = Camera.open(camFront);

if (camera != null) {
try {
camera.setPreviewDisplay(sHolder.getHolder());
camera.startPreview();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

Thanks guys.



Related Topics



Leave a reply



Submit