How to Create a IARReferenceImage at runtime from a texture file?

Include the following details (edit as applicable):

  • Issue category: ARDK Documentation
  • Device type & OS version: Any
  • Host machine & OS version: Any
  • Issue Environment : On Device
  • Xcode version:
  • ARDK version: 2.4.1
  • Unity version: 2021.3

Description of the issue:

The IARReferenceImage documentation gives reference to:

  • Loading an image from a file
  • Loading an image from a Text asset containing the “bytes” for an image

But I cannot find an example of how to take a Texture File associated in the editor as a Texture2D and successfully load it. All attempts simply result in the extremely unhelpful “InvalidReferenceImage”

This is the code I have attempted to use, based on the documentation samples:

        private bool GetImageDataFromTexture(Texture2D trackedImage)
        {
            byte[] textureBytes = trackedImage.GetRawTextureData();

            try
            {
                IARReferenceImage trackedImageARReference =
                      ARReferenceImageFactory.Create
                      (
                        trackedImage.Name,
                        textureBytes ,
                        textureBytes .Length,
                        physicalImageWidth
                      );
            }
            catch (Exception)
            {
                return false;
            }

            return true;
        }

I even (for fun) tried to convert the bytes in to a TextAsset to more closely mirror the documentation example: (granted, ridiculous)

        private bool GetImageDataFromTexture(Texture2D trackedImage)
        {
            byte[] bytes = trackedImage.GetRawTextureData();
            string texAsString = Convert.ToBase64String(bytes);
            TextAsset imageText = new TextAsset(texAsString);

            try
            {
                IARReferenceImage trackedImageARReference =
                      ARReferenceImageFactory.Create
                      (
                        trackedImage.Name,
                        imageText.bytes,
                        imageText.bytes.Length,
                        physicalImageWidth
                      );
                AddTrackedImageToLibrary(trackedImage, trackedImageARReference);
            }
            catch (Exception)
            {
                return false;
            }

            return true;
        }

But no joy, even using the sample images used in the ARDK examples, the same result.

It would also be MORE helpful if there was more explanation as to WHY the image was rejected, such as:

  • Length does not match array (but why LENGTH has to be supplied when you already pass the array :S )
  • Image format was not recognised
  • Insufficient tracking points to detect
  • The odds are not in your favour.

Any ideas on how this might be achieved?

Hello and thanks for letting us know about this. I’m working on reproducing the error you’re seeing and submitting a request to update the documentation with accepted image formats for this process. I’ll keep you posted as updates come back to me.

1 Like

Update on this request: We’ve linked this with another similar issue reported on Discourse and are requesting updates to the documentation and to the error logs to make it more clear what the back end would like to see. In this case it looks like it wanted a .jpg but we’ll continue trying to improve clarity on this. Thank you again for bringing this to our attention