Android开发中amera2 Preview怎么用

发布时间:2021-09-09 13:38:11 作者:小新
来源:亿速云 阅读:127

小编给大家分享一下Android开发中amera2 Preview怎么用,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

前言

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();
        }
    }

以上是“Android开发中amera2 Preview怎么用”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. Android开发实践:掌握Camera的预览方向和拍照方向
  2. python中Atom插件如何使用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android camera2 preview

上一篇:LNMP架构的搭建及应用部署方法

下一篇:怎么通过重启路由的方法切换IP地址

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》