Camera 工作结构
- Camera Device:代表一个摄像头,在一部手机上有可能有多个摄像头,如前置摄像头,后置摄像头等。
- CameraCaptureSession:表式Android Device(APP)与CameraDevice之间的会话层,类似于 http中的 session。
- CaptureRequest:APP要想拍照,摄像都是向 CameraDevice 发送CaptureRequest请求。这个请求是通过上面的 Session发送出去的。
- CameraMetadata:CameraDevice将捕获到的数据封装成CameraMetadata通过Session传给 APP。其实这一步基本是隐藏起来的,在API中我们很少直接用它。
- Android Device:可以把它想象成我们自己的APP应用。
- Surface:存放数据的类,在实际的操作中,我们只要把 surface告诉CaptureRequest.Builder,当有数据到来之后,系统底层就会将数据存放到 surface中。
frameworks 实现预览拍照流程图
AndroidCamera2AgentImpl.java
CameraManager 对象初始化
private final CameraManager mCameraManager;
AndroidCamera2AgentImpl(Context context) {
...
mCameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
...
}
CameraDevice 对象初始化
protected CameraDevice mCamera;
mCameraManager.openCamera(mCameraId, mCameraDeviceStateCallback, this);
// This callback monitors our connection to and disconnection from camera devices.
private CameraDevice.StateCallback mCameraDeviceStateCallback =
new CameraDevice.StateCallback() {
@Override
public void onOpened(CameraDevice camera) {
...
mCamera = camera;
...
}
@Override
public void onDisconnected(CameraDevice camera) {
}
@Override
public void onError(CameraDevice camera, int error) {
}
};
CameraCaptureSession 对象初始化
protected CameraCaptureSession mSession;
mCamera.createCaptureSession(
Arrays.asList(mPreviewSurface,mCaptureReader.getSurface()),
mCameraPreviewStateCallback,this);
// This callback monitors our camera session (i.e. our transition into and out of preview).
protected CameraCaptureSession.StateCallback mCameraPreviewStateCallback=
new CameraCaptureSession.StateCallback(){
@Override
public void onConfigured(CameraCaptureSession session){
mSession=session;
...
}
@Override
public void onConfigureFailed(CameraCaptureSession session){
}
@Override
public void onActive(CameraCaptureSession session){
}
};
CameraRequest 对象初始化
mPersistentSettings.createRequest(mCamera,
CameraDevice.TEMPLATE_STILL_CAPTURE,
mCaptureReader.getSurface())
// Camera2RequestSettingsSet.java
public CaptureRequest createRequest(CameraDevice camera,int template,Surface...targets)
throws CameraAccessException{
...
Builder reqBuilder=camera.createCaptureRequest(template);
...
return reqBuilder.build();
}
CameraDevice.java
CameraDevice 常量解释
public abstract class CameraDevice implements AutoCloseable {
// 用于创建一个相机预览窗口的请求
public static final int TEMPLATE_PREVIEW = 1;
// 用于创建静态图像捕获的请求
public static final int TEMPLATE_STILL_CAPTURE = 2;
// 用于创建录像请求
public static final int TEMPLATE_RECORD = 3;
// 用于在录制视频时创建静态图像捕获的请求
public static final int TEMPLATE_VIDEO_SNAPSHOT = 4;
// 用于创建零延迟拍照请求
public static final int TEMPLATE_ZERO_SHUTTER_LAG = 5;
// 用于创建一个直接控制相机拍照参数的请求(需支持 MANUAL_SENSOR 功能)
public static final int TEMPLATE_MANUAL = 6;
}
CameraCaptureSession.java
CameraCaptureSession 方法介绍
/**
* <p>Submit a request for an image to be captured by the camera device.</p>
* 提交捕获图像请求,即拍照
*/
public abstract int capture(@NonNull CaptureRequest request,
@Nullable CaptureCallback listener, @Nullable Handler handler)
throws CameraAccessException;
/**
* Request endlessly repeating capture of images by this capture session.
*/
public abstract int captureBurst(@NonNull List<CaptureRequest> requests,
@Nullable CaptureCallback listener, @Nullable Handler handler)
throws CameraAccessException;
/**
* <p>Request endlessly repeating capture of a sequence of images by this
* capture session.</p>
*/
public abstract int setRepeatingBurst(@NonNull List<CaptureRequest> requests,
@Nullable CaptureCallback listener, @Nullable Handler handler)
throws CameraAccessException;
/**
* Request endlessly repeating capture of images by this capture session.
* 请求不断重复捕获图像,即实现预览
*/
public abstract int setRepeatingRequest(@NonNull CaptureRequest request,
@Nullable CaptureCallback listener, @Nullable Handler handler)
throws CameraAccessException;
/**
* <p>Cancel any ongoing repeating capture set by either
* 停止捕获图像,即停止预览
*/
public abstract void stopRepeating() throws CameraAccessException;
注意:
- capture() 和 setRepeatingRequest() 方法都是向相机设备发送获取图像的请求
- capture() 的功能是获取一次,一般用于拍照,图像会停在那里;setRepeatingRequest() 是不停的发出 capture 请求,不断重复的获取图像,一般用于预览或录制视频
- capture() 比 setRepeatingRequest() 的优先级高,当在 setRepeatingRequest() 时执行capture() ,会先处理capture(),然后继续处理 setRepeatingRequest()