30

How can I get the ID of the currently open android camera from an android camera instance? I can't see it in the parameters and getCameraInfo requires the id as a parameter.

mbdavis
  • 3,861
  • 2
  • 22
  • 42
  • 4
    If by "currently open android camera", you mean one that you opened with `Camera.open()`, you need to hold onto that `int` value yourself. – CommonsWare Feb 04 '14 at 18:01
  • 1
    @CommonsWare okay, so there's definitely no way to tell from the camera instance? Seems a bit strange – mbdavis Feb 04 '14 at 18:20
  • I can't quibble with the "strange" adjective. :-) – CommonsWare Feb 04 '14 at 18:27
  • 1
    Ok, but what if I opened the camera with Camera.open() without a specific cameraId because I need the main back-facing camera? Is it always CameraId = 0? – ARLabs Sep 19 '14 at 13:33
  • 6
    This is really strange, you can get CameraInfo with its number, but not from a camera object. And you can't get camera number from camera. Weird. – flaschenpost Jan 29 '15 at 16:24

4 Answers4

29

There isn't a way to get the id of the currently open android camera. I ended up storing the id when I opened it.

mbdavis
  • 3,861
  • 2
  • 22
  • 42
  • Your way is best idea. Really surprised when Google company still not support this checker : currently is Back camera or Front camera. Thank you. Store in single ton way is best way. – Huy Tower Mar 23 '15 at 08:20
  • 4
    And which method of the Camera do yout use to find the id? – androidevil Mar 25 '15 at 16:39
  • 1
    @androidevil There's no method to find the id, I stored whichever integer 'id' I chose when I called `Camera.open(id)` – mbdavis Nov 16 '15 at 14:57
23

It is just a number of the camera, so you loop through looking for the camera you want.

Here is a snippet to find the front-facing camera:

int cameraId = -1;
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;
  }
}
James Black
  • 41,583
  • 10
  • 86
  • 166
  • 4
    This gets the ID of the front camera, I need the ID of the camera that is currently being used! Sorry for confusion. – mbdavis Feb 04 '14 at 18:19
  • 1
    @mbdavis - I don't think you can get that, but if you loop through as above and try to open each camera, the one you can't open is in use, but remember to release them immediately after opening. http://developer.android.com/reference/android/hardware/Camera.html – James Black Feb 04 '14 at 19:03
2
  private int findFrontFacingCameraID() {
        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(TAG, "Camera found");
            cameraId = i;
            break;
          }
        }
        return cameraId;
     }
supersabbath
  • 364
  • 3
  • 8
0

For Kotlin with newer camera API:

fun getCameraId(context: Context, facing: Int): String {
    val manager = context.getSystemService(CAMERA_SERVICE) as CameraManager

    return manager.cameraIdList.first {
        manager
            .getCameraCharacteristics(it)
            .get(CameraCharacteristics.LENS_FACING) == facing
    }
}

Valid values for facing are:

CameraCharacteristics.LENS_FACING_FRONT
CameraCharacteristics.LENS_FACING_BACK
CameraCharacteristics.LENS_FACING_EXTERNAL
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135