Android open front camera by default

I need to open the default front camera in my application. If the user presses the button, the front camera should be open.

In my code, I get the number of cameras present in the current Android device, and if the device has two cameras, I will take the identifier of the second camera and write some camera logic there.

Please help me create this code

0
source share
5 answers

try this code. It works great :)

private Camera openFrontFacingCameraGingerbread() { int cameraCount = 0; Camera cam = null; Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); cameraCount = Camera.getNumberOfCameras(); for (int camIdx = 0; camIdx<cameraCount; camIdx++) { Camera.getCameraInfo(camIdx, cameraInfo); if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { try { cam = Camera.open(camIdx); } catch (RuntimeException e) { Log.e("Your_TAG", "Camera failed to open: " + e.getLocalizedMessage()); } } } return cam; } 
0
source

adding androidmanifest xml file in this ..try out

 <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" android:required="false" /> <uses-feature android:name="android.hardware.camera.front" android:required="false" /> 
0
source

I use this code, it will work for hope :)

 Camera c = null; // object that use Camera.CameraInfo info = new Camera.CameraInfo(); int count = Camera.getNumberOfCameras(); for (int i = 0; i<cameraCount; i++) { Camera.getCameraInfo(i, info); if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { try { c = Camera.open(i); } catch (RuntimeException e) { // Handle } } } 
0
source

Just write this line

Camera mCamera = null;

mCamera = Camera.open (1); // for the front camera

mCamera = Camera.open (0); // for rear view camera

0
source

You must change this line,

Camera mCamera = null;

mCamera = Camera.open (1); // for the front camera

mCamera = Camera.open (0); // for rear view camera

0
source