从5.0开始(API Level 21),可以完全控制安卓设备相机的新api Camera2(android.hardware.Camera2)被引入了进来。在以前的Camera api(android.hardware.Camera)中,对相机的手动控制需要更改系统才能实现,而且api也不友好。不过老的Camera API在5.0上已经过时,如今Android推荐使用Camera2采集视频,借着写这篇记录的过程,熟悉和理解Camera2流程。
专业性名词
YUV
一种颜色编码的方法,在旧Camera API 常用的是NV21和YV12,可以转成RGB编码。
//获取Camera2 支持的颜色编码
StreamConfigurationMap map = characteristics.get(
CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
map.getOutputFormats();
CameraManager
Camera2中负责管理、查询摄像头信息、打开可用的摄像头;
- 可以通过调用Context.getSystemService(java.lang.String)方法来获取一个CameraManager的实例;
- cameraId 通过 getCameraIdList() 枚举得到,代表选择使用哪个摄像头;
- 设备信息通过 CameraCharacteristics getCameraCharacteristics(String cameraId) 可拿到;
- 打开摄像头 openCamera(String cameraId, CameraManager.StateCallback callback, Handler handler),StateCallback是接收设备状态的更新的回调,比如后面的cameraDevice就是通过stateCallback的onOpen()回调中拿到,Handler 表示打开摄像头的工作在具体哪个handler的looper中,也就是在哪个线程中执行,若为null则在当前线程;
CameraDevice
具体的摄像头,提供一组属性信息,描述硬件设备以及设备的可用设置和参数。
- CameraDevice是在CameraManager打开摄像头后,通过CameraDevice.StateCallback的回调中拿到的,是个异步的过程;
- createCaptureRequest() 创建CaptureRequest.Builder,CaptureRequest.Builder负责创建各种捕获图像的请求 CaptureRequest;
- createCaptureSession() 负责创建捕获图像的会话
CameraCaptureSession;
CaptureRequest
一次捕获请求,通过CaptureRequest.Builder的build()创建,其实请求参数也是通过Buider来设置:
CaptureRequest.Builder常用的方法:
- addTarget(Surface outputTarget) 将surface添加到输出列表中,才可以显示在SurfaceView、TextureView或者输出到ImageReader中;
- set(Key< T> key, T value) 设置其他属性;
CameraCaptureSession
捕获的会话Session,预览、拍照,都由该它进行控制的。
- cameraCaptureSession是通过CameraDevice的 createCaptureSession(List< Surface>, CameraCaptureSession.StateCallback, Handler) 创建;
- 拍照 capture(CaptureRequest, CameraCaptureSession.CaptureCallback, Handler);
- 预览 setRepeatingRequest(CaptureRequest, CameraCaptureSession.CaptureCallback, Handler);
camera结构
CameraManager结构在写代码的时候发现上面几个callback弄不清楚
- openCamera中的CameraManager.StateCallback 是 camra 创建过程中状态回调;
- createCaptureSession中的CameraCaptureSession.StateCallback 是session创建过程中的状态回调;
- capture 或 setRepeatingRequest的CameraCaptureSession.CaptureCallback 是在预览或者拍照request请求之后的回调;
流程
初始化执行camera动作的线程和handler
private void startCameraThread() {
mCameraThread = new HandlerThread("CameraThread");
mCameraThread.start();
mCameraHandler = new Handler(mCameraThread.getLooper());
}
设置摄像头属性
private void setupCamera(int width, int height) {
//获取摄像头的管理者CameraManager
CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
//遍历所有摄像头
for (String cameraId : manager.getCameraIdList()) {
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
//此处默认打开后置摄像头
if (facing != null && facing == CameraCharacteristics.LENS_FACING_FRONT)
continue;
//获取StreamConfigurationMap,它是管理摄像头支持的所有输出格式和尺寸
StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
assert map != null;
//根据TextureView的尺寸设置预览尺寸
mPreviewSize = getOptimalSize(map.getOutputSizes(SurfaceTexture.class), width, height);
mCameraId = cameraId;
break;
}
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
//选择sizeMap中大于并且最接近width和height的size
private Size getOptimalSize(Size[] sizeMap, int width, int height) {
List<Size> sizeList = new ArrayList<>();
for (Size option : sizeMap) {
if (width > height) {
if (option.getWidth() > width && option.getHeight() > height) {
sizeList.add(option);
}
} else {
if (option.getWidth() > height && option.getHeight() > width) {
sizeList.add(option);
}
}
}
if (sizeList.size() > 0) {
return Collections.min(sizeList, new Comparator<Size>() {
@Override
public int compare(Size lhs, Size rhs) {
return Long.signum(lhs.getWidth() * lhs.getHeight() - rhs.getWidth() * rhs.getHeight());
}
});
}
return sizeMap[0];
}
打开摄像头
private void openCamera() {
CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
return;
}
manager.openCamera(mCameraId, mStateCallback, mCameraHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
开始预览
在mStateCallback中预览。
TextureView预览
private void startPreview() {
SurfaceTexture mSurfaceTexture = mTextureView.getSurfaceTexture();
mSurfaceTexture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
Surface previewSurface = new Surface(mSurfaceTexture);
try {
mCaptureRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
mCaptureRequestBuilder.addTarget(previewSurface);
mCameraDevice.createCaptureSession(Arrays.asList(previewSurface, mImageReader.getSurface()), new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(CameraCaptureSession session) {
try {
mCaptureRequest = mCaptureRequestBuilder.build();
mCameraCaptureSession = session;
mCameraCaptureSession.setRepeatingRequest(mCaptureRequest, null, mCameraHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(CameraCaptureSession session) {
}
}, mCameraHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
SurfaceView预览
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void startPreView(){
try {
Surface surface = mSurfaceHolder.getSurface();
mCaptureRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
if (surface!=null){
Log.d(TAG,"SURFACE不为空");
mCaptureRequestBuilder.addTarget(surface);
}else {
Log.d(TAG,"SURFACE为空");
}
mCameraDevice.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession session) {
mCaptureRequest = mCaptureRequestBuilder.build();
mCameraCaptureSession = session;
try {
mCameraCaptureSession.setRepeatingRequest(mCaptureRequest,null,mCameraHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(@NonNull CameraCaptureSession session) {
}
},mCameraHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
停止预览
@Override
protected void onPause() {
super.onPause();
if (mCameraCaptureSession != null) {
mCameraCaptureSession.close();
mCameraCaptureSession = null;
}
if (mCameraDevice != null) {
mCameraDevice.close();
mCameraDevice = null;
}
}
网友评论