How do I open the "front camera" on the Android platform? How do I open the "front camera" on the Android platform? android android

How do I open the "front camera" on the Android platform?


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(TAG, "Camera failed to open: " + e.getLocalizedMessage());            }        }    }    return cam;}

Add the following permissions in the AndroidManifest.xml file:

<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" />

Note: This feature is available in Gingerbread(2.3) and Up Android Version.


All older answers' methods are deprecated by Google (supposedly because of troubles like this), since API 21 you need to use the Camera 2 API:

This class was deprecated in API level 21. We recommend using the new android.hardware.camera2 API for new applications.

In the newer API you have almost complete power over the Android device camera and documentation explicitly advice to

String[] getCameraIdList()

and then use obtained CameraId to open the camera:

void openCamera(String cameraId, CameraDevice.StateCallback callback, Handler handler)

99% of the frontal cameras have id = "1", and the back camera id = "0"according to this:

Non-removable cameras use integers starting at 0 for their identifiers, while removable cameras have a unique identifier for each individual device, even if they are the same model.

However, this means if device situation is rare like just 1-frontal -camera tablet you need to count how many embedded cameras you have, and place the order of the camera by its importance ("0"). So CAMERA_FACING_FRONT == 1 CAMERA_FACING_BACK == 0, which implies that the back camera is more important than frontal.

I don't know about a uniform method to identify the frontal camera on all Android devices. Simply said, the Android OS inside the device can't really find out which camera is exactly where for some reasons: maybe the only camera hardcoded id is an integer representing its importance or maybe on some devices whichever side you turn will be .. "back".

Documentation: https://developer.android.com/reference/android/hardware/camera2/package-summary.html

Explicit Examples: https://github.com/googlesamples/android-Camera2Basic


For the older API (it is not recommended, because it will not work on modern phones newer Android version and transfer is a pain-in-the-arse). Just use the same Integer CameraID (1) to open frontal camera like in this answer:

cam = Camera.open(1);

If you trust OpenCV to do the camera part:

Inside

    <org.opencv.android.JavaCameraView    ../>

use the following for the frontal camera:

        opencv:camera_id="1"


As of Android 2.1, Android only supports a single camera in its SDK. It is likely that this will be added in a future Android release.