How to determine if there is a front camera, and if so, how to reach and use the front camera?

How to determine if there is a front camera, and is there access to the front camera?

+4
source share
4 answers

How to determine if there is a front camera and is there access to the front camera and use it?

There is no API for this, at least not through Android 2.2. With luck, the upcoming release of Gingerbread will add built-in support for front-facing cameras. Excuse me!

+1
source

If you use API level 9 (Android 2.3) or higher, you can do the following to calculate the index of the (first) front camera:

int getFrontCameraId() { CameraInfo ci = new CameraInfo(); for (int i = 0 ; i < Camera.getNumberOfCameras(); i++) { Camera.getCameraInfo(i, ci); if (ci.facing == CameraInfo.CAMERA_FACING_FRONT) return i; } return -1; // No front-facing camera found } 

you can use the index for the Camera.open method, for example:

 int index = getFrontCameraId(); if (index == -1) error(); Camera c = Camera.open(index); 

To get the appropriate camera.

+18
source

Oak your code will not support sdk above 21, so I added these lines to make it useful :)

  int getFrontCameraId(CameraManager cManager) { if (Build.VERSION.SDK_INT < 22) { Camera.CameraInfo ci = new Camera.CameraInfo(); for (int i = 0; i < Camera.getNumberOfCameras(); i++) { Camera.getCameraInfo(i, ci); if (ci.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) return i; } } else { try { for ( int j = 0;j<cManager.getCameraIdList().length; j++) { String[] cameraId = cManager.getCameraIdList(); CameraCharacteristics characteristics = cManager.getCameraCharacteristics(cameraId[j]); int cOrientation = characteristics.get(CameraCharacteristics.LENS_FACING); if (cOrientation == CameraCharacteristics.LENS_FACING_FRONT) return j; } } catch (CameraAccessException e) { e.printStackTrace(); } } return -1; // No front-facing camera found } 

I updated the Oak code, which now supports the new camera2 library.

+5
source

Kulpit, I don’t know how you managed to get the code to work. I tried to edit your answer, but the full answer needs to be changed. Here is what I got.

 @SuppressLint("NewApi" ) int getFrontCameraId() { if (Build.VERSION.SDK_INT < 22) { Camera.CameraInfo ci = new Camera.CameraInfo(); for (int i = 0; i < Camera.getNumberOfCameras(); i++) { Camera.getCameraInfo(i, ci); if (ci.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) return i; } } else { try { CameraManager cManager = (CameraManager) getApplicationContext() .getSystemService(Context.CAMERA_SERVICE); String[] cameraId = cManager.getCameraIdList(); for ( int j = 0;j<cameraId.length; j++) { CameraCharacteristics characteristics = cManager.getCameraCharacteristics(cameraId[j]); int cOrientation = characteristics.get(CameraCharacteristics.LENS_FACING); if (cOrientation == CameraCharacteristics.LENS_FACING_FRONT) return Integer.parseInt(cameraId[j]); } } catch (CameraAccessException e) { e.printStackTrace(); } } return -1; // No front-facing camera found } 
0
source

All Articles