Android中判断是否有前置摄像头,后置摄像头的方法

2024-12-22 23:47:49
推荐回答(1个)
回答1:

以下代码为一系列的方法,用来判断是否有前置摄像头(Front Camera),后置摄像头。

复制代码代码如下:

private static boolean checkCameraFacing(final int facing) {
if (getSdkVersion() < Build.VERSION_CODES.GINGERBREAD) {
return false;
}
final int cameraCount = Camera.getNumberOfCameras();
CameraInfo info = new CameraInfo();
for (int i = 0; i < cameraCount; i++) {
Camera.getCameraInfo(i, info);
if (facing == info.facing) {
return true;
}
}
return false;
}

public static boolean hasBackFacingCamera() {
final int CAMERA_FACING_BACK = 0;
return checkCameraFacing(CAMERA_FACING_BACK);
}
public static boolean hasFrontFacingCamera() {
final int CAMERA_FACING_BACK = 1;
return checkCameraFacing(CAMERA_FACING_BACK);
}
public static int getSdkVersion() {
return android.os.Build.VERSION.SDK_INT;
}

注意:由于getNumberOfCameras以及getCameraInfo均为API 9 引入,所以方法只适用于2.3及其以上。