Android开发中amera2|Android开发中amera2 Preview使用详解

目录

  • 前言
  • 一、Camera2 Preview需要用到哪些模块
  • 二、各个模块的功能和之间的关系
    • 2.1 SurfaceTexture之SurfaceTextureListener
      • 2.1.1 首先看关于SurfaceTexture的说明
      • 2.1.2 SurfaceTextureListener的使用
    • 2.2 CameraManager
      • 2.2.1 CameraManager的作用
      • 2.2.2 使用CameraManager打开Camera
    • 2.3 CameraDevice之StateCallback
      • 2.3.1 StateCallback的作用
      • 2.3.2 StateCallback的示例代码
    • 2.4 CameraDevice之createCaptureSession
      • 2.4.1 新建CaptureRequest.Builder
      • 2.4.2 新建CameraDevice之createCaptureSession
      • 2.4.3 创建CaptureRequest
  • 小结

    前言 Camera2是Android新的Camera框架,整体来讲Camera2为应用程序提供了许多标准接口,使更多的功能可以通过参数控制;但是,灵活的同时也带来了架构的复杂。本文通过讨论实现Camera2的Preview功能,来和大家一起探讨Camera2所用到的模块最小集合。

    一、Camera2 Preview需要用到哪些模块 总结起来,用到了如下模块,
    • SurfaceTexture之SurfaceTextureListener
    • CameraManager
    • CameraDevice之StateCallback
    • CameraDevice之createCaptureSession
    • CaptureRequest.Builder、 CaptureRequest

    二、各个模块的功能和之间的关系 下面详细分析这几个模块的功能和之间的关系,

    2.1 SurfaceTexture之SurfaceTextureListener

    2.1.1 首先看关于SurfaceTexture的说明 主要目的是接收camera preview的数据并在UI上显示。
    Captures frames from an image stream as an OpenGL ES texture.
    从图像流捕获帧, 作为OpenGL ES纹理.
    The image stream may come from either camera preview or video decode. A android.view.Surface created from a SurfaceTexture can be used as an output destination for the android.hardware.camera2, android.media.MediaCodec, android.media.MediaPlayer, and android.renderscript.Allocation APIs. When updateTexImage is called, the contents of the texture object specified when the SurfaceTexture was created are updated to contain the most recent image from the image stream. This may cause some frames of the stream to be skipped.

    2.1.2 SurfaceTextureListener的使用 当与此Surface Texture关联的表面纹理可用时,可以使用此Listener来获得通知。
    当收到SurfaceTexture可用通知的时候,执行初始化camera的动作。
    示例代码如下,
    private final SurfaceTextureListener mSurfaceTextureListener = new SurfaceTextureListener() {@Overridepublic void onSurfaceTextureAvailable(@NonNull SurfaceTexture surface, int width, int height) {Log.i(TAG, "onSurfaceTextureAvailable: ++"); try {Log.i(TAG, "onCreate: call initCamera()"); initCamera(); } catch (CameraAccessException e) {e.printStackTrace(); }} @Overridepublic void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surface, int width, int height) {Log.i(TAG, "onSurfaceTextureSizeChanged: ++"); } @Overridepublic boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surface) {Log.i(TAG, "onSurfaceTextureDestroyed: ++"); return false; } @Overridepublic void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface) {Log.i(TAG, "onSurfaceTextureUpdated: ++"); }};


    2.2 CameraManager

    2.2.1 CameraManager的作用 A system service manager for detecting, characterizing, and connecting to CameraDevices.
    一种系统服务管理器,用于检测、表征和连接摄像设备。

    2.2.2 使用CameraManager打开Camera 示例代码如下,
    注意:
    openCamera的第一个参数是打开哪一个摄像头,0代表后置摄像头;1代表前置摄像头;2代表外接摄像头。这里打开的是前置摄像头。
    第二个参数是CameraDevice之StateCallback,稍后解析;
    第三个参数是Handler,这里我们把Handler放在一个新创建的thread中;
    CameraManager manager = (CameraManager) this.getSystemService(Context.CAMERA_SERVICE); manager.openCamera("1", stateCallback, mBackgroundHander);


    2.3 CameraDevice之StateCallback

    2.3.1 StateCallback的作用 A callback objects for receiving updates about the state of a camera device.
    用于接收相机设备状态更新的回调对象。

    2.3.2 StateCallback的示例代码 我们会在onOpened()函数中创建Preview Session!
    CameraDevice.StateCallback stateCallback = new CameraDevice.StateCallback() {@Overridepublic void onOpened(@NonNull CameraDevice camera) {Log.i(TAG, "onOpened: ++"); mCameraDevice = camera; createPreviewSession(); } @Overridepublic void onDisconnected(@NonNull CameraDevice camera) {Log.i(TAG, "onDisconnected: ++"); } @Overridepublic void onError(@NonNull CameraDevice camera, int error) {Log.i(TAG, "onError: ++"); }};


    2.4 CameraDevice之createCaptureSession

    2.4.1 新建CaptureRequest.Builder A builder for capture requests.

    2.4.2 新建CameraDevice之createCaptureSession A configured capture session for a CameraDevice, used for capturing images from the camera or reprocessing images captured from the camera in the same session previously.

    2.4.3 创建CaptureRequest An immutable package of settings and outputs needed to capture a single image from the camera device.
    Contains the configuration for the capture hardware (sensor, lens, flash), the processing pipeline, the control algorithms, and the output buffers. Also contains the list of target Surfaces to send image data to for this capture.
    这部分的示例代码如下,
    private void createPreviewSession() {SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture(); // should be preview size, will got it latersurfaceTexture.setDefaultBufferSize(mTextureView.getWidth(), mTextureView.getHeight()); mImageReader = ImageReader.newInstance(mTextureView.getWidth(), mTextureView.getHeight(),ImageFormat.JPEG, /*maxImages*/2); Surface surface =new Surface(surfaceTexture); try {mPreviewRequstBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); mPreviewRequstBuilder.addTarget(surface); mCameraDevice.createCaptureSession(Arrays.asList(surface, mImageReader.getSurface()),new CameraCaptureSession.StateCallback() {@Overridepublic void onConfigured(@NonNull CameraCaptureSession session) {Log.i(TAG, "onConfigured: "); if (mCameraDevice == null) {return; } else {mCaptureSession = session; } mPreviewRequest = mPreviewRequstBuilder.build(); try {mCaptureSession.setRepeatingRequest(mPreviewRequest, null, mBackgroundHander); } catch (CameraAccessException e) {e.printStackTrace(); }} @Overridepublic void onConfigureFailed(@NonNull CameraCaptureSession session) {Log.i(TAG, "onConfigureFailed: "); }}, mBackgroundHander); } catch (CameraAccessException e) {e.printStackTrace(); }}


    小结 Camera2使用SurfaceTexture组件显示Preview视频流;
    使用CameraManager打开CameraDevice之camera,同时塞入回调函数CameraDevice之StateCallback来反馈camera状态;
    当Camera状况为Opened时,创建CaptureRequest.Builder为preview,并将surface关联至builder;之后创建CameraDevice之createCaptureSession,当收到onConfigured信息时,对诸如 sensor, lens, flash等进行配置,然后呼叫Builder的build函数完成配置CaptureRequest,最后设置session为repeat模式。
    【Android开发中amera2|Android开发中amera2 Preview使用详解】到此这篇关于Android开发中amera2 Preview使用详解的文章就介绍到这了,更多相关Android Camera2 Preview内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

      推荐阅读