public abstract class CameraDevice
extends Object
implements AutoCloseable
The CameraDevice class is a representation of a single camera connected to an Android device, allowing for fine-grain control of image capture and post-processing at high frame rates.
Your application must declare the
Camera
permission in its manifest in order to access camera devices.
内部类
CameraDevice . StateCallback
A callback objects for receiving updates about the state of a camera device
private int mCameraId = CameraCharacteristics.LENS_FACING_FRONT;
private CameraManager mCameraManager; // 相机管理者
private CameraDevice mCameraDevice; // 相机对象
private Handler mBackgroundHandler;
private HandlerThread mBackgroundThread;
private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
@Override
public void onOpened(@NonNull CameraDevice camera) {
mCameraDevice = camera; // 获取到可用的 CameraDevice 实例
// 当相机成功打开时回调该方法,接下来可以执行创建预览的操作
}
@Override
public void onDisconnected(@NonNull CameraDevice camera) {
// 当相机断开连接时回调该方法,应该在此执行释放相机的操作
}
@Override
public void onError(@NonNull CameraDevice camera, int error) {
// 当相机打开失败时,应该在此执行释放相机的操作
}
@Override
public void onClosed(@NonNull CameraDevice camera) {
// 当相机关闭时回调该方法,这个方法可以不用实现
}
};
public void openCamera() {
try {
// 前处理
mCameraManager.openCamera(Integer.toString(mCameraId), mStateCallback, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
Public Methods(常用方法)
- void close()
Close the connection to the camera device as quickly as possible,once the device has fully shut down, the StateCallback#onClosed callback will be called, and the camera is free to be re-opened
- CaptureRequest.Builder createCaptureRequest(int templateType)
Create a
CaptureRequest.Builder
for new capture requests, initialized with template for a target use case. This methods allows clients to pass physical camera ids which can be used to customize the request for a specific physical camera.Parameters:
templateType -- int: An enumeration selecting the use case for this request. physicalCameraIdSet -- set : A set of physical camera ids that can be used to customize the request for a specific physical camera
Returns:
CaptureRequest . Builder -- a builder for a capture request, initialized with default settings for that template, and no output streams This value cannot be
null
.
- void createCaptureSession(List outputs, CameraCaptureSession.StateCallback, Handler handler)
使用一个指定的Surface输出列表创建一个相机捕捉回话
outputs : 输出的 Surface 集合,每个 CaptureRequest 的输出 Surface 都应该是 outputs 的一个子元素。
callback : 创建会话的回调。成功时将调用 CameraCaptureSession.StateCallback 的 onConfigured(CameraCaptureSession session) 方法。
handler : 指定回调执行的线程,传 null 时默认使用当前线程的 Looper。