Search Unity

Question Taking camera snapshot with "TryGetLatestImage"

Discussion in 'AR/VR (XR) Discussion' started by sasob8, Aug 4, 2020.

  1. sasob8

    sasob8

    Joined:
    Jul 11, 2017
    Posts:
    25
    I have couple of questions regarding how TryGetLatestImage works...

    1. I have "TakeSnapshot" method connected with button (onclick). When i click the button first time it doesn't go inside "arCameraManager.TryGetLatestImage(out cameraImage)". Second time it works.

    Code (CSharp):
    1. private void TakeSnapshot() {
    2.             XRCameraImage cameraImage;
    3.  
    4.             if (arCameraManager.TryGetLatestImage(out cameraImage)) {
    5.                 StartCoroutine(ProcessImage(cameraImage));
    6.                 cameraImage.Dispose();
    7.             }
    8.         }
    9.  
    10.         IEnumerator ProcessImage(XRCameraImage image) {
    11.             var request = image.ConvertAsync(new XRCameraImageConversionParams {
    12.                 inputRect = new RectInt(0, 0, image.width, image.height),
    13.                     outputDimensions = new Vector2Int(image.width, image.height),
    14.                     outputFormat = TextureFormat.RGB24,
    15.                     transformation = CameraImageTransformation.MirrorY
    16.             });
    17.  
    18.             while (!request.status.IsDone())
    19.                 yield return null;
    20.  
    21.             if (request.status != AsyncCameraImageConversionStatus.Ready) {
    22.                 Debug.LogErrorFormat("Request failed with status {0}", request.status);
    23.                 request.Dispose();
    24.                 yield break;
    25.             }
    26.  
    27.             var rawData = request.GetData<byte>();
    28.  
    29.             Texture2D imageTexture = new Texture2D(
    30.                 request.conversionParams.outputDimensions.x,
    31.                 request.conversionParams.outputDimensions.y,
    32.                 request.conversionParams.outputFormat,
    33.                 false);
    34.  
    35.             imageTexture.LoadRawTextureData(rawData);
    36.             imageTexture.Apply();
    37.  
    38.             request.Dispose();
    39.  
    40.             snapshotImage.texture = imageTexture;
    41.         }
    2. Texture is 640x480 and rotated (landscape instead of portrait). How can i set resolution to native and rotate it?

    3. If i use "arCameraManager.frameReceived" resolution is correct, but this is not useful, cause frame is received every frame (i think).

    Code (CSharp):
    1. private void OnEnable() {
    2.             arCameraManager.frameReceived += OnFrameReceived;
    3.         }
    4.  
    5.         private void OnFrameReceived(ARCameraFrameEventArgs obj) {
    6.             var tex = obj.textures[0];
    7.         }

    Help please :) ... Thank you
     
  2. sasob8

    sasob8

    Joined:
    Jul 11, 2017
    Posts:
    25
    Bytheway code is from official help: https://docs.unity3d.com/Packages/com.unity.xr.arfoundation@4.0/manual/cpu-camera-image.html
    I have "TakeSnapshot" instead of "GetImageAsync"...
    I think that TryGetLatestImage is async (it doesn't receive image on the same frame). But if that is true, you must call "GetImageAsync" at least 2 times... Which is wierd.
    I'm confused here.

    Edit:
    I also updated ARFoundation, subsytem and ARCore, and i used:
    arCameraManager.TryAcquireLatestCpuImage(out XRCpuImage cameraImage)
    ... no difference.
     
    Last edited: Aug 4, 2020