Android / Java:检测设备是否有BACK摄像头

前端之家收集整理的这篇文章主要介绍了Android / Java:检测设备是否有BACK摄像头前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何检测设备背面是否有摄像头?

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

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

但即使设备只有没有手电筒的前置摄像头,它也是正确的.

解决方法

对于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 API:

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);
原文链接:https://www.f2er.com/android/317103.html

猜你在找的Android相关文章