当前位置: 首页 > news >正文

Android Camera系列(二):TextureView+Camera

两岸猿声啼不住,轻舟已过万重山—李白

本系列主要讲述Android开发中Camera的相关操作、预览方式、视频录制等,项目结构代码耦合性低,旨在帮助大家能从中有所收获(方便copy :) ),对于个人来说也是一个总结的好机会

Alt

本章我们来讲解TextureView进行Camera预览,基于上一篇Android Camera系列(一):SurfaceView+Camera的成果,我们已经对Camera进行了封装,CameraManager拿来直接使用就好

一.TextureView使用

优点:
支持复杂的视图变换‌:与SurfaceView不同,TextureView支持包括缩放、旋转在内的各种变换操作,这些操作在视图层次中进行,使得TextureView更加灵活和适应复杂的用户界面需求。
缺点:
性能不如SurfaceView:在低端设备或高GPU负荷情况下,可能会出现掉帧或卡顿现象,低性能可以给一个参考指标大概就是10年前的设备,或者是给欠发达国家提供的低性能的海外设备

TextureView作为Camera的预览视图与SurfaceView不同,TextureView要获取SurfaceTexture,并传递给Camera作为预览容器

CameraManager针对SurfaceTexture的预览接口

    /*** 使用TextureView预览Camera** @param surface*/@Overridepublic synchronized void startPreview(SurfaceTexture surface) {Logs.i(TAG, "startPreview...");if (isPreviewing) {return;}if (mCamera != null) {try {mCamera.setPreviewTexture(surface);if (!mPreviewBufferCallbacks.isEmpty()) {mCamera.addCallbackBuffer(new byte[mPreviewWidth * mPreviewHeight * 3 / 2]);mCamera.setPreviewCallbackWithBuffer(mPreviewCallback);}mCamera.startPreview();onPreview(mPreviewWidth, mPreviewHeight);} catch (Exception e) {onPreviewError(CAMERA_ERROR_PREVIEW, e.getMessage());}}}
  1. 自定义CameraTextureView继承TextureView
  2. 实现TextureView.SurfaceTextureListener接口,并在CameraTextureView初始化时设置回调
  3. 实现自定义CameraCallback接口,监听Camera状态
  4. 一定要实现onResumeonPause接口,并在对应的Activity生命周期中调用。这是所有使用Camera的bug的源头
/*** 摄像头预览TextureView** @author xiaozhi* @since 2024/8/22*/
public abstract class BaseTextureView extends TextureView implements TextureView.SurfaceTextureListener, CameraCallback, BaseCameraView {private static final String TAG = BaseTextureView.class.getSimpleName();private Context mContext;private SurfaceTexture mSurfaceTexture;private boolean isMirror;private boolean hasSurface; // 是否存在摄像头显示层private ICameraManager mCameraManager;private int mRatioWidth = 0;private int mRatioHeight = 0;private int mTextureWidth;private int mTextureHeight;public BaseTextureView(Context context) {super(context);init(context);}public BaseTextureView(Context context, AttributeSet attrs) {super(context, attrs);init(context);}public BaseTextureView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init(context);}private void init(Context context) {mContext = context;mCameraManager = createCameraManager(context);mCameraManager.setCameraCallback(this);setSurfaceTextureListener(this);}public abstract ICameraManager createCameraManager(Context context);/*** 获取摄像头工具类** @return*/public ICameraManager getCameraManager() {return mCameraManager;}/*** 是否镜像** @return*/public boolean isMirror() {return isMirror;}/*** 设置是否镜像** @param mirror*/public void setMirror(boolean mirror) {isMirror = mirror;requestLayout();}private void setAspectRatio(int width, int height) {if (width < 0 || height < 0) {throw new IllegalArgumentException("Size cannot be negative.");}mRatioWidth = width;mRatioHeight = height;requestLayout();}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);int width = MeasureSpec.getSize(widthMeasureSpec);int height = MeasureSpec.getSize(heightMeasureSpec);if (0 == mRatioWidth || 0 == mRatioHeight) {setMeasuredDimension(width, width * 4 / 3);} else {if (width < height * mRatioWidth / mRatioHeight) {setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);} else {setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);}}if (isMirror) {Matrix transform = new Matrix();transform.setScale(-1, 1, getMeasuredWidth() / 2, 0);setTransform(transform);} else {setTransform(null);}}/*** 获取SurfaceTexture** @return*/@Overridepublic SurfaceTexture getSurfaceTexture() {return mSurfaceTexture;}@Overridepublic void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {Logs.i(TAG, "onSurfaceTextureAvailable.");mTextureWidth = width;mTextureHeight = height;mSurfaceTexture = surfaceTexture;hasSurface = true;openCamera();}@Overridepublic void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int width, int height) {Logs.i(TAG, "onSurfaceTextureSizeChanged.");}@Overridepublic boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {Logs.v(TAG, "onSurfaceTextureDestroyed.");closeCamera();hasSurface = false;return true;}@Overridepublic void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {}/*** 打开摄像头并预览*/@Overridepublic void onResume() {if (hasSurface) {// 当activity暂停,但是并未停止的时候,surface仍然存在,所以 surfaceCreated()// 并不会调用,需要在此处初始化摄像头openCamera();}}/*** 停止预览并关闭摄像头*/@Overridepublic void onPause() {closeCamera();}@Overridepublic void onDestroy() {}/*** 初始化摄像头,较为关键的内容*/private void openCamera() {if (mSurfaceTexture == null) {Logs.e(TAG, "mSurfaceTexture is null.");return;}if (mCameraManager.isOpen()) {Logs.w(TAG, "Camera is opened!");return;}mCameraManager.openCamera();}private void closeCamera() {mCameraManager.releaseCamera();}@Overridepublic void onOpen() {mCameraManager.startPreview(mSurfaceTexture);}@Overridepublic void onOpenError(int error, String msg) {}@Overridepublic void onPreview(int previewWidth, int previewHeight) {if (mTextureWidth > mTextureHeight) {setAspectRatio(previewWidth, previewHeight);} else {setAspectRatio(previewHeight, previewWidth);}}@Overridepublic void onPreviewError(int error, String msg) {}@Overridepublic void onClose() {}
}

1.Camera操作时机

  • onSurfaceTextureAvailable回调中打开Camera,在onSurfaceTextureDestroyed中关闭摄像头
  • 一定要记得在onResume中也打开摄像头,onPause中关闭摄像头。

再次强调onResumeonPuase中一定也要对Camera进行打开关闭操作。SurfaceTexture的回调和SurfaceHolder不同,页面不显示时SurfaceHolder会destroy,而SurfaceTexture并不会回调onSurfaceTextureDestroyed。如果我们按Home键回到桌面打开系统相机,然后再次进入我们的应用你会发现预览黑屏,这就是没有正确在生命周期中关闭Camera导致。这也是很多别的开源项目正常用没问题,随便退出再进来总有Bug的源头。

2.TextureView计算大小

基本上和CameraSurfaceView一样,我们在onPreview回调中设置TextureView的大小和比例即可

二.最后

本文介绍了Camera+TextureView的基本操作及关键代码。本章内容不是很多,这得益于我们上一章定义好了CameraManager的功劳。下一章介绍GLSurfaceView同样也是用第一章的CameraManager,嘻嘻嘻。

lib-camera库包结构如下:

说明
cameracamera相关操作功能包,包括Camera和Camera2。以及各种预览视图
encoderMediaCdoec录制视频相关,包括对ByteBuffer和Surface的录制
glesopengles操作相关
permission权限相关
util工具类

每个包都可独立使用做到最低的耦合,方便白嫖

github地址:https://github.com/xiaozhi003/AndroidCamera,https://gitee.com/xiaozhi003/android-camera

参考:

  1. https://github.com/afei-cn/CameraDemo
  2. https://github.com/saki4510t/UVCCamera
  3. https://github.com/google/grafika
http://www.lryc.cn/news/432716.html

相关文章:

  • DFS算法专题(一)——二叉树中的深搜【回溯与剪枝的初步注入】
  • AWS SES服务 Golang接入教程(排坑版)
  • Vite + Vue3 +Vant4出现Toast is not a function
  • 【MATLAB】模拟退火算法
  • 什么是Kubernetes RBAC?
  • 在Spring Boot中通过自定义注解、反射以及AOP(面向切面编程)
  • 安防监控视频平台LntonAIServer视频智能分析平台新增视频质量诊断功能
  • vscode从本地安装插件
  • Superset二次开发之新增复选框Checkbox筛选器
  • PromQL 语法
  • 掌握Go语言中的时间与日期操作
  • 4G模块、WIFI模块、NBIOT模块通过AT指令连接华为云物联网服务器(MQTT协议)
  • spring数据校验Validation
  • Uniapp基于uni拦截器+Promise的请求函数封装
  • 【工具】使用 Jackson 实现优雅的 JSON 格式化输出
  • ApacheKafka中的设计
  • .NET 自定义过滤器 - ActionFilterAttribute
  • VMware Fusion Pro 13 for Mac虚拟机软件
  • HarmonyOS应用开发环境搭建
  • YOLOv8改进实战 | 注意力篇 | 引入ICCV2023顶会LSKNet:大选择性卷积注意力模块LSKA,助力小目标检测
  • 00Mac安装playwright
  • materail3 CircularProgressIndicator和LinearProgressIndicator有难看的白块和断点
  • 菜鸟入门Docker
  • 什么是单片机?为什么要学习单片机?
  • 电子发射与气体导电
  • 【数据库】MySQL表的Updata(更新)和Delete(删除)操作
  • Unity Adressables 使用说明(六)加载(Load) Addressable Assets
  • 视频监控系统布局策略:EasyCVR视频汇聚平台构建高效、全面的安全防线
  • Spark的Web界面
  • 语言中的内联