2.33 Camera详解与架构
2.33.1 Camera详解
- 打开
Camera
...
mCamera = Camera.open();
...
// 开启预览
SurfaceTexture surfaceTexture = new SurfaceTexture(textureId);
mCamera.setPreviewTexture(surfaceTexture);
mCamera.startPreview();
// 只采集不渲染
SurfaceTexture surfaceTexture = new SurfaceTexture(0);
mCamera.setPreviewTexture(surfaceTexture);
surfaceTexture.detachFromGLContext();
mCamera.startPreview();
- 设置
Camera
参数
Camera.Parameters params = mCamera.getParameters();
List<Camera.Size> previewSizeList = params.getSupportedPreviewSizes();
...
// 设置预览尺寸
params.setPreviewSize(width, height);
List<Camera.Size> pictureSizeList = params.getSupportedPictureSizes();
// 设置摄像头的分辨率
params.setPictureSize(width, height);
// 设置对焦模式
if (cameraId == Camera.CameraInfo.CAMERA_FACING_BACK) {
params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
}
// 设置闪光灯
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
- 对焦
List<Camera.Area> focusAreas = new ArrayList<Camera.Area>();
focusAreas.add(new Camera.Area(rect, 1000));
params.setFocusAreas(focusAreas);
mCamera.cancelAutoFocus();
mCamera.setParameters(params);
mCamera.autoFocus(this);
- 拍照
mCamera.takePicture(null, null, PictureCallback);
- 切换摄像头
mCamera.stopPreview();
mCamera.release();
mCamera = Camera.open(cameraId);
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
- 打开闪光灯
if (isOpen) {
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
} else {
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
}
mCamera.setParameters(params);
2.33.2 Camera优化
-
启动优化
-
预览优化