1、在Activity界面代码中:
private var mCamera: Camera? = null
private val mWidth = GwApplication.DEFAULT_REMOTE_WIDTH_EXT
private val mHeight = GwApplication.DEFAULT_REMOTE_HEIGHT_EXT
private var imgData: ImageData = ImageData(mWidth, mHeight)

/*** Camera初始化**/
private fun initCameara() {//log("====initCameara()")try {mCamera = Camera.open(GwApplication.DEFAULT_CR_CAMERA)val params = mCamera!!.getParameters()params.previewFormat = ImageFormat.NV21params.setPreviewSize(mWidth, mHeight)//params.pictureFormat = ImageFormat.NV21params.setPictureSize(mWidth, mHeight)//params.zoom = 0//params.setRotation(0)params.setPreviewFpsRange(10, 15)mCamera!!.setParameters(params)} catch (ex: RuntimeException) {ex.printStackTrace()}
}/*** 开始监听回调,设置预览**/
private fun setCallback() {//log("====setCallback()")try {// 主要是surfaceTexture获取预览数据,但不显示val surfaceTexture = SurfaceTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES)mCamera!!.setPreviewTexture(surfaceTexture)} catch (e: IOException) {e.printStackTrace()}// 设置 mCamera.addCallbackBuffer(mPreviewData) 后才会回调,旨在每处理完一帧数据回调一次mCamera!!.setPreviewCallbackWithBuffer(mPreviewCallback)mCamera!!.addCallbackBuffer(imgData.data)mCamera!!.startPreview()
}/*** 帧数据监听实现
**/
private val mPreviewCallback =android.hardware.Camera.PreviewCallback { data, camera -> // 在此处处理当前帧数据,并设置下一帧回调//log("====PreviewCallback()")imgData.setTempData(data)mCamera!!.addCallbackBuffer(imgData.data)//        if (isShow) {//            showPic(imgData.previewData)//        }}/*** 关闭相机
**/
private fun closeCamera() {mCamera!!.stopPreview()mCamera!!.setPreviewCallbackWithBuffer(null)mCamera!!.release()mCamera = null
}/*** 显示图片**/
private fun showPic(data: ByteArray) {val time = System.currentTimeMillis()try {val image = YuvImage(data, ImageFormat.NV21, mWidth, mHeight, null)if (image != null) {val stream = ByteArrayOutputStream()image.compressToJpeg(Rect(0, 0, mWidth, mHeight), 80, stream)runOnUiThread {val curTime = System.currentTimeMillis()ivCamera.setImageBitmap(null)releaseBitmap()mBitmap = BitmapFactory.decodeByteArray(stream.toByteArray(), 0, stream.size())//fastYUVtoRGB!!.convertYUVtoRGB(data, mWidth, mHeight)stream.close()ivCamera.setImageBitmap(mBitmap)ivCamera.invalidate()}}} catch (e: Exception) {}
}private fun releaseBitmap() {if(mBitmap != null) {if(!mBitmap!!.isRecycled) {mBitmap!!.recycle()}mBitmap = null}
}

2、屏幕和视频图像的效果图:

其他依赖代码:
ImageData.java文件

/**

  • author zoufeng

  • date:2021/10/15

  • desc:
    */
    public class ImageData {
    private int width;
    private int height;
    private int size;
    private byte[] data;
    private byte[] temp;
    private Boolean isNew;

    public ImageData(int width, int height) {
    this.width = width;
    this.height = height;
    size = width * height * 3 / 2;
    data = new byte[size];
    temp = new byte[size];
    isNew = true;
    }

    public int getWidth() {
    return width;
    }

    public int getHeight() {
    return height;
    }

    public byte[] getData() {
    return data;
    }

    public void setTempData(byte[] preData) {
    synchronized (isNew) {
    if(!isNew) {
    System.arraycopy(preData, 0, temp, 0, preData.length);
    isNew = true;
    }
    }
    }

    public byte[] getTempData() {
    return temp;
    }

    public boolean isNew() {
    return isNew;
    }

    public void setNew(boolean isNew) {
    synchronized (this.isNew) {
    this.isNew = isNew;
    }
    }
    }

FastYUVtoRGB.java文件:(用于YUV直接转换成Bitmap,速度比较快,图像转换在4毫秒一下。)

/**

  • author zoufeng
  • date:2021/11/17
  • desc:
    */
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.Canvas;
    import android.graphics.Matrix;
    import android.renderscript.Allocation;
    import android.renderscript.Element;
    import android.renderscript.RenderScript;
    import android.renderscript.ScriptIntrinsicYuvToRGB;
    import android.renderscript.Type;

/**

  • 使用RenderScript将视频YUV流转换为BMP

  • 注:这个类适用于CameraPreview不变的情况
    */
    public class FastYUVtoRGB {
    private RenderScript rs;
    private ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic;
    private Type.Builder yuvType, rgbaType;
    private Allocation in, out;

    public FastYUVtoRGB(Context context) {
    rs = RenderScript.create(context);
    yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));
    }

    public Bitmap convertYUVtoRGB(byte[] yuvData, int width, int height) {
    if (yuvType == null) {
    yuvType = new Type.Builder(rs, Element.U8(rs)).setX(yuvData.length);
    in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT);

         rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(width).setY(height);out = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT);}in.copyFrom(yuvData);yuvToRgbIntrinsic.setInput(in);yuvToRgbIntrinsic.forEach(out);Bitmap bmpout = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);out.copyTo(bmpout);return bmpout;
    

    }

    /**

    • Returns a transformation matrix from one reference frame into another.

    • Handles cropping (if maintaining aspect ratio is desired) and rotation.

    • @param srcWidth Width of source frame.

    • @param srcHeight Height of source frame.

    • @param dstWidth Width of destination frame.

    • @param dstHeight Height of destination frame.

    • @param applyRotation Amount of rotation to apply from one frame to another.

    •                        Must be a multiple of 90.
      
    • @param flipHorizontal should flip horizontally

    • @param flipVertical should flip vertically

    • @param maintainAspectRatio If true, will ensure that scaling in x and y remains constant,

    •                        cropping the image if necessary.
      
    • @return The transformation fulfilling the desired requirements.
      */
      public static Matrix getTransformationMatrix(
      final int srcWidth,
      final int srcHeight,
      final int dstWidth,
      final int dstHeight,
      final int applyRotation, boolean flipHorizontal, boolean flipVertical,
      final boolean maintainAspectRatio) {
      final Matrix matrix = new Matrix();

      if (applyRotation != 0) {
      if (applyRotation % 90 != 0) {
      throw new IllegalArgumentException(String.format(“Rotation of %d % 90 != 0”, applyRotation));
      }

       // Translate so center of image is at origin.matrix.postTranslate(-srcWidth / 2.0f, -srcHeight / 2.0f);// Rotate around origin.matrix.postRotate(applyRotation);
      

      }

      // Account for the already applied rotation, if any, and then determine how
      // much scaling is needed for each axis.
      final boolean transpose = (Math.abs(applyRotation) + 90) % 180 == 0;

      final int inWidth = transpose ? srcHeight : srcWidth;
      final int inHeight = transpose ? srcWidth : srcHeight;

      int flipHorizontalFactor = flipHorizontal ? -1 : 1;
      int flipVerticalFactor = flipVertical ? -1 : 1;

      // Apply scaling if necessary.
      if (inWidth != dstWidth || inHeight != dstHeight) {
      final float scaleFactorX = flipHorizontalFactor * dstWidth / (float) inWidth;
      final float scaleFactorY = flipVerticalFactor * dstHeight / (float) inHeight;

       if (maintainAspectRatio) {// Scale by minimum factor so that dst is filled completely while// maintaining the aspect ratio. Some image may fall off the edge.final float scaleFactor = Math.max(Math.abs(scaleFactorX), Math.abs(scaleFactorY));matrix.postScale(scaleFactor, scaleFactor);} else {// Scale exactly to fill dst from src.matrix.postScale(scaleFactorX, scaleFactorY);}
      

      }

      if (applyRotation != 0) {
      // Translate back from origin centered reference to destination frame.
      float dx = dstWidth / 2.0f;
      float dy = dstHeight / 2.0f;
      matrix.postTranslate(dx, dy);
      // postScale中心点如果出错,图像不会被变换
      matrix.postScale(flipHorizontalFactor, flipVerticalFactor, dx, dy);
      }

      return matrix;
      }

    /*
    /*
    example
    final Canvas canvas = new Canvas(out);
    // 这里的空白bitmap尺寸需要与变换后的预期尺寸一致
    Bitmap src = Bitmap.createBitmap(height, width, Bitmap.Config.ARGB_8888);
    Matrix transformation = getTransformationMatrix(src.getWidth(), src.getHeight(), targetWidth, targetHeight, rotation,true,false, true);
    canvas.drawBitmap(src, transformation, null);
    */
    }

Android不显示Camera视频获取摄像头视频帧数据相关推荐

  1. android获取摄像头视频帧数据

    1.思路 一.通过android的Camera.setPreviewCallback(PreviewCallback cb)监听回调. 二.此回调函数public void onPreviewFram ...

  2. pyQT 视频播放器(三) 实现视频截图、获取每一帧数据

    pyQT 视频播放器(三) 实现视频截图.获取每一帧数据 背景 方法调研 详细代码说明 最终效果 总结: 参考资料 背景 在 "PyQt5 实现视频播放器(二) ,详细版本 ,适合新手入门& ...

  3. 使用FFMpeg API 获取摄像头的图像数据

    文章目录 1. 获取摄像头的信息 2. 打开并初始化摄像头 3. 获取摄像头数据及渲染 摄像头是我们比较常用的外设,很多场景我们都会用到摄像头.比如视频直播.视频监控等各个领域都会用到摄像头.摄像头图 ...

  4. opencv获取外接摄像头_opencv获取摄像头视频

    结合Leaning OpenCV 第二个例子 显示一个视屏文件 写了一下 获取摄像头的代码为并且创建窗口显示的代码为: #include "stdafx.h" #include # ...

  5. Android webview加载页面获取摄像头权限实践(Kotlin)

    场景介绍: Android app中的webview加载含摄像头相关逻辑的H5页面,进行赋权的操作 H5页面调取摄像头逻辑: vue编写 <template><div>< ...

  6. Qt从视频中获取每一帧数据

    参考了以下文章:https://blog.csdn.net/jxbinwd/article/details/81034339 我按照上面文章的描述写程序,解析出来的图像保存成图片.保存的图片有很多乱码 ...

  7. H.264裸流文件中获取每一帧数据

    测试解码器性能时,最常用的无非是向解码器中推送码流. 之前封装了一个avc的解码器,想做一个测试,读取H.264裸流文件将码流定期定时推送到解码器. 测试其实很简单: 1.了解H.264裸流文件的构成 ...

  8. 利用Flash获取摄像头视频进行动态捕捉

    一.引言 摄像头游戏是随着摄像头的普及和图形图像技术的不断发展而出现的.它摆脱了鼠标键盘的限制,给游戏者带来全新的游戏体验,能够实现许多传统游戏无法实现的游戏效果.它通过摄像头将玩家投影到游戏中,由玩 ...

  9. Android最新相机(Camera)拍照、拍视频全面总结

    介绍 利用系统相机 调用系统相机拍照 获取小图标 获取全尺寸图片 添加到相册 系统相机拍视频 自定义相机 自定义相机拍照 监测设备是否有相机可使用 利用SurfaceView创建拍照时预览界面 拍照并 ...

最新文章

  1. 嵌套母版页中的控件访问
  2. Important table for Fiori launchpad Designer and configuration
  3. 如何成为数据科学家_成为数据科学家的5大理由
  4. C++primer 第 2 章 变量和基本类型
  5. TypeError: cannot perform reduce with flexible type
  6. gitee项目能用SVN拉取吗_基于SpringBoot的车牌识别系统(附项目地址)
  7. sybase 事务 超时返回_分布式事务设计与实践-消息最终一致性
  8. jquery 给a标签加上或去掉下划线
  9. 【前端】html+css实现鼠标悬浮变色的按钮 hover【HTML+CSS+JavaScript(JS)】
  10. 世界超长经典名车荟萃
  11. STM32单片机驱动L298N
  12. Unity3D-----摄像机镜头移动并限制角度
  13. N个数选出M个最小或最大值算法
  14. 百会CRM发布新春致5万企业用户的公开信
  15. 【OpenStreetMap】任意城市道路数据下载(附带数据解释)
  16. 域控服务器怎么开策略,组策略(域和域服务的搭建)
  17. word转excel
  18. React中文文档之Hello world翻译
  19. PPT打开之后不能编辑,怎么解决?
  20. 2022年全国职业院校技能大赛 网络搭建与应用赛项 公开赛卷 (十套合卷)

热门文章

  1. 热带太平洋的1.5层C网格海洋模式数值模拟
  2. iOS 开发 高级:通过AirServer使用AirPlay镜像并实现多屏幕显示控制
  3. Windows下SSD安装验证步骤
  4. VNote v2.7.1 绿色版下载
  5. 2021-06-10 工作笔记 ng-change不能触发的解决方法+wui-date日期选择器使用onchange调用函数报错
  6. PCB板子的奥秘—板材的TG值含义
  7. 全面解析HTTP/2:历史、特性、调试、性能
  8. 初识网络及socket编程基础
  9. 达观招聘丨听说你想来做人工智能了
  10. 职场漂流记:序章说明之要