Save AcquireCameraImageBytes() from Unity ARCore to storage as an image

前端 未结 2 797
误落风尘
误落风尘 2020-12-10 18:17

Using unity, and the new 1.1 version of ARCore, the API exposes some new ways of getting the camera information. However, I can\'t find any good examples of saving this as a

相关标签:
2条回答
  • 2020-12-10 18:59

    In Unity, it should be possible to load the raw image data into a texture and then save it to a JPG using UnityEngine.ImageConversion.EncodeToJPG. Example code:

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

    However, I'm not sure if the TextureFormat corresponds to a format that works with Frame.CameraImage.AcquireCameraImageBytes(). (I'm familiar with Unity but not ARCore.) See Unity's documentation on TextureFormat, and whether that is compatible with ARCore's ImageFormatType.

    Also, test whether the code is performant enough for your application.

    EDIT: As user @Lece explains, save the encoded data with File.WriteAllBytes. I've updated my code example above as I omitted that step originally.

    EDIT #2: For the complete answer specific to ARCore, see the update to the question post. The comments here may also be useful - Jordan specified that "the main part was to use the texture reader from the computer vision sdk example here".

    0 讨论(0)
  • 2020-12-10 19:01

    Since I'm not familiar with ARCore, I shall keep this generic.

    1. Load the byte array into your Texture2D using LoadRawTextureData() and Apply()
    2. Encode the texture using EncodeToJPG()
    3. Save the encoded data with File.WriteAllBytes(path + ".jpg", encodedBytes)
    0 讨论(0)
提交回复
热议问题