Jor*_*son 4 c# android unity-game-engine arcore
使用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并且有一些检索该类中的摄像机数据的示例,但没有任何保存该数据的示例.
我已经看过:如何使用Unity ARCore SDK拍摄并保存图片/屏幕截图?它使用较旧的API获取数据的方式,并没有真正详细说明保存.
我理想的是Frame.CameraImage.AcquireCameraImageBytes()通过Unity 将数据从API转换为磁盘上存储的jpg的方法.
更新
我已经开始工作了主要是在ARCore github页面上挖掘这个问题:https://github.com/google-ar/arcore-unity-sdk/issues/72#issuecomment-355134812并修改下面的Sonny的答案,所以一个人被接受是公平的.
如果其他人试图这样做,我必须执行以下步骤:
OnImageAvailable在图像可用时,向Start方法添加回调以运行方法:
public void Start()
{
TextureReaderComponent.OnImageAvailableCallback += OnImageAvailable;
}
Run Code Online (Sandbox Code Playgroud)将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);
}
Run Code Online (Sandbox Code Playgroud)在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)
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我不确定TextureFormat是否对应于可以使用的格式Frame.CameraImage.AcquireCameraImageBytes().(我熟悉Unity但不熟悉ARCore.)请参阅Unity有关TextureFormat的文档,以及它是否与ARCore兼容ImageFormatType.
此外,测试代码是否足够适合您的应用程序.
编辑:正如用户@Lece所解释的那样,保存编码数据File.WriteAllBytes.我已经更新了上面的代码示例,因为我最初省略了该步骤.
编辑#2:有关ARCore的完整答案,请参阅问题帖子的更新.这里的评论也可能有用 - 约旦指出"主要部分是使用来自计算机视觉sdk示例的纹理阅读器".
| 归档时间: |
|
| 查看次数: |
2192 次 |
| 最近记录: |