Android 检查当前设备是否有摄像头

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jing85432373/article/details/51898099

直接上代码

package com.app.emdk;

import android.hardware.Camera;
import android.os.Build;

/**
 * Created by WangXM on 2016/7/13.
 */
public class CameraProvider {

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

    /**
     * 检查设备是否有摄像头
     * @return
     */
    public static boolean hasCamera() {
        return hasBackFacingCamera() || hasFrontFacingCamera();
    }

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

    /**检查设备是否有前置摄像头
     * @return
     */
    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及其以上。
PS:貌似现在找个4.0以下的手机也有点难了,这个问题可以直接忽略...

展开阅读全文

没有更多推荐了,返回首页