来自官网的模型图,展示了相关的工作流程
重新设计 Android Camera API 的目的在于大幅提高应用对于 Android 设备上的相机子系统的控制能力,同时重新组织 API,提高其效率和可维护性。
在CaptureRequest中设置不同的Surface用于接收不同的图片数据,最后从不同的Surface中获取到图片数据和包含拍照相关信息的CaptureResult。
通过设计框架的改造和优化,Camera2具备了以下优点:
框架上的变化,对整个使用流程变化也非常大,首先了解一些主要的开发类
相机系统服务,用于管理和连接相机设备
相机设备类,和Camera1中的Camera同级
主要用于获取相机信息,内部携带大量的相机信息,包含摄像头的正反(LENS_FACING
)、AE模式、AF模式等,和Camera1中的Camera.Parameters类似
相机捕获图像的设置请求,包含传感器,镜头,闪光灯等
CaptureRequest的构造器,使用Builder模式,设置更加方便
请求抓取相机图像帧的会话,会话的建立主要会建立起一个通道。一个CameraDevice一次只能开启一个CameraCaptureSession。 源端是相机,另一端是 Target,Target可以是Preview,也可以是ImageReader。
用于从相机打开的通道中读取需要的格式的原始图像数据,可以设置多个ImageReader。
CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
复制代码
for (String cameraId : cameraManager.getCameraIdList()) {
CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId);
Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
if (null != facing && facing == CameraCharacteristics.LENS_FACING_FRONT) {
continue;
}
....
}
复制代码
这里默认选择前置摄像头,并获取相关相机信息。
mImageReader = ImageReader.newInstance(largest.getWidth(), largest.getHeight(), ImageFormat.JPEG, 2);
mImageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader reader) {
Log.d("DEBUG", "##### onImageAvailable: " + mFile.getPath());
mBackgroundHandler.post(new ImageSaver(reader.acquireNextImage(), mFile));
}
}, mBackgroundHandler);
复制代码
ImageReader
是获取图像数据的重要途径,通过它可以获取到不同格式的图像数据,例如JPEG、YUV、RAW等。通过ImageReader.newInstance(int width, int height, int format, int maxImages)
创建ImageReader
对象,有4个参数:
ImageFormat.JPEG
,ImageFormat.YUV_420_888
等ImageReader其他相关的方法和回调:
try {
if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
throw new RuntimeException("Time out waiting to lock camera opening.");
}
cameraManager.openCamera(mCameraId, mStateCallback, mBackgroundHandler);
} catch (Exception e) {
e.printStackTrace();
}
复制代码
cameraManager.openCamera(@NonNull String cameraId,@NonNull final CameraDevice.StateCallback callback, @Nullable Handler handler)
的三个参数:
其中callback回调:
private final CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
@Override
public void onOpened(@NonNull CameraDevice camera) {
mCameraOpenCloseLock.release();
mCameraDevice = camera;
createCameraPreviewSession();
}
@Override
public void onDisconnected(@NonNull CameraDevice camera) {
mCameraOpenCloseLock.release();
camera.close();
mCameraDevice = null;
}
@Override
public void onError(@NonNull CameraDevice camera, int error) {
mCameraOpenCloseLock.release();
camera.close();
mCameraDevice = null;
}
@Override
public void onClosed(@NonNull CameraDevice camera) {
super.onClosed(camera);
}
};
复制代码
在CameraDevice.StateCallback的onOpened回调中执行:
private void createCameraPreviewSession() {
SurfaceTexture texture = mTextureView.getSurfaceTexture();
assert texture != null;
texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
Surface surface = new Surface(texture);
try {
mPreviewRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
mPreviewRequestBuilder.addTarget(surface);
// Here, we create a CameraCaptureSession for camera preview.
mCameraDevice.createCaptureSession(Arrays.asList(surface, mImageReader.getSurface()),
new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
// The camera is already closed
if (null == mCameraDevice) {
return;
}
// When the session is ready, we start displaying the preview.
mCaptureSession = cameraCaptureSession;
try {
// Auto focus should be continuous for camera preview.
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
// Flash is automatically enabled when necessary.
setAutoFlash(mPreviewRequestBuilder);
// Finally, we start displaying the camera preview.
mPreviewRequest = mPreviewRequestBuilder.build();
mCaptureSession.setRepeatingRequest(mPreviewRequest,
mCaptureCallback, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(
@NonNull CameraCaptureSession cameraCaptureSession) {
Toast.makeText(Camera2Activity.this, "configureFailed", Toast.LENGTH_SHORT).show();
}
}, null
);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
复制代码
这段的代码核心方法是mCameraDevice.createCaptureSession()
创建Capture会话,它接受了三个参数:
CaptureRequest是向CameraCaptureSession提交Capture请求时的信息载体,其内部包括了本次Capture的参数配置和接收图像数据的Surface。
mPreviewRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
mPreviewRequestBuilder.addTarget(surface);
复制代码
通过CameraDevice.createCaptureRequest()
创建CaptureRequest.Builder
对象,传入一个templateType参数,templateType用于指定使用何种模板创建CaptureRequest.Builder
对象,templateType的取值:
除了模式的配置,CaptureRequest还可以配置很多其他信息,例如图像格式、图像分辨率、传感器控制、闪光灯控制、3A(自动对焦-AF、自动曝光-AE和自动白平衡-AWB)控制等。在createCaptureSession的回调中可以进行设置
// Auto focus should be continuous for camera preview.
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
// Flash is automatically enabled when necessary.
setAutoFlash(mPreviewRequestBuilder);
// Finally, we start displaying the camera preview.
mPreviewRequest = mPreviewRequestBuilder.build();
复制代码
代码中设置了AF为设置未图片模式下的连续对焦,并设置自动闪光灯。最后通过build()
方法生成CaptureRequest对象。
Camera2中,通过连续重复的Capture实现预览功能,每次Capture会把预览画面显示到对应的Surface上。连续重复的Capture操作通过mCaptureSession.setRepeatingRequest(mPreviewRequest,mCaptureCallback, mBackgroundHandler)
实现,该方法有三个参数:
停止预览使用mCaptureSession.stopRepeating()
方法。
设置上面的request,session后,就可以真正的开始拍照操作
mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, mBackgroundHandler);
复制代码
该方法也有三个参数,和mCaptureSession.setRepeatingRequest一样:
这里设置了mCaptureCallback:
private CameraCaptureSession.CaptureCallback mCaptureCallback = new CameraCaptureSession.CaptureCallback() {
@Override
public void onCaptureProgressed(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull CaptureResult partialResult) {
process(partialResult);
}
@Override
public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
process(result);
}
private void process(CaptureResult result) {
switch (mState) {
case STATE_PREVIEW: {
// We have nothing to do when the camera preview is working normally.
break;
}
case STATE_WAITING_LOCK: {
Integer afState = result.get(CaptureResult.CONTROL_AF_STATE);
Log.d("DEBUG", "##### process STATE_WAITING_LOCK: " + afState);
if (afState == null) {
captureStillPicture();
} else if (CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED == afState ||
CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED == afState) {
// CONTROL_AE_STATE can be null on some devices
Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
if (aeState == null ||
aeState == CaptureResult.CONTROL_AE_STATE_CONVERGED) {
mState = STATE_PICTURE_TAKEN;
captureStillPicture();
} else {
runPrecaptureSequence();
}
}
break;
}
case STATE_WAITING_PRECAPTURE: {
// CONTROL_AE_STATE can be null on some devices
Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
if (aeState == null ||
aeState == CaptureResult.CONTROL_AE_STATE_PRECAPTURE ||
aeState == CaptureRequest.CONTROL_AE_STATE_FLASH_REQUIRED) {
mState = STATE_WAITING_NON_PRECAPTURE;
}
break;
}
case STATE_WAITING_NON_PRECAPTURE: {
// CONTROL_AE_STATE can be null on some devices
Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
if (aeState == null || aeState != CaptureResult.CONTROL_AE_STATE_PRECAPTURE) {
mState = STATE_PICTURE_TAKEN;
captureStillPicture();
}
break;
}
}
}
};
复制代码
通过设置mState
来区分当前状态,是在预览还是拍照
退到后台或者当前页面被关闭的时候,已经不需要使用相机了,需要进行相机关闭操作,释放资源,
private void closeCamera() {
try {
mCameraOpenCloseLock.acquire();
if (null != mCaptureSession) {
mCaptureSession.close();
mCaptureSession = null;
}
if (null != mCameraDevice) {
mCameraDevice.close();
mCameraDevice = null;
}
if (null != mImageReader) {
mImageReader.close();
mImageReader = null;
}
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted while trying to lock camera closing.", e);
} finally {
mCameraOpenCloseLock.release();
}
}
复制代码
先后对CaptureSession,CameraDevice,ImageReader进行close操作,释放资源。 这里仅仅对Camera2基本使用流程做了介绍,一些更高级的用法需要大家自行去实践。在Camera1中需要对画面进行方向矫正,而Camera2是否需要呢,关于相机Orientation相关的知识,通过后面的章节再进行介绍。
文章中涉及到的代码
分类:
Android
标签:
Android
作者:yeungeek
链接:https://juejin.cn/post/6844904062798790663
来源:稀土掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。