get Back Camera Id - Android android.hardware

Android examples for android.hardware:Back Camera

Description

get Back Camera Id

Demo Code

import android.hardware.Camera;

public class Main {
  private static int cameraNumber = -1;
  private static int backCameraId = -1;

  public static int getBackCameraId() {
    if (backCameraId == -1) {
      Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
      for (int i = 0; i < getCameraNumber(); i++) {
        Camera.getCameraInfo(i, cameraInfo);
        if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
          backCameraId = i;//from www  .j  a v  a2  s  .  c om
          break;
        }
      }
    }
    return backCameraId;
  }

  public static int getCameraNumber() {
    if (cameraNumber == -1) {
      cameraNumber = Camera.getNumberOfCameras();
    }
    return cameraNumber;
  }

  public static Camera.CameraInfo getCameraInfo(int id) {
    if (id >= 0 && id < getCameraNumber()) {
      Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
      Camera.getCameraInfo(id, cameraInfo);
      return cameraInfo;
    }
    return null;
  }

}

Related Tutorials