微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 将Unity ARCore中的AcquireCameraImageBytes()保存为存储为图像

使用Unity和新的1.1版本的ARCore,API公开了一些获取相机信息的新方法.但是,我找不到任何好的示例,例如将其作为文件保存为本地存储作为jpg.

ARCore示例有一个很好的例子,可以检索摄像机数据,然后在这里做一些事情:https://github.com/google-ar/arcore-unity-sdk/blob/master/Assets/GoogleARCore/Examples/ComputerVision/Scripts/ComputerVisionController.cs#L212并且有几个例子可以检索该类中的摄像机数据,但没有任何保存数据的例子.

我已经看到了这一点:How to take & save picture / screenshot using Unity ARCore SDK?使用旧的API获取数据的方式,并没有真正详细介绍保存.

我理想的是通过Unity将数据从API中的Frame.Cameraimage.AcquireCameraimageBytes()转换为磁盘上的存储jpg.

更新

我已经开始使用它主要是通过在ARCore github页面解决这个问题:https://github.com/google-ar/arcore-unity-sdk/issues/72#issuecomment-355134812修改下面的Sonny的答案,所以只有一个被接受才公平.

如果其他人试图这样做,我必须执行以下步骤:

>在Start方法添加一个回调,以便在图像可用时运行OnImageAvailable方法

public void Start()
{
    TextureReaderComponent.OnImageAvailableCallback += OnImageAvailable;
}

>将TextureReader(从SDK提供的计算机视觉示例)添加到您的相机和脚本中
>你的OnImageAvailable应该看起来像这样:

/// <summary>
/// Handles a new cpu image.
/// </summary>
/// <param name="format">The format of the image.</param>
/// <param name="width">Width of the image, in pixels.</param>
/// <param name="height">Height of the image, in pixels.</param>
/// <param name="pixelBuffer">Pointer to raw image buffer.</param>
/// <param name="bufferSize">The size of the image buffer, in bytes.</param>
private void OnImageAvailable(TextureReaderApi.ImageFormatType format, int width, int height, IntPtr pixelBuffer, int bufferSize)
{
    if (m_TexturetoRender == null || m_EdgeImage == null || m_ImageWidth != width || m_ImageHeight != height)
    {
        m_TexturetoRender = new Texture2D(width, height, TextureFormat.RGBA32, false, false);
        m_EdgeImage = new byte[width * height * 4];
        m_ImageWidth = width;
        m_ImageHeight = height;
    }

    System.Runtime.InteropServices.Marshal.copy(pixelBuffer, m_EdgeImage, 0, bufferSize);

    // Update the rendering texture with the sampled image.
    m_TexturetoRender.LoadRawTextureData(m_EdgeImage);
    m_TexturetoRender.Apply();

    var encodedJpg = m_TexturetoRender.EncodetoJPG();
    var path = Application.persistentDataPath;

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

解决方法:

在Unity中,应该可以将原始图像数据加载到纹理中,然后使用UnityEngine.ImageConversion.EncodeToJPG将其保存到JPG.示例代码

public class Example : MonoBehavIoUr
{
    private Texture2D _texture;
    private TextureFormat _format = TextureFormat.RGBA32;

    private void Awake()
    {
        _texture = new Texture2D(width, height, _format, false);
    }

    private void Update()
    {
        using (var image = Frame.Cameraimage.AcquireCameraimageBytes())
        {
            if (!image.IsAvailable) return;

            // Load the data into a texture 
            // (this is an expensive call, but it may be possible to optimize...)
            _texture.LoadRawTextureData(image);
            _texture.Apply();
        }
    }

    public void SaveImage()
    {
        var encodedJpg = _texture.EncodetoJPG();
        File.WriteallBytes("test.jpg", encodedJpg)
    }
}

但是,我不确定TextureFormat是否对应于与Frame.Cameraimage.AcquireCameraimageBytes()一起使用的格式. (我熟悉Unity但不熟悉ARCore.)请参阅TextureFormat的Unity文档,以及它是否与ARCore的ImageFormatType兼容.

此外,测试代码是否足够适合您的应用程序.

编辑:正如用户@Lece所解释的那样,使用File.WriteallBytes保存编码数据.我已经更新了上面的代码示例,因为我最初省略了该步骤.

编辑#2:有关ARCore的完整答案,请参阅问题帖子的更新.这里的评论也可能有用 – 约旦指出“主要部分是使用computer vision sdk example here的纹理阅读器”.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐