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:
- Move the code that creates mCameraView to startCam().
- 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:
- You can keep the same CameraView if you swap its mCamera.
- 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.
- The preferred way is to use a background HandlerThread so that the Camera callbacks also happen on the background.
- 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.
- 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
How to Clear the (Css) Visited History of an Android Webview
Androidruntime Error: Parcel: Unable to Marshal Value
How to Use Okhttp to Upload a File
Android Heterogeneous Gridview Like Pinterest
Access Resource Files in Android
How to Start Activity in Adapter
How to Connect Android with MySQL Using MySQL Jdbc Driver
How to Include Jar Dependency into an Aar Library
Android - Copy Assets to Internal Storage
Why Use Enums Instead of Constants? Which Is Better in Terms of Software Design and Readability
How to Implement "CSS Versioning" (To Solve Cache Issues) Using Jsf 2 H:Outputstylesheet
Asynctask.Executeonexecutor() Before API Level 11
Concise Way of Writing New Dialogpreference Classes
Android: How to Disable List Items on List Creation
Java Lang Unsupportedclassversion Error in Xamarin Studio
How to Properly Use Backwards Compatible Vector Drawable with the Latest Android Support Library