问题
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.
回答1:
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.
回答2:
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;
}
}
回答3:
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;
}
回答4:
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
来源:https://stackoverflow.com/questions/21559699/get-id-of-currently-open-camera