Search Unity

Question Cast Texture2D to XRCpuImage

Discussion in 'AR' started by DImadron, Apr 19, 2022.

  1. DImadron

    DImadron

    Joined:
    Jan 30, 2019
    Posts:
    2
    Hello.
    I am trying to create unit tests to test my QR code recognition module. I have a method that takes frames from a camera of type XRCpuImage. The frames are then converted to texture2d and decoded. In unit tests, I want to simulate incoming camera frames with pre-prepared frames, but I do not know how to bring files to the XRCpuImage type.

    Question: Is it possible to create XRCpuImage type objects from Texture 2d type objects that I will get from files on disk.

    Below I have given a method that processes frames from the camera. This is the method I would like to test.

    I used a translator, so mistakes are possible, sorry.

    Code (CSharp):
    1.  public IEnumerator DecodeQRImage(XRCpuImage image, Action<Result> callback)
    2.     {
    3.         var request = image.ConvertAsync(new XRCpuImage.ConversionParams
    4.         {
    5.             inputRect = new RectInt(0, 0, image.width, image.height),
    6.  
    7.             //outputDimensions = new Vector2Int(image.width / 2, image.height / 2),
    8.             outputDimensions = new Vector2Int(image.width, image.height),
    9.  
    10.             outputFormat = TextureFormat.RGB24,
    11.         });
    12.  
    13.         while (!request.status.IsDone())
    14.             yield return null;
    15.  
    16.         if (request.status != XRCpuImage.AsyncConversionStatus.Ready)
    17.         {
    18.             // Something went wrong
    19.             Debug.LogErrorFormat("Request failed with status {0}", request.status);
    20.  
    21.             // Dispose even if there is an error.
    22.             request.Dispose();
    23.             yield break;
    24.         }
    25.  
    26.         var rawData = request.GetData<byte>();
    27.         if (m_Texture == null)
    28.         {
    29.             m_Texture = new Texture2D(
    30.                 request.conversionParams.outputDimensions.x,
    31.                 request.conversionParams.outputDimensions.y,
    32.                 request.conversionParams.outputFormat,
    33.                 false);
    34.             Debug.Log("m_Texture=" + m_Texture.width + "x" + m_Texture.height);
    35.         }
    36.  
    37.         m_Texture.LoadRawTextureData(rawData);
    38.         m_Texture.Apply();
    39.  
    40.         byte[] barcodeBitmap = m_Texture.GetRawTextureData();
    41.         LuminanceSource source = new RGBLuminanceSource(barcodeBitmap, m_Texture.width, m_Texture.height);
    42.  
    43.         Result result = barCodeReader.Decode(source);
    44.         request.Dispose();
    45.         callback.Invoke(result);
    46.     }
     
  2. KyryloKuzyk

    KyryloKuzyk

    Joined:
    Nov 4, 2013
    Posts:
    1,143
    There is no cast from Texture2D to XRCpuImage available. XRCpuImage uses XRCpuImage.Api that handles conversions. It's possible to write a custom XRCpuImage.Api, but it may not be worth the effort.

    An easier solution than writing a custom XRCpuImage.Api would be to rewrite BarCodeReader so it can accept raw texture data. Then, you'll be able to feed raw texture data to BarCodeReader by calling Texture2D.GetRawTextureData() on the texture loaded from file.
     
    DImadron likes this.
  3. DImadron

    DImadron

    Joined:
    Jan 30, 2019
    Posts:
    2
    Thanks for your reply. I did as you advised!
     
    KyryloKuzyk likes this.