Kitesurfer Kitesurfer - 4 months ago 7
Android Question

Is Camera the Front or Back Facing

when i open the Camera like this:

Camera cam = Camera.open();


or

Camera cam = Camera.open(number);


How can i get information that this is the Front or Back Camera ? Some Devices may have both, some just one.

I need that Information when writing some logic of my code, also for logging.

regards,
Kitesurfer

Answer

First of all you may want to look at the reference of Camera class.

Methods getNumberOfCameras (does this device have only one camera or both?) and getCameraInfo (https://developer.android.com/reference/android/hardware/Camera.CameraInfo.html) will provide you with the exact information that you need.

There is an example of using both these methods:

private int findFrontFacingCamera() {
    int cameraId = -1;
    // Search for the front facing camera
    int numberOfCameras = Camera.getNumberOfCameras();
    for (int i = 0; i < numberOfCameras; i++) {
      CameraInfo info = new CameraInfo();
      Camera.getCameraInfo(i, info);
      if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
        Log.d(DEBUG_TAG, "Camera found");
        cameraId = i;
        break;
      }
    }
    return cameraId;
}
Comments