亲宝软件园·资讯

展开

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

jgw2008 人气:0
想了解Android开发中amera2 Preview使用详解的相关内容吗,jgw2008在本文为您仔细讲解Android Camera2 Preview的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,Camera2,Preview,Android,Camera,下面大家一起来学习吧。

前言

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

一、Camera2 Preview需要用到哪些模块

总结起来,用到了如下模块,

二、各个模块的功能和之间的关系

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

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() {
        @Override
        public 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();
            }
        }
 
        @Override
        public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surface, int width, int height) {
            Log.i(TAG, "onSurfaceTextureSizeChanged: ++");
        }
 
        @Override
        public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surface) {
            Log.i(TAG, "onSurfaceTextureDestroyed: ++");
            return false;
        }
 
        @Override
        public 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() {
                @Override
                public void onOpened(@NonNull CameraDevice camera) {
                    Log.i(TAG, "onOpened: ++");
                    mCameraDevice = camera;
                    createPreviewSession();
                }
 
                @Override
                public void onDisconnected(@NonNull CameraDevice camera) {
                    Log.i(TAG, "onDisconnected: ++");
                }
 
                @Override
                public 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 later
        surfaceTexture.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() {
                        @Override
                        public 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();
                            }
                        }
 
                        @Override
                        public 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模式。

加载全部内容

相关教程
猜你喜欢
用户评论