Android Camera2官方demo的学习

时间:2024-04-08 10:31:04

一.介绍:

Camera2 是 Android L 的一个重大更新,重新定义了相机 API,也重构了相机 API 的架构,但使用起来,还是很复杂。 官方demo地址:Camera2

二.流程:

Android Camera2官方demo的学习
下面根据官方demo来详细了解下Camera2的拍照流程

1.设置预览图的尺寸
   mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class),
                        rotatedPreviewWidth, rotatedPreviewHeight, maxPreviewWidth,
                        maxPreviewHeight, largest);
2.创建一个用于处理拍照图片处理的ImageReder
   mImageReader = ImageReader.newInstance(largest.getWidth(), largest.getHeight(),
                        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) activity.getSystemService(Context.CAMERA_SERVICE);
        try {
        //这里把相机加了一把锁,因为可能会有多个应用用到相机设备,在指定时间内,没有拿到许可就会抛异常
            if (!mCameraOpenCloseLock.tryAcquire(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 = 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);
5.创建一个CameraCaptureSession
/**
第一个参数List<Surface>,surface用于显示预览图的,就是第四步中创建的那个Surface,mImageReader.getSurface()使用来处理拍照返回的图片的Surface,

第二个参数callback, CameraCaptureSession.StateCallback对象,创建session成功与否的回调

第三个参数,Handler指定回调的线程,null表示当前线程
*/
mCameraDevice.createCaptureSession(Arrays.asList(surface, mImageReader.getSurface()),callback, mImageReader.getSurface()),
                  , 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;
            }
            // This is the CaptureRequest.Builder that we use to take a picture.
            final CaptureRequest.Builder captureBuilder =
                    mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);

//将mImageReader的surface添加到此请求的目标列表中,这时拍照所返回的图片就会交由mImageReader的mOnImageAvailableListener处理
            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);
            setAutoFlash(captureBuilder);

            // Orientation
            int rotation = activity.getWindowManager().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, mFile.toString());
                    unlockFocus();
                }
            };

            mCaptureSession.stopRepeating();
            mCaptureSession.abortCaptures();
            mCaptureSession.capture(captureBuilder.build(), CaptureCallback, null);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }

至此,拍照也已完成

三.总结

这仅仅是对谷歌Camera2官方Demo的一个简单学习,关于Camera2中的各种操作还有许多学习的地方,包括美颜,磨皮各种模式的实现