Camera2 是 Android L 的一个重大更新,重新定义了相机 API,也重构了相机 API 的架构,但使用起来,还是很复杂。官方 Demo 中仅仅实现了分辨率适配、预览图片和拍照三个功能,就花费了840多行,且待我细细道来。
一、框架
先说 Camera2 的框架,Camera2 将相机设备模拟成一个管道,应用向设备发送请求,设备返回结果,应用处理结果。
CameraManager:设备相机管理类,通过 getSystemService(Context.CAMERA_SERVICE) 获得。
CameraDevices:用于创建请求、会话等。有一组性能参数,用于描述硬件设备、设置和输出,参数信息表述的类是 CameraCharacteristics,获取方法为 CameraManager#getCameraCharacteristics(String)
二、步骤
1.创建相机拍摄会话(camera capture session),和一组输出图层(Surface)。方法为 createCaptureSession(List, CameraCaptureSession.StateCallback, Handler) 。
2.每个图层都需要合适的大小和格式(appropriate size and format),利用这些参数来对相机传回的图片进行处理,一个目标图层类并不单一,可以是 SurfaceView, SurfaceTexture
via Surface(SurfaceTexture), MediaCodec, MediaRecorder, Allocation, 和 ImageReader。
3.一般而言,相机的预览图片发送至 SurfaceView或 TextureView(via its SurfaceTexture) ,若要获取 JPEG 的图片或者是用于 DngCreator 的原始数据,都可以通过 ImageReader 来完成,只需设置格式为 JPEG
和 RAW_SENSOR。其他格式请自行查阅 ImageFormat,只是个设置参数解码的过程。
4.需要构建CaptureRequest,它定义了用于拍摄的所有参数,包括聚焦、闪光灯、曝光率等等一切拍照可能需要的或者相机设备支持的参数。还会列出哪一个图层是目的输出图层。有一个工厂方法request builder来构建它。
5.CaptureRequest 构建成功后,就可以激活拍摄会话。无论是用于一次性的拍摄还是不断地重复请求。两种请求的队列不同,但重复请求的优先级要低些,所以两个队列中都有请求存在时,总是会优先响应一次拍摄的请求。
6.响应请求之后,相机设备会生成一个类名为 TotalCaptureResult 的对象,这个虚席包含了相机设备拍摄时的参数和最终的状态结果。它的参数设置并不总是和请求相同,因为可能有些功能设备不支持。相机设备也会对每个请求中包含的图层发送图片信息,图片信息和之前的拍摄结果信息不是同步的,实际上会晚那么一点。
三、代码分析
现在来分析一下官方 Demo 的代码结构.
@Override
public void onResume() {
super.onResume();
startBackgroundThread();
// When the screen is turned off and turned back on, the SurfaceTexture is already
// available, and "onSurfaceTextureAvailable" will not be called. In that case, we can open
// a camera and start preview from here (otherwise, we wait until the surface is ready in
// the SurfaceTextureListener.
if (mTextureView.isAvailable()) {
openCamera(mTextureView.getWidth(), mTextureView.getHeight());
} else {
mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
}
}
首先,为相机开启了一个后台线程,这个进程用于后台执行相关的工作,这些工作我们看不到,是 native 的代码。还会用这个进程的 Looper 构建一个 Handler 用于回调,在 createCaptureSession(List, CameraCaptureSession.StateCallback, Handler) 中最后的参数便是这个。这里是开启后台进程的代码:
/**
* Starts a background thread and its {@link Handler}.
*/
private void startBackgroundThread() {
mBackgroundThread = new HandlerThread("CameraBackground");
mBackgroundThread.start();
mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
}
对应有关闭后台线程的方法,在 onPause() 时关闭后台线程,保证线程安全:
/**
* Starts a background thread and its {@link Handler}.
*/
private void startBackgroundThread() {
mBackgroundThread = new HandlerThread("CameraBackground");
mBackgroundThread.start();
mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
}
其次,当图层可用时开启相机。这里用了两个分支,从注释中可知,在关闭屏幕时,图层视图并不会立即关闭自己,接着开启屏幕,图层直接就处于已就绪状态,也不会触发状态改变的回调函数,依此做了分支。
打开相机:
/**
* Opens the camera specified by {@link #mCameraId}.
*
* @param width
* @param height
*/
private void openCamera(int width, int height) {
setUpCameraOutputs(width, height);
configureTransform(width, height);
Activity activity = getActivity();
if (activity == null) {
return;
}
CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
try {
// Check if permission granted.
if (ContextCompat.checkSelfPermission(activity,
Manifest.permission.CAMERA) !=PackageManager.PERMISSION_GRANTED) {
// Show an explanation.
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.CAMERA},PERMISSION_REQUEST_CAMERA);
return;
}
if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
throw new RuntimeException("Time out waiting to lock camera opening.");
}
manager.openCamera(mCameraId, mStateCallback, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted while trying to lock camera opening.", e);
}
}
设置相机输出,setUpCameraOutputs(width, height),包括对相机设备的选择,ImageReader的初始化和参数、回调设置。设置显示的转化矩阵,即将预览的图片调整至显示图层的大小。打开相机设备,manager.openCamera(mCameraId, mStateCallback, mBackgroundHandler); 可以理解为第一个方法是对 mCameraId 的初始化,和对展示图层的初始化,还有编码器ImageReader的初始化。步骤二并不是必须的,是为了让显示图层的显示效果更好而设置的。步骤 3 中还添加了对 Android6.0 的权限适配。还有这句话:mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)这个对象是信号量,在开关相机时需要对相机加锁,因为相机可能同时被几个应用或者进程访问,此时应当加锁。
三个步骤依次探究,首先是
setUpCameraOutputs(int width, int height)
/**
* Sets up member variables related to camera.
*
* @param width The width of available size for camera preview
* @param height The height of available size for camera preview.
*/
private void setUpCameraOutputs(int width, int height) {
Activity activity = getActivity();
CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
try {
for (String cameraId : manager.getCameraIdList()) {
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
// We don't use a front facing camera in this sample.
if (characteristics.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_FRONT) {
continue;
}
StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
// For still image captures, we use the largest available size;
Size largest = Collections.max(Arrays.asList(map.getOutputSizes(ImageFormat.JPEG)), new CompareSizeByArea());
mImageReader = ImageReader.newInstance(largest.getWidth(), largest.getHeight(), ImageFormat.JPEG, /*maxImages*/ 2);
mImageReader.setOnImageAvailableListener( mOnImageAvailableListener, mBackgroundHandler);
// Danger, W.R.! Attempting to use too large a preview size could exceed the camera
// bus' bandwidth limitation, resulting in gorgeous preview but the storage of
// garbage capture data.
mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class),width, height, largest);
// We fit the aspect ratio of TextureView to the size of preview we picked.
int orientation = getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
mTextureView.setAspectRatio(mPreviewSize.getWidth(), mPreviewSize.getHeight());
} else {
mTextureView.setAspectRatio(mPreviewSize.getHeight(), mPreviewSize.getWidth());
}
mCameraId = cameraId;
return;
}
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
这里一共为四个对象经行了操作,mImageReader,mPreviewSize,mTextureView,和mCameraId,剩余的代码是对屏幕的方向、分辨率之类的一些获取和适配。
configureTransform(int width, int height)
/**
* Configures the necessary {@link android.graphics.Matrix} transformation to
* {@link #mTextureView}.
* This method should be called after the camera preview size is determined in
* {@link #setUpCameraOutputs(int, int)} and also the size of {@link #mTextureView} is fixed.
*
* @param width the width of {@link #mTextureView}
* @param height the height of {@link #mTextureView}
*/
private void configureTransform(int width, int height) {
Activity activity = getActivity();
if (mTextureView == null || mPreviewSize == null || activity == null) {
return;
}
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
Matrix matrix = new Matrix();
RectF viewRect = new RectF(0, 0, width, height);
RectF bufferRect = new RectF(0, 0, mPreviewSize.getHeight(), mPreviewSize.getWidth());
float centerX = viewRect.centerX();
float centerY = viewRect.centerY();
if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY());
matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL);
float scala = Math.max((float) height / mPreviewSize.getHeight(), (float) width / mPreviewSize.getWidth());
matrix.postScale(scala, scala, centerX, centerY);
matrix.postRotate(90 * (rotation - 2), centerX, centerY);
} else if (Surface.ROTATION_180 == rotation) {
matrix.postRotate(180, centerX, centerY);
}
mTextureView.setTransform(matrix);
}
这里对预览图片的大小和方向做了适配,利用矩阵转化来对图片进行处理。
CameraDevice.StateCallback mStateCallback:
/**
* {@link CameraDevice.StateCallback} is called when {@link CameraDevice} changes its state.
*/
private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
@Override
public void onOpened(CameraDevice camera) {
// This method is called when the camera is opened. We start camera preview here.
mCameraOpenCloseLock.release();
mCameraDevice = camera;
createCameraPreviewSession();
}
@Override
public void onDisconnected(CameraDevice camera) {
mCameraOpenCloseLock.release();
camera.close();
mCameraDevice = null;
}
@Override
public void onError(CameraDevice camera, int error) {
mCameraOpenCloseLock.release();
camera.close();
mCameraDevice = null;
Activity activity = getActivity();
if (null != activity) {
activity.finish();
}
}
};
这是在开启设备相机时使用的状态变化回调函数。当然的,在状态变化之后,需要关闭相机锁,之后根据当前相机设备的状态经行接下来的操作。若相机打开成功,那么开始预览。这个功能并不是必须的,有“一次性的拍摄”也有“重复持续的拍摄”,这里预览属于重复的拍摄,维持一个拍摄会话,持续发送预览请求。
createCameraPreviewSession()
/**
* Creates a new {@link CameraCaptureSession} for camera preview.
*/
private void createCameraPreviewSession() {
try {
SurfaceTexture texture = mTextureView.getSurfaceTexture();
assert texture != null;
// We configure the size of default buffer to be the size of camera preview we want.
texture.setDefaultBufferSize(mPreviewSize.getWidth(),mPreviewSize.getHeight());
// This is the output Surface we need to start preview.
Surface surface = new Surface(texture);
// We set up a CaptureRequest.Builder with the output Surface.
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(CameraCaptureSession session) {
// The camera is already closed
if (null == mCameraDevice) {
return;
}
// When the session is ready, we start displaying the preview.
mCaptureSession = session;
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.
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
// Finally, we start displaying the camera preview.
mPreviewRequest = mPreviewRequestBuilder.build();
mCaptureSession.setRepeatingRequest(mPreviewRequest,
mCaptureCallback, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(CameraCaptureSession session) {
showToast("Failed");
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
核心方法是
void createCaptureSession(@NonNull List
这个方法需要一组对应的 Surface 参数来接受输出内容,同时也需要对 Surface 的分辨率进行适配,同时还需要一个拍摄会话状态的回调参数。这个例子中,会在会话设置完成之后构造拍摄请求,由于是预览,所以选用了重复不间断的拍摄请求,即
mCaptureSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback, mBackgroundHandler);
这里对应的回调函数
private CameraCaptureSession.CaptureCallback mCaptureCallback
= new CameraCaptureSession.CaptureCallback() {
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);
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 = 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 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;
}
}
}
@Override
public void onCaptureProgressed(CameraCaptureSession session, CaptureRequest request,
CaptureResult partialResult) {
process(partialResult);
}
@Override
public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request,
TotalCaptureResult result) {
process(result);
}
};
这里人为帮其设定了一些状态值,初始状态为:
/**
* The current state of camera state for taking pictures.
*/
private int mState = STATE_PREVIEW;
此时在回调中什么都不做,但是当点击按钮或者别的操作触发了自定义状态的改变时,便会进入准备阶段或拍摄阶段,拍摄阶段如下:
/**
* Capture a still picture. This method should be called when we get a response in
* {@link #mCaptureCallback} from both {@link #lockFocus()}
*/
private void captureStillPicture() {
try {
final Activity activity = getActivity();
if (activity == null || mCameraDevice == null) {
return;
}
// This is the CaptureRequest.Builder that we use to take a picture.
final CaptureRequest.Builder captureBuilder =
mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
captureBuilder.addTarget(mImageReader.getSurface());
// Use the same AE and AF modes as the preview.
captureBuilder.set(CaptureRequest.CONTROL_AF_MODE,
CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
captureBuilder.set(CaptureRequest.CONTROL_AE_MODE,
CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
// Orientation
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, ORIENTATIONS.get(rotation));
CameraCaptureSession.CaptureCallback captureCallback
= new CameraCaptureSession.CaptureCallback() {
@Override
public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request,
TotalCaptureResult result) {
showToast("Saved: " + mFile);
unlockFocus();
}
};
mCaptureSession.stopRepeating();
mCaptureSession.capture(captureBuilder.build(), captureCallback, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
可以发现,拍摄方法的内容同开启预览的内容所差无几,只是少了展示图层的设置,因为此时已经设置过了,修改的地方仅仅是请求的内容变了。
拍摄单张照片也同理。
以上便是拍摄照片的一般流程。
四、总结
总的来讲,Camera2 的复杂并不在于其结构设计,而是在于本身的图形领域,包括对相机的控制以及对成像后图片的处理,这些都需要自己定制,文档又冗长,不了解图片处理就会话费很多时间。
Camera2 的优点在于用一种管道的方式对相机设备建立了模型,在使用时对结构、流程的理解会相对简单一点,但是通道会话的每个步骤都需要相对应的回调函数,在官方 Demo 中一共需要使用到四个,分别是TextureView.SurfaceTextureListener,CameraDevice.StateCallback,CameraCaptureSession.CaptureCallback,ImageReader.OnImageAvailableListener,Android 里上一处需要用到如此之多的回调函数还是在 Activity 和 Fragment 中,但是后两者有着明确的生命周期和生命周期管理,相机设备则不同,生命周期的概念并不明确,每一步都需要人为定制,细枝末节很多,也就加大了开发的难。