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".
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…