ARCore for Unity保存相机图像

使用ARCore for Unity,尝试将Frame.CameraImage.AcquireCameraImageBytes()保存为图像并扫描图像以查找QR码。 但转换后的图像不是实际比例,而是重复,因此无法正确扣除QR码。

这是我的代码

 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摄像机图像,其数据可通过YUV-420-888格式从CPU访问。 检查此项 。 对于YUV,缓冲区大小为宽*高* 1.5。 您可能需要将YUV转换为RGB格式。