AndroidCamera2官⽅demo的学习
⼀.介绍:
Camera2 是 Android L 的⼀个重⼤更新,重新定义了相机 API,也重构了相机 API 的架构,但使⽤起来,还是很复杂。 官⽅demo地址:
⼆.流程:
下⾯根据官⽅demo来详细了解下Camera2的拍照流程
1.设置预览图的尺⼨
mPreviewSize = OutputSizes(SurfaceTexture.class),
rotatedPreviewWidth, rotatedPreviewHeight, maxPreviewWidth,
maxPreviewHeight, largest);
2.创建⼀个⽤于处理拍照图⽚处理的ImageReder
mImageReader = Width(), Height(),
ImageFormat.JPEG, /*maxImages*/2);
mImageReader.setOnImageAvailableListener(
mOnImageAvailableListener, mBackgroundHandler);
这⾥会为ImageReder设置⼀个OnImageAvailableListener的,⼀旦ImageReder获取到相机设备传过来的图像,就会回调该的onImageAvailable⽅法,在onImageAvailable⽅法的ImageReader参数就携带了拍照所得的图像
private final ImageReader.OnImageAvailableListener mOnImageAvailableListener
= new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader reader) {
mBackgroundHandler.post(new ImageSaver(reader.acquireNextImage(), mFile));
}
};
3.获取CamerManager对象,并打开相机设备
CameraManager manager = (CameraManager) SystemService(Context.CAMERA_SERVICE);
try {
//这⾥把相机加了⼀把锁,因为可能会有多个应⽤⽤到相机设备,在指定时间内,没有拿到许可就会抛异常
if (!Acquire(2500, TimeUnit.MILLISECONDS)) {
throw new RuntimeException("Time out waiting to lock camera opening.");
}
//这⾥回传⼊⼀个CameraDevice.StateCallback对象mStateCallback,这个就是拿到相机设备成功失败的回调
manager.openCamera(mCameraId, mStateCallback, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted while trying to lock camera opening.", e);
}
4.获取到相机设备,创建⽤于显⽰预览图⽚的Surface和⼀个适合相机预览窗⼝的请求
SurfaceTexture texture = SurfaceTexture();
assert texture != null;
// We configure the size of default buffer to be the size of camera preview we want.
texture.Width(), Height());
/
/ 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
= ateCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
mPreviewRequestBuilder.addTarget(surface);
5.创建⼀个CameraCaptureSession
/**
第⼀个参数List<Surface>,surface⽤于显⽰预览图的,就是第四步中创建的那个Surface()使⽤来处理拍照返回的图⽚的Surface,第⼆个参数callback, CameraCaptureSession.StateCallback对象,创建session成功与否的回调
第三个参数,Handler指定回调的线程,null表⽰当前线程
*/
, null
);
6.在 CameraCaptureSession.StateCallback接收到CameraCaptureSession并发送预览请求,
new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
// The camera is already closed
if (null == mCameraDevice) {
return;
}
// When the session is ready, we start displaying the preview.
mCaptureSession = cameraCaptureSession;
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.
setAutoFlash(mPreviewRequestBuilder);
// Finally, we start displaying the camera preview.
mPreviewRequest = mPreviewRequestBuilder.build();
/
/ 通过此捕获会话⽆休⽌地重复捕获图像,即预览请求 mCaptureSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(
@NonNull CameraCaptureSession cameraCaptureSession) {
showToast("Failed");
}
}
到此,我们的TextureView就可以显⽰预览的图像了
7.拍照
try {
// This is how to tell the camera to lock focus,⾃动对焦
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
CameraMetadata.CONTROL_AF_TRIGGER_START);
// Tell #mCaptureCallback to wait for the lock.
mState = STATE_WAITING_LOCK;
//提交要由相机设备捕获的图像的请求 mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback,
mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
在mCaptureCallback中,调⽤captureStillPicture()
private void captureStillPicture() {
try {
final Activity activity = getActivity();
if (null == activity || null == mCameraDevice) {
return;
}
android学习教程// This is the CaptureRequest.Builder that we use to take a picture.
final CaptureRequest.Builder captureBuilder =
//将mImageReader的surface添加到此请求的⽬标列表中,这时拍照所返回的图⽚就会交由mImageReader的mOnImageAvailableListener处理
captureBuilder.Surface());
// Use the same AE and AF modes as the preview.
captureBuilder.set(CaptureRequest.CONTROL_AF_MODE,
CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
setAutoFlash(captureBuilder);
// Orientation
int rotation = WindowManager().getDefaultDisplay().getRotation();
captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(rotation));
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, String());
unlockFocus();
}
};
mCaptureSession.stopRepeating();
mCaptureSession.abortCaptures();
mCaptureSession.capture(captureBuilder.build(), CaptureCallback, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
⾄此,拍照也已完成
三.总结
这仅仅是对⾕歌Camera2官⽅Demo的⼀个简单学习,关于Camera2中的各种操作还有许多学习的地⽅,包括美颜,磨⽪各种模式的实现
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论