gpt4 book ai didi

java - 安卓/Java : detect if device has a BACK Camera

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:51:52 26 4
gpt4 key购买 nike

如何检测设备背面是否有摄像头?

这是我用来检测设备是否有手电筒的代码:

if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH) 
|| (_camera.getParameters().getSupportedFlashModes() != null)) hasFlashlight = true;
else hasFlashlight = false;

但即使设备只有前置摄像头而没有手电筒,它也是TRUE

最佳答案

对于 API >= 9 :您可以使用 Camera.getCameraInfo像这样:

int backCameraId = -1;
for(int i=0;i<Camera.getNumberOfCameras();i++){
CameraInfo cameraInfo = new CameraInfo();
Camera.getCameraInfo(i,cameraInfo);
if(cameraInfo.facing==CameraInfo.CAMERA_FACING_BACK) {
backCameraId = i;
break;
}
}
Log.d(TAG, "back camera exists ? "+(backCameraId>-1));
Log.d(TAG, "back camera id :"+backCameraId);

API >= 21,建议使用Camera2应用程序接口(interface):

String backCameraId = null;
CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
for(String cameraId:manager.getCameraIdList()){
CameraCharacteristics cameraCharacteristics = manager.getCameraCharacteristics(cameraId);
Integer facing = cameraCharacteristics.get(CameraCharacteristics.LENS_FACING);
if(facing==CameraMetadata.LENS_FACING_BACK) {
backCameraId = cameraId;
break;
}
}
Log.d(TAG, "back camera exists ? "+(backCameraId!=null));
Log.d(TAG, "back camera id :"+backCameraId);

关于java - 安卓/Java : detect if device has a BACK Camera,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26107696/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com