请选择 进入手机版 | 继续访问电脑版

[Android] Android开辟 中amera2 Preview使用 详解

[复制链接]
查看56 | 回复16 | 2021-9-14 05:43:58 | 显示全部楼层 |阅读模式
目次

前言

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

  1. <a target="_blank" href="https://developer.android.google.cn/reference/kotlin/android/view/Surface.html" rel="external nofollow" >android.view.Surface</a>
复制代码
created from a SurfaceTexture can be used as an output destination for the
  1. <a target="_blank" href="https://developer.android.google.cn/reference/kotlin/android/hardware/camera2/package-summary.html" rel="external nofollow" >android.hardware.camera2</a>
复制代码
,
  1. <a target="_blank" href="https://developer.android.google.cn/reference/kotlin/android/media/MediaCodec.html" rel="external nofollow" >android.media.MediaCodec</a>
复制代码
,
  1. <a target="_blank" href="https://developer.android.google.cn/reference/kotlin/android/media/MediaPlayer.html" rel="external nofollow" >android.media.MediaPlayer</a>
复制代码
, and
  1. <a target="_blank" href="https://developer.android.google.cn/reference/kotlin/android/renderscript/Allocation.html" rel="external nofollow" >android.renderscript.Allocation</a>
复制代码
APIs. When
  1. <a target="_blank" href="https://developer.android.google.cn/reference/kotlin/android/graphics/SurfaceTexture?hl=en#updateTexImage%28%29" rel="external nofollow" >updateTexImage</a>
复制代码
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的动作。
示例代码如下,

  1. private final SurfaceTextureListener mSurfaceTextureListener = new SurfaceTextureListener() {
  2. @Override
  3. public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surface, int width, int height) {
  4. Log.i(TAG, "onSurfaceTextureAvailable: ++");
  5. try {
  6. Log.i(TAG, "onCreate: call initCamera()");
  7. initCamera();
  8. } catch (CameraAccessException e) {
  9. e.printStackTrace();
  10. }
  11. }
  12. @Override
  13. public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surface, int width, int height) {
  14. Log.i(TAG, "onSurfaceTextureSizeChanged: ++");
  15. }
  16. @Override
  17. public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surface) {
  18. Log.i(TAG, "onSurfaceTextureDestroyed: ++");
  19. return false;
  20. }
  21. @Override
  22. public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface) {
  23. Log.i(TAG, "onSurfaceTextureUpdated: ++");
  24. }
  25. };
复制代码

2.2 CameraManager

2.2.1 CameraManager的作用

A system service manager for detecting, characterizing, and connecting to

  1. <a target="_blank" href="https://developer.android.google.cn/reference/kotlin/android/hardware/camera2/CameraDevice" rel="external nofollow" >CameraDevices</a>
复制代码
.
一种体系 服务管理器,用于检测、表征和毗连 摄像装备 。

2.2.2 利用 CameraManager打开Camera

示例代码如下,

留意 :
openCamera的第一个参数是打开哪一个摄像头,0代表后置摄像头;1代表前置摄像头;2代表外接摄像头。这里打开的是前置摄像头。
第二个参数是CameraDevice之StateCallback,稍后剖析 ;
第三个参数是Handler,这里我们把Handler放在一个新创建的thread中;

  1. CameraManager manager = (CameraManager) this.getSystemService(Context.CAMERA_SERVICE);
  2. 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!

  1. CameraDevice.StateCallback stateCallback = new CameraDevice.StateCallback() {
  2. @Override
  3. public void onOpened(@NonNull CameraDevice camera) {
  4. Log.i(TAG, "onOpened: ++");
  5. mCameraDevice = camera;
  6. createPreviewSession();
  7. }
  8. @Override
  9. public void onDisconnected(@NonNull CameraDevice camera) {
  10. Log.i(TAG, "onDisconnected: ++");
  11. }
  12. @Override
  13. public void onError(@NonNull CameraDevice camera, int error) {
  14. Log.i(TAG, "onError: ++");
  15. }
  16. };
复制代码

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

  1. <a target="_blank" href="https://developer.android.google.cn/reference/android/hardware/camera2/CameraDevice" rel="external nofollow" >CameraDevice</a>
复制代码
, 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.

这部分的示例代码如下,

  1. private void createPreviewSession() {
  2. SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture();
  3. // should be preview size, will got it later
  4. surfaceTexture.setDefaultBufferSize(mTextureView.getWidth(), mTextureView.getHeight());
  5. mImageReader = ImageReader.newInstance(mTextureView.getWidth(), mTextureView.getHeight(),
  6. ImageFormat.JPEG, /*maxImages*/2);
  7. Surface surface =new Surface(surfaceTexture);
  8. try {
  9. mPreviewRequstBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
  10. mPreviewRequstBuilder.addTarget(surface);
  11. mCameraDevice.createCaptureSession(Arrays.asList(surface, mImageReader.getSurface()),
  12. new CameraCaptureSession.StateCallback() {
  13. @Override
  14. public void onConfigured(@NonNull CameraCaptureSession session) {
  15. Log.i(TAG, "onConfigured: ");
  16. if (mCameraDevice == null) {
  17. return;
  18. } else {
  19. mCaptureSession = session;
  20. }
  21. mPreviewRequest = mPreviewRequstBuilder.build();
  22. try {
  23. mCaptureSession.setRepeatingRequest(mPreviewRequest, null, mBackgroundHander);
  24. } catch (CameraAccessException e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. @Override
  29. public void onConfigureFailed(@NonNull CameraCaptureSession session) {
  30. Log.i(TAG, "onConfigureFailed: ");
  31. }
  32. }, mBackgroundHander);
  33. } catch (CameraAccessException e) {
  34. e.printStackTrace();
  35. }
  36. }
复制代码

小结

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 Preview利用 详解的文章就先容 到这了,更多相干 Android Camera2 Preview内容请搜索 脚本之家从前 的文章或继续欣赏 下面的相干 文章盼望 大家以后多多支持脚本之家!


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

avatar 123457736 | 2021-9-14 07:33:25 | 显示全部楼层
admin楼主,您忘记吃药了吧?
回复

使用道具 举报

avatar 紫色214 | 2021-9-15 23:08:54 | 显示全部楼层
admin楼主又闹绯闻了!
回复

使用道具 举报

avatar 水月花郎稳 | 2021-9-18 02:29:29 | 显示全部楼层
勤奋灌水,天天向上!
回复

使用道具 举报

avatar 倪丹军 | 2021-9-20 15:27:33 | 显示全部楼层
最近回了很多帖子,都没人理我!
回复

使用道具 举报

avatar 天寿保健护理彰 | 2021-9-21 00:58:00 | 显示全部楼层
admin楼主的头像能辟邪啊!
回复

使用道具 举报

avatar 安夕萌mdj | 2021-9-26 22:18:24 | 显示全部楼层
每天顶顶贴,一身轻松啊!
回复

使用道具 举报

avatar 乔峰之逆风痰 | 2021-10-4 10:05:20 | 显示全部楼层
哥回复的不是帖子,是寂寞!
回复

使用道具 举报

admin楼主很有艺术范!
回复

使用道具 举报

内容很有深度!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则