【发布时间】:2018-03-30 18:13:06
【问题描述】:
使用 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); }
【问题讨论】: