文章目录
一、概况
1.现在Google已经开始推广CameraX API了,为什么我们还要学习Camera API2?
从CameraX源码可以知道CameraX的使用非常简单,原因在于它把Camera2中复杂的API封装到统一的config中,供开发者调用,如果我们想进一步深入理解Android Camera子系统的原理还是需要从Camera2一步步深入。
参考链接:
Android Camera-Camera2使用
Android Camera 编程从入门到精通
Android Camera2拍照流程
2.Camera API2 学习代码参考有哪些?
Google 关于Camera API2使用提供的开源库,因为Google推广CameraX的缘故已经迁移到Camera2Basic,我们基于此代码进行Camera API2的研究。
二、使用流程
1.获取CameraManager
CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
2.获取相机信息
private fun setUpCameraOutputs(width: Int, height: Int) {
val manager = activity.getSystemService(Context.CAMERA_SERVICE) as CameraManager
try {
for (cameraId in manager.cameraIdList) {
val characteristics = manager.getCameraCharacteristics(cameraId)
// We don't use a front facing camera in this sample.
val cameraDirection = characteristics.get(CameraCharacteristics.LENS_FACING)
if (cameraDirection != null &&
cameraDirection == CameraCharacteristics.LENS_FACING_FRONT) {
continue
}
......
}
这里默认选择前置摄像头,并获取相关相机信息。
3.初始化ImageReader
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个参数:
width:图像数据的宽度
height:图像数据的高度
format:图像数据的格式,例如ImageFormat.JPEG,ImageFormat.YUV_420_888等
maxImages:最大Image个数,Image对象池的大小,指定了能从ImageReader获取Image对象的最大值,过多获取缓冲区可能导致OOM,所以最好按照最少的需要去设置这个值
ImageReader其他相关的方法和回调:
ImageReader.OnImageAvailableListener:有新图像数据的回调
acquireLatestImage():从ImageReader的队列里面,获取最新的Image,删除旧的,如果没有可用的Image,返回null
acquireNextImage():获取下一个最新的可用Image,没有则返回null
close():释放与此ImageReader关联的所有资源
getSurface():获取为当前ImageReader生成Image的Surface
4.打开相机
private fun openCamera(width: Int, height: Int) {
val permission = ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA)
if (permission != PackageManager.PERMISSION_GRANTED) {
requestCameraPermission()
return
}
setUpCameraOutputs(width, height)
configureTransform(width, height)
val manager = activity.getSystemService(Context.CAMERA_SERVICE) as CameraManager
try {
// Wait for camera to open - 2.5 seconds is sufficient
if (!cameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
throw RuntimeException("Time out waiting to lock camera opening.")
}
manager.openCamera(cameraId, stateCallback, backgroundHandler)
} catch (e: CameraAccessException) {
Log.e(TAG, e.toString())
} catch (e: InterruptedException) {
throw RuntimeException("Interrupted while trying to lock camera opening.", e)
}
}
cameraManager.openCamera(@NonNull String cameraId,@NonNull final CameraDevice.StateCallback callback, @Nullable Handler handler)的三个参数:
cameraId:摄像头的唯一标识
callback:设备连接状态变化的回调
handler:回调执行的Handler对象,传入null则使用当前的主线程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);
}
};
onOpened:表示相机打开成功,可以真正开始使用相机,创建Capture会话
onDisconnected:当相机断开连接时回调该方法,需要进行释放相机的操作
onError:当相机打开失败时,需要进行释放相机的操作
onClosed:调用Camera.close()后的回调方法
5.创建CameraCaptureSession
在CameraDevice.StateCallback的onOpened回调中执行:
/**
* Creates a new [CameraCaptureSession] for camera preview.
*/
private fun createCameraPreviewSession() {
try {
val texture = textureView.surfaceTexture
// We configure the size of default buffer to be the size of camera preview we want.
texture.setDefaultBufferSize(previewSize.width, previewSize.height)
// This is the output Surface we need to start preview.
val surface = Surface(texture)
// We set up a CaptureRequest.Builder with the output Surface.
previewRequestBuilder = cameraDevice!!.createCaptureRequest(
CameraDevice.TEMPLATE_PREVIEW
)
previewRequestBuilder.addTarget(surface)
// Here, we create a CameraCaptureSession for camera preview.
cameraDevice?.createCaptureSession(Arrays.asList(surface, imageReader?.surface),
object : CameraCaptureSession.StateCallback() {
override fun onConfigured(cameraCaptureSession: CameraCaptureSession) {
// The camera is already closed
if (cameraDevice == null) return
// When the session is ready, we start displaying the preview.
captureSession = cameraCaptureSession
try {
// Auto focus should be continuous for camera preview.
previewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE)
// Flash is automatically enabled when necessary.
setAutoFlash(previewRequestBuilder)
// Finally, we start displaying the camera preview.
previewRequest = previewRequestBuilder.build()
captureSession?.setRepeatingRequest(previewRequest,
captureCallback, backgroundHandler)
} catch (e: CameraAccessException) {
Log.e(TAG, e.toString())
}
}
override fun onConfigureFailed(session: CameraCaptureSession) {
activity.showToast("Failed")
}
}, null)
} catch (e: CameraAccessException) {
Log.e(TAG, e.toString())
}
}
这段的代码核心方法是mCameraDevice.createCaptureSession()创建Capture会话,它接受了三个参数:
outputs:用于接受图像数据的surface集合,这里传入的是一个preview的surface
callback:用于监听 Session 状态的CameraCaptureSession.StateCallback对象
handler:用于执行CameraCaptureSession.StateCallback的Handler对象,传入null则使用当前的主线程Handler
6.创建CaptureRequest
CaptureRequest是向CameraCaptureSession提交Capture请求时的信息载体,其内部包括了本次Capture的参数配置和接收图像数据的Surface。
mPreviewRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
mPreviewRequestBuilder.addTarget(surface);
通过CameraDevice.createCaptureRequest()创建CaptureRequest.Builder对象,传入一个templateType参数,templateType用于指定使用何种模板创建CaptureRequest.Builder对象,templateType的取值:
TEMPLATE_PREVIEW:预览模式
TEMPLATE_STILL_CAPTURE:拍照模式
TEMPLATE_RECORD:视频录制模式
TEMPLATE_VIDEO_SNAPSHOT:视频截图模式
TEMPLATE_MANUAL:手动配置参数模式
除了模式的配置,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();
7.预览
Camera2中,通过连续重复的Capture实现预览功能,每次Capture会把预览画面显示到对应的Surface上。连续重复的Capture操作通过mCaptureSession.setRepeatingRequest(mPreviewRequest,mCaptureCallback, mBackgroundHandler)实现,该方法有三个参数:
request:CaptureRequest对象
listener:监听Capture 状态的回调
handler:用于执行CameraCaptureSession.CaptureCallback的Handler对象,传入null则使用当前的主线程Handler
停止预览使用mCaptureSession.stopRepeating()方法。
8.拍照
android-Camera2Basic\Application\src\main\java\com\example\android\camera2basic
设置上面的request,session后,就可以真正的开始拍照操作
通过上面三个步骤已经可以在屏幕上显示预览了,现在开始做拍照操作,从第一部分我们可以知道拍照也是要通过向CameraCaptureSession发送一个CaptureRequest来实现。
CameraCaptureSession.CaptureCallback CaptureCallback
= new CameraCaptureSession.CaptureCallback() {
@Override
public void onCaptureCompleted(@NonNull CameraCaptureSession session,
@NonNull CaptureRequest request,
@NonNull TotalCaptureResult result) {
showToast("Saved: " + mFile);
Log.d(TAG, mFile.toString());
unlockFocus();
}
};
mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, mBackgroundHandler);
该方法也有三个参数,和mCaptureSession.setRepeatingRequest一样:
request:CaptureRequest对象
listener:监听Capture 状态的回调
handler:用于执行CameraCaptureSession.CaptureCallback的Handler对象,传入null则使用当前的主线程Handler
这里设置了mCaptureCallback:
通过设置mState来区分当前状态,是在预览还是拍照
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;
}
}
}
};
9.关闭相机
相机关闭操作,释放资源,先后对CaptureSession,CameraDevice,ImageReader进行close操作,释放资源。
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();
}
}