Android Camera开发入门 第六篇: Camera2相机 打开功能实现

本课程内容由 @小驰笔记 出品,欢迎关注,获取更多交流信息~
欢迎访问个人博客:www.xiaochibiji.com`

流程图:

1、获取当前设备支持摄像头颗数

CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
manager.getCameraIdList();

2、获取到对应摄像头的ID

CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE)

for (String cameraId: manager.getCameraIdList()) {             
     mCameraCharacteristics = manager.getCameraCharacteristics(mCameraId);

     // We don't use a front facing camera in this sample.
     Integer facing = mCameraCharacteristics.get(CameraCharacteristics.LENS_FACING);

     if (facing != null && facing == CameraCharacteristics.LENS_FACING_BACK) {
	 continue;
     }
}
` ``

## 3、根据camera id打开摄像头

```CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
manager.openCamera(mCameraId,mStateCallback, mBackgroundHandler);

4、camera状态监听

privatefinal CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
        @Override
        public void onOpened(@NonNull CameraDevice cameraDevice) {

        }

        @Override
        public void onDisconnected(@NonNull CameraDevice cameraDevice) {

        }

        @Override
        public void onError(@NonNull CameraDevice cameraDevice, int error) {

        }
};

5、相关api接口

//1) CameraManager,获取camera服务
CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);

//2) CameraCharacteristics,获取camera相关参数
CameraCharacteristics mCameraCharacteristics = manager.getCameraCharacteristics(mCameraId);

//facing信息
Integer facing = mCameraCharacteristics.get(CameraCharacteristics.LENS_FACING);

//stream map 信息
StreamConfigurationMap map = mCameraCharacteristics.get(
                        CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
//3)open camera
manager.openCamera(mCameraId,mStateCallback, mBackgroundHandler);

CameraDevice mCameraDevice;
CameraDevice.StateCallback
CaptureRequest.Builder

//4)创建session
mCameraDevice.createCaptureSession(Arrays.asList(surface, mImageReader.getSurface()),
                    new CameraCaptureSession.StateCallback() {
}

//5) repeatingRequest
CameraCaptureSession mPreviewCaptureSession;
mPreviewCaptureSession.setRepeatingRequest(mPreviewRequest,
                                        null, mBackgroundHandler);

6、总结正常流程

推荐阅读:

关于我

今年,做好一件事情

从一件事改变做起

一篇文章带你了解Android 最新Camera框架

深圳上班,

从事Android Camera相关软件开发工作,

记录生活和工作的点滴,

点击关注“小驰笔记”,期待和你相遇~

posted @ 2022-09-01 23:03  小驰行动派  阅读(60)  评论(0编辑  收藏  举报  来源