ARCore for Unity save camera image

2019-03-03 17:53发布

问题:

Using ARCore for Unity, trying to save Frame.CameraImage.AcquireCameraImageBytes() as image and scanning the image for QR code. But the converted image is not in actual scale and it is repeating, so not able to deduct the QR code correctly.

Here is my code

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();
            }
        }
    }
}

Here is the converted image

What is wrong here

回答1:

An ARCore camera image with its data accessible from the CPU in YUV-420-888 formatCheck this. The buffer size is width*height*1.5 for YUV. You may need to convert YUV to RGB format.