How to save the raw CPU image in unity into JPG file while the app is running on mobile?

Viewed 26

My Unity setup is as follows: enter image description here

My code for getting the raw image in unity is as follows:

using System;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
using System.IO;

public class ImageCapture : MonoBehaviour
{
    
    // Start is called before the first frame update
    public ARCameraManager CameraManager
    {
        get => _cameraManager;
        set => _cameraManager = value;
    }

    [SerializeField]
    [Tooltip("The ARCameraManager which will produce camera frame events.")]
    private ARCameraManager _cameraManager;


    public AROcclusionManager OcclusionManager
    {
        get => _occlusionManager;
        set => _occlusionManager = value;
    }

    [SerializeField]
    [Tooltip("The AROcclusionManager which will produce depth textures.")]
    private AROcclusionManager _occlusionManager;

    public RawImage RawImage
    {
        get => _rawImage;
        set => _rawImage = value;
    }

    [SerializeField]
    [Tooltip("The UI RawImage used to display the image on screen.")]
    private RawImage _rawImage;

     void Update()
    {
        if (OcclusionManager.TryAcquireEnvironmentDepthCpuImage(out XRCpuImage image))
        {
            using (image)
            {
                // Use the texture.
                UpdateRawImage( image);
                Debug.Log("Update image");
            }
        }
    }

    private unsafe static void UpdateRawImage( XRCpuImage cpuImage)
    {
        Debug.Log("this function is called");
        var conversionParams = new XRCpuImage.ConversionParams
        {
            // Get the entire image.
            inputRect = new RectInt(0, 0, cpuImage.width, cpuImage.height),

            // Downsample by 2.
            outputDimensions = new Vector2Int(cpuImage.width / 2, cpuImage.height / 2),

            // Choose RGBA format.
            outputFormat = TextureFormat.RGBA32,

            // Flip across the vertical axis (mirror image).
            transformation = XRCpuImage.Transformation.MirrorY
        };

        // See how many bytes you need to store the final image.
        int size = cpuImage.GetConvertedDataSize(conversionParams);

        // Allocate a buffer to store the image.
        var buffer = new NativeArray<byte>(size, Allocator.Temp);

        // Extract the image data
        cpuImage.Convert(conversionParams, new IntPtr(buffer.GetUnsafePtr()), buffer.Length);

        // The image was converted to RGBA32 format and written into the provided buffer
        // so you can dispose of the XRCpuImage. You must do this or it will leak resources.
        cpuImage.Dispose();
        

        buffer.Dispose();

        


    }

}

I now want to save whatever data is there in the buffer to JPG file to later use it.

I have tried EncodeToJPG but the app crashes upon building it into mobile device to initiate AR camera.

Please help.

0 Answers
Related