从零开发一款相机APP 第八篇: Camera2相机 拍照功能实现

2022-12-20 19:16:45 浏览数 (1)

代码语言:javascript复制
本课程内容由 @公众号:小驰笔记出品,欢迎关注公众号,获取更多交流信息~

一、创建拍照capture request

代码语言:javascript复制
final CaptureRequest.Builder captureBuilder =
        mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);

captureBuilder.addTarget(mImageReader.getSurface());

二、 设置af模式

代码语言:javascript复制
captureBuilder.set(CaptureRequest.CONTROL_AF_MODE,
               CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);

三、设置jpeg方向

代码语言:javascript复制
captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(rotation));

四、拍照尺寸设置

代码语言:javascript复制
mImageReader = ImageReader.newInstance(largest.getWidth(), largest.getHeight(),
                        ImageFormat.JPEG, /*maxImages*/2);
						
mImageReader.setOnImageAvailableListener(
		mOnImageAvailableListener, mBackgroundHandler);

五、创建拍照会话

代码语言:javascript复制
 CameraCaptureSession.CaptureCallback CaptureCallback
                    = new CameraCaptureSession.CaptureCallback() {

                @Override
                public void onCaptureCompleted(@NonNull CameraCaptureSession session,
                                               @NonNull CaptureRequest request,
                                               @NonNull TotalCaptureResult result) {

                    unlockFocus();
                }
};

mPreviewCaptureSession.stopRepeating();
mPreviewCaptureSession.abortCaptures();
mPreviewCaptureSession.capture(captureBuilder.build(), CaptureCallback, null);

六、保存拍照照片

代码语言:javascript复制
private final ImageReader.OnImageAvailableListener mOnImageAvailableListener
            = new ImageReader.OnImageAvailableListener() {
        @Override
        public void onImageAvailable(ImageReader reader) {
            mBackgroundHandler.post(new ImageSaver(reader.acquireLatestImage(), mFile));
        }
};

深圳上班,

从事Android Camera相关软件开发工作,

公众号记录生活和工作的点滴,

0 人点赞