使用 ARCore for Unity,尝试保存Frame.CameraImage.AcquireCameraImageBytes()
作为图像并扫描图像中的二维码。但转换后的图像不是实际比例,并且是重复的,因此无法正确推断二维码。
这是我的代码
void Update()
{
using (var image = Frame.CameraImage.AcquireCameraImageBytes())
{
if (image.IsAvailable)
{
byte[] m_EdgeImage = null;
Color32[] pixels = null;
IParser Parser = new ZXingParser();
if (_texture == null || m_EdgeImage == null || _texture.width != image.Width || _texture.height != image.Height)
{
_texture = new Texture2D(image.Width, image.Height, TextureFormat.RGBA32, false, false);
m_EdgeImage = new byte[image.Width * image.Height*4];
}
System.Runtime.InteropServices.Marshal.Copy(image.Y, m_EdgeImage, 0, image.Width * image.Height);
_texture.LoadRawTextureData(m_EdgeImage);
_texture.Apply();
ParserResult Result = Parser.Decode(pixels, _texture.width, _texture.height);
if (Result != null)
{
Debug.Log("QRCODE");
}
else
{
var encodedJpg = _texture.EncodeToJPG();
var path = Application.persistentDataPath;
File.WriteAllBytes(path + "/test.jpg", encodedJpg);
Debug.Log("NOQRCODE");
Application.Quit();
}
}
}
}
这是转换后的图像
这里出了什么问题
ARCore 相机图像,其数据可通过 CPU 以 YUV-420-888 格式访问检查这个 https://developers.google.com/ar/reference/unity/struct/GoogleARCore/CameraImageBytes。 YUV 的缓冲区大小为宽度*高度*1.5。您可能需要将 YUV 转换为 RGB 格式。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)