Android 获取当前打开的相机的ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21559699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get ID of currently open camera
提问by mbdavis
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.
如何从 android 相机实例获取当前打开的 android 相机的 ID?我在参数中看不到它,getCameraInfo 需要 id 作为参数。
采纳答案by mbdavis
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.
没有办法获取当前打开的 android 相机的 id。当我打开它时,我最终存储了 id。
回答by James Black
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;
}
}
回答by supersabbath
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;
}
回答by Mateen Ulhaq
For Kotlin with newer camera API:
对于具有较新相机 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
}
}
Valid values for facing
are:
的有效值为facing
:
CameraCharacteristics.LENS_FACING_FRONT
CameraCharacteristics.LENS_FACING_BACK
CameraCharacteristics.LENS_FACING_EXTERNAL