ARCore 在按钮单击时保存相机图像 (Unity C#)

2024-01-13

我有一个类似的问题,例如以下三个问题:

  • 将 Unity ARCore 中的 AcquireCameraImageBytes() 作为图像保存到存储 https://stackoverflow.com/questions/49579334/save-acquirecameraimagebytes-from-unity-arcore-to-storage-as-an-image
  • 从 unity ARCore 保存相机图像 https://stackoverflow.com/questions/49674399/save-camera-image-from-unity-arcore
  • ARCore for Unity 保存相机图像 https://stackoverflow.com/questions/49798067/arcore-for-unity-save-camera-image

我的目标是通过单击按钮保存相机图像(没有增强对象)和相机的姿势。今天我尝试了一整天用 ARCore 保存相机图像。我尝试了上面链接的三个问题的不同方法,但没有成功。 我的 C# 脚本附加到按钮:

using UnityEngine;
using UnityEngine.UI;
using GoogleARCore;
using System.IO;
public class takeimg : MonoBehaviour
{
    private Texture2D m_TextureRender;
    public Button yourButton;
    private byte[] m_EdgeDetectionResultImage = null;

    void Start()
    {
        Button btn = yourButton.GetComponent<Button>();
        btn.onClick.AddListener(TaskOnClick);
    }

    void TaskOnClick()
    {
        var image = Frame.CameraImage.AcquireCameraImageBytes();

        m_TextureRender = new Texture2D(image.Width, image.Height, TextureFormat.RGBA32, false, false);
        m_EdgeDetectionResultImage = new byte[image.Width * image.Height * 4];


        System.Runtime.InteropServices.Marshal.Copy(image.Y, m_EdgeDetectionResultImage, 0, image.Width * image.Height * 4);

        m_TextureRender.LoadRawTextureData(m_EdgeDetectionResultImage);
        m_TextureRender.Apply();

        var encodedJpg = m_TextureRender.EncodeToJPG();
        var path = Application.persistentDataPath;

        File.WriteAllBytes(path + "/test.jpg", encodedJpg);
    }
}

目前我得到的图像看起来:保存的图像 https://i.stack.imgur.com/n9i8O.jpg它看起来类似于我上面链接的第三个SO问题。 所以有些东西仍然是错误/缺失的。有人可以帮我看看出了什么问题吗?与缓冲区有关的东西吗?

Update:与此同时,我设法取回一张黑白照片:黑白图片 https://i.stack.imgur.com/tVGTt.jpg这是我的新 TaskOnClick 函数:

void TaskOnClick()
{
    var image = Frame.CameraImage.AcquireCameraImageBytes();

    byte[] bufferY = new byte[image.Width * image.Height];
    byte[] bufferU = new byte[image.Width * image.Height / 2];
    byte[] bufferV = new byte[image.Width * image.Height / 2];
    System.Runtime.InteropServices.Marshal.Copy(image.Y, bufferY, 0, image.Width * image.Height);
    System.Runtime.InteropServices.Marshal.Copy(image.U, bufferU, 0, image.Width * image.Height / 2);
    System.Runtime.InteropServices.Marshal.Copy(image.V, bufferV, 0, image.Width * image.Height / 2);


    m_TextureRender = new Texture2D(image.Width, image.Height, TextureFormat.RGBA32, false, false);


    Color c = new Color();
    for (int y = 0; y < image.Height; y++) {
        for (int x =0; x<image.Width;x++) {
            float Y = bufferY[y * image.Width + x];
            float U = bufferU[(y/2) * image.Width + x];
            float V = bufferV[(y/2) * image.Width + x];
            c.r = Y;
            c.g = Y;
            c.b = Y;

            c.r /= 255.0f;
            c.g /= 255.0f;
            c.b /= 255.0f;

            if (c.r < 0.0f) c.r = 0.0f;
            if (c.g < 0.0f) c.g = 0.0f;
            if (c.b < 0.0f) c.b = 0.0f;

            if (c.r > 1.0f) c.r = 1.0f;
            if (c.g > 1.0f) c.g = 1.0f;
            if (c.b > 1.0f) c.b = 1.0f;

            c.a = 1.0f;
            m_TextureRender.SetPixel(image.Width-1-x, y, c);      
        }
    }

    var encodedJpg = m_TextureRender.EncodeToJPG();
    var path = Application.persistentDataPath;
    File.WriteAllBytes(path + "/test.jpg", encodedJpg);
}

有人能告诉我,Google ARCore 使用的实际 YUV 到 RGB 对话是什么吗?我尝试了一些,但图片中的颜色看起来总是不对...... 有没有比我的解决方案更简单的方法来保存当前帧的相机图像?


您可以使用 NatCam API 轻松完成此操作:https://assetstore.unity.com/packages/tools/integration/natcam-webcam-api-52154 https://assetstore.unity.com/packages/tools/integration/natcam-webcam-api-52154

如果您想要视频,还有 NatCorder

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ARCore 在按钮单击时保存相机图像 (Unity C#) 的相关文章

  • 如何有效地左填充字节数组

    假设我有一个数组 LogoDataBy byte 0x00000008 0x00000000 0x41 0x00000001 0x42 0x00000002 0x43 0x00000003 0x44 0x00000004 0x31 0x00
  • 获取不带波形符的泛型类名称[重复]

    这个问题在这里已经有答案了 我正在尝试获取类型名称T使用这个 typeof T Name 班级名称是ConfigSettings 而不是返回ConfigSettings它正在返回ConfigSettings 1 有什么具体原因吗 我怎样才能
  • 如何修复 TcpClient Ip 标头错误校验和

    我正在使用 System Net Sockets TcpClient 类 但每当我通过网络发送自定义数据包时 我都会在wireshark捕获上看到错误的校验和 我该如何修复它 问题是您在网络接口上设置了校验和卸载 这会导致您的网卡计算校验和
  • 如何获取 PropertyGrid 的单元格值 (c#)?

    如何在 C 中获取属性网格项和项的值 例如 Name Ali LastName Ahmadi Name 和 LastName 是 propertygrid 的 2 个属性 PropertyGrid只是对象的组件模型表示的视图 我会说 查看组
  • Eclipse java 断点 - 目的是什么?

    我正在学习 Android 教程 刚刚进入调试部分 我想知道断点的用途是什么 我还不能告诉 它实际上停止了应用程序 以便我可以确定它运行到该点 或者我可以设置多个断点并将它们用作标记来从断点到断点检查 停止和运行 我的代码 断点是执行停止的
  • Apple IOS 上的 C# 应用程序 [已关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我有基于 C Net 的应用程序 有什么方法可以在 Apple IOS 上运行这些应用程序吗 我没有资
  • 关于 FirstOrDefault 或 SingleOrDefault

    FirstOrDefault 或 SingleOrDefault 将返回什么类型的数据 假设我的查询返回 3 条记录 例如 empid ename salary 1 joy 1500 2 rob 4500 3 jen 6500 所以如果我们
  • 使用 OpenSSL 库在 C++ 中生成 SHA 哈希值

    如何使用以下命令生成 SHA1 或 SHA2 哈希值OpenSSL https openssl org图书馆 我搜索了谷歌 找不到任何函数或示例代码 从命令行来看 很简单 printf compute sha1 openssl sha1 您
  • 在unity3D中显示数学方程

    我想使用它的 GUI 系统统一显示数学方程 有办法吗 我正在使用 C 语言在 Unity 中进行编程 如果我还可以使用 C 代码显示数学符号 这对我来说会很有用 谢谢 自 2016 年起 您可以使用TEXDraw https assetst
  • 获取RFC返回的嵌套结构的值?

    我是 C 新手 我有 rfc 它以嵌套结构的形式从 SAP 系统返回数据 但是当我使用以下方式获取该数据时 IrfcTable table rfc getTable exporting parameter et customer 它仅返回第
  • OpenMP 和 C++:this 指针

    Is thisOpenMP 中始终共享指针 尽管编译器不会抱怨以下代码default none pragma omp parallel for default none shared n for SInt i 0 i lt n i f i
  • 随机排列

    我无法找到一种随机洗牌元素的好方法std vector经过一些操作后 恢复原来的顺序 我知道这应该是一个相当简单的算法 但我想我太累了 由于我被迫使用自定义随机数生成器类 我想我不能使用std random shuffle 无论如何这没有帮
  • 在 Ubuntu 16.04 上编译 PCL 1.7,CMake 生成的 Makefile 中出现错误

    我正在尝试让 PCL 1 7 点云库 而不是其他 pcl 在 Ubuntu 16 04 上运行 我最终希望用于 C 的东西 但现在我只是想让这些例子工作 我使用的是 Ubuntu GNU 5 3 1 附带的默认编译器和 Cmake 版本 3
  • Android 调整图片大小

    我的图像存储在 SD 卡上 每个大小约为 4MB 我想调整每个的大小 而不是将其设置为 ImageView 但我不能使用BitmapFactory decodeFile path 因为异常 java lang OutOfMemoryErro
  • 使用客户端 hello 消息进行 TLS 协议检测

    我需要检测网络流量中的 https 数据包 到目前为止 我将所有 443 标记为 https 但我不想再在这种情况下使用端口信息 检查客户端问候消息是否足够 Check 22 and version info 0300 0301 or 03
  • 比较 C# 中的对象属性[关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 Locked 这个问题及其答案是locked help locked posts因为这个问题是题外话 但却具有历史意义 目前不接受新的答案或互动
  • 在 C# 中设置风扇速度

    我知道以前有人问过这个问题 但我似乎无法让它发挥作用 我已调用以下内容 using System Management using System Management Instrumentation using System Runtime
  • 除了前一个按钮意图之外,如何添加另一个按钮意图?

    这是我的代码 它包含一个名为的按钮button1A当我单击它时 它会打开一个名为的列表list1 如何为另一个名为 button2A 的按钮添加代码 该按钮将打开一个列表 List2 import android os Bundle imp
  • Eclipse CDT C/C++:包含另一个项目的头文件

    我在 Eclipse CDT 中有两个 C 项目main and shared In shared我有一个名为calc h 我想在中使用这个标头main 所以我做了以下事情 added include calc h到相关文件main In
  • Image.Save 异常“GDI+ 中发生一般错误。”保存到 MemoryStream 时

    我有一个服务器客户端应用程序 我想从服务器获取屏幕截图 但在线bitmap Save ms System Drawing Imaging ImageFormat Png 我得到这个例外 A generic error occurred in

随机推荐