检查Android是否具有摄像头

发布网友 发布时间:2022-04-22 07:06

我来回答

1个回答

热心网友 时间:2022-06-17 05:28

import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.os.Build;
public class CameraUtils {
/**
* 检测是否有前置摄像头
* @return
*/
public static boolean hasFrontFacingCamera() {
final int CAMERA_FACING_BACK = 1;
return checkCameraFacing(CAMERA_FACING_BACK);
}

/**
* 检测是否有后置摄像头
* @return
*/
public static boolean hasBackFacingCamera() {
final int CAMERA_FACING_BACK = 0;
return checkCameraFacing(CAMERA_FACING_BACK);
}

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 int getSdkVersion() {
return android.os.Build.VERSION.SDK_INT;
}
}

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com