如何检测设备背面是否有摄像头?
这是我检测设备是否有手电筒的代码:
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)
|| (_camera.getParameters().getSupportedFlashModes() != null)) hasFlashlight = true;
else hasFlashlight = false;
但即使设备只有没有手电筒的前置摄像头,它也是 TRUE 。
答案 0 :(得分:18)
对于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&gt; = 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);
答案 1 :(得分:2)
尝试检查相机的数量:
import android.hardware.Camera;
int numberOfCameras = Camera.getNumberOfCameras();
如果大于1,可以肯定有后置摄像头。
答案 2 :(得分:1)
public boolean isRearCameraAvailable(Context context) {
return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA);
}
检查设备是否有远离设备用户的摄像头
答案 3 :(得分:0)
CameraManager mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
/** Which way the camera is facing. */
public static enum Facing {
FRONT, BACK;
}
/** Returns the ID of the first camera facing the given direction. */
private String findCameraId(Facing facing) {
if (facing == Facing.FRONT) {
return findFirstFrontCameraId();
} else {
return findFirstBackCameraId();
}
}
/** Returns the ID of the first back-facing camera. */
private String findFirstBackCameraId() {
Log.d(TAG, "Getting First BACK Camera");
String cameraId = findFirstCameraIdFacing(CameraCharacteristics.LENS_FACING_BACK);
if (cameraId == null) {
Log.w(TAG, "No back-facing camera found.");
}
return cameraId;
}
/** Returns the ID of the first front-facing camera. */
private String findFirstFrontCameraId() {
Log.d(TAG, "Getting First FRONT Camera");
String cameraId = findFirstCameraIdFacing(CameraCharacteristics.LENS_FACING_FRONT);
if (cameraId == null) {
Log.w(TAG, "No front-facing camera found.");
}
return cameraId;
}
/** Returns the ID of the first camera facing the given direction. */
private String findFirstCameraIdFacing(int facing) {
try {
String[] cameraIds = mCameraManager.getCameraIdList();
for (String cameraId : cameraIds) {
CameraCharacteristics characteristics = mCameraManager
.getCameraCharacteristics(cameraId);
if (characteristics.get(CameraCharacteristics.LENS_FACING) == facing) {
return cameraId;
}
}
} catch (CameraAccessException ex) {
Log.w(TAG, "Unable to get camera ID", ex);
}
return null;
}