如何从Android摄像头实例中获取当前打开的Android摄像头的ID?我在参数中看不到它,getCameraInfo需要id作为参数。
答案 0 :(得分:27)
没有办法获取当前打开的Android摄像头的ID。我打开它时最终存储了id。
答案 1 :(得分:17)
这只是一些相机,所以你循环寻找你想要的相机。
这是一个找到前置摄像头的片段:
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;
}
}
答案 2 :(得分: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;
}
答案 3 :(得分:1)
对于具有更新的相机API的Kotlin:
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
}
}
facing
的有效值为:
CameraCharacteristics.LENS_FACING_FRONT
CameraCharacteristics.LENS_FACING_BACK
CameraCharacteristics.LENS_FACING_EXTERNAL