Android CameraManager 使用

Android CameraManager 使用

本篇文章简单介绍下android 中cameraManager 的使用.

1: 简介

cameraManager 用于管理设备摄像头的类,一系列方法来获取摄像头的信息.

详细的方法可以参考:

https://developer.android.com/reference/android/hardware/camera2/CameraManager

2: 获取实例

获取CameraManager实例的代码也很简单,通过getSystemService获取即可.

 CameraManager manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);

3: 获取摄像头个数

public class CameraUtils {
    private static final String TAG = "CameraUtils";

    public static int getCameraCount(Context context) {
        if (Build.VERSION_CODES.LOLLIPOP <= Build.VERSION.SDK_INT) {
            CameraManager manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
            try {
                return manager.getCameraIdList().length;
            } catch (Throwable e) {
                Log.e(TAG, "getCameraCount: ", e);
            }
        }
        try {
            return Camera.getNumberOfCameras();
        } catch (Throwable e) {
            Log.e(TAG, "getCameraCount: ", e);
        }
        return -1;
    }


}

4: 获取摄像头的id列表

public static String[] getCameraIdList(Context context) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            CameraManager manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
            try {
                return manager.getCameraIdList();
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
        return new String[]{};
}

5:获取相机设备信息

    public static void getCameraCharacteristics(Context context,String id){
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            CameraManager manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
            try {
                CameraCharacteristics cameraCharacteristics = manager.getCameraCharacteristics(id);
                for (CameraCharacteristics.Key<?> key : cameraCharacteristics.getKeys()) {
                    Log.d(TAG, key.getName() + ": " + cameraCharacteristics.get(key));
                }
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }

你可能感兴趣的:(android,android)