• 安卓Unity3D Camera图像和音频采集推送代码


        安卓Unity3d 可以使用ReadPixels从当前Render Target读取图像,音频可以从AudioClip读取,具体调用GetData接口,读取到的可能是float类型,有些音频编码器可能需要sint16格式,这需要做一个转换。 读图像先用StartCoroutine创建协程,等待WaitForEndOfFrame, 之后调用ReadPixels就好,调用之前要创建一个和Camera一样的大小Texture2D, 创建代码如下:

     Texture2D image_texture = new Texture2D(w, h, texture_format_, false);

      启动停止协程,捕获图像代码如下:

    1. // Github: https://github.com/daniulive/SmarterStreaming
    2. // Copyright (C) 1130758427@qq.com
    3. public class TestCameraCapture: MonoBehaviour
    4. {
    5. Coroutine coroutine_ = null;
    6. void OnDisable()
    7. {
    8. coroutine_ = StartCoroutine(captureImage());
    9. }
    10. void OnEnable()
    11. {
    12. StopCoroutine(coroutine_);
    13. coroutine_ = null;
    14. }
    15. IEnumerator captureImage()
    16. {
    17. yield return new WaitForEndOfFrame();
    18. image_texture.ReadPixels(new Rect(0, 0, w, h), 0, 0, false);
    19. // 编码图像
    20. }
    21. }

      这个是Unity3D官方给出的代码:

    1. using System.IO;
    2. using UnityEngine;
    3. using UnityEngine.Networking;
    4. using System.Collections;
    5. public class ExampleClass : MonoBehaviour
    6. {
    7. // Take a shot immediately
    8. IEnumerator Start()
    9. {
    10. UploadPNG();
    11. yield return null;
    12. }
    13. IEnumerator UploadPNG()
    14. {
    15. // We should only read the screen buffer after rendering is complete
    16. yield return new WaitForEndOfFrame();
    17. // Create a texture the size of the screen, RGB24 format
    18. int width = Screen.width;
    19. int height = Screen.height;
    20. Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
    21. // Read screen contents into the texture
    22. tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
    23. tex.Apply();
    24. // Encode texture into PNG
    25. byte[] bytes = tex.EncodeToPNG();
    26. Destroy(tex);
    27. // For testing purposes, also write to a file in the project folder
    28. // File.WriteAllBytes(Application.dataPath + "/../SavedScreen.png", bytes);
    29. // Create a Web Form
    30. WWWForm form = new WWWForm();
    31. form.AddField("frameCount", Time.frameCount.ToString());
    32. form.AddBinaryData("fileUpload", bytes);
    33. // Upload to a cgi script
    34. var w = UnityWebRequest.Post("http://localhost/cgi-bin/env.cgi?post", form);
    35. yield return w.SendWebRequest();
    36. if (w.result != UnityWebRequest.Result.Success)
    37. print(w.error);
    38. else
    39. print("Finished Uploading Screenshot");
    40. yield return null;
    41. }
    42. }

       上面代码是读取图像后保存成PNG图片,只要把读取的纹理传给我写的C接口即可:

    1. // Github: https://github.com/daniulive/SmarterStreaming
    2. // Copyright (C) 1130758427@qq.com
    3. private int postLayerImageRGBA8888Native(long handle, int index, int left, int top,
    4. long rgba_plane, int offset, int row_stride, int width, int height,
    5. int is_vertical_flip, int is_horizontal_flip,
    6. int scale_width, int scale_height, int scale_filter_mode,
    7. int rotation_degree){
    8. return obj_.Call<int>("OnPostLayerImageRGBA8888Native", handle, index, left, top,
    9. rgba_plane, offset, row_stride, width, height,
    10. is_vertical_flip, is_horizontal_flip,
    11. scale_width, scale_height, scale_filter_mode, rotation_degree);
    12. }
    13. private int postLayerImageRGBX8888Native(long handle, int index, int left, int top,
    14. long rgbx_plane, int offset, int row_stride, int width, int height,
    15. int is_vertical_flip, int is_horizontal_flip,
    16. int scale_width, int scale_height, int scale_filter_mode,
    17. int rotation_degree){
    18. return obj_.Call<int>("OnPostLayerImageRGBX8888Native", handle, index, left, top,
    19. rgbx_plane, offset, row_stride, width, height,
    20. is_vertical_flip, is_horizontal_flip,
    21. scale_width, scale_height, scale_filter_mode, rotation_degree);
    22. }

    读Camera和读Screen差不多,在我自己手机上硬编码视频可以跑到60帧,有些眼镜性能较弱,帧率稍微低些,不过基本都能满足需求, 实际开发中遇到不少问题,经验就是多看Unity3D官方文档,基本都能找到解决方案.

  • 相关阅读:
    spring boot项目资源跳转,及引入js、css和a标签,ajax等的路径问题
    vuex基础用法1.0
    Prometheus、Grafana安装-部署-监控linux
    PostgreSQL数据优化——死元组清理
    JCEF中js与java交互、js与java相互调用
    不要再说离群点难观测,来学学这种特征异常检测的高效方法
    redis 登录客户端命令
    实现数字化转型的解决方案
    面试题精讲丨MySQL的隔离级别真的越高越好吗?!
    Xilinx Zynq 7000系列中端FPGA解码MIPI视频,基于MIPI CSI-2 RX Subsystem架构实现,提供5套工程源码和技术支持
  • 原文地址:https://blog.csdn.net/lsheevyfg/article/details/126601246