Cannot Set File/Image to RunTimeImageSource of Object Tracker

Viewed 455

I am trying to dynamically create image targets at runtime. For that, I am capturing screen from vuforia AR camera and saving it in Application.persistentDataPath. Then I am creating a dataset trackable behavior following the script provided in How to Create and Load Targets in Unity.
My code for capturing the screen:

            var datapath = Application.persistentDataPath;
            
            datapath = Path.Combine(datapath, "image.jpg");
        
            int width = Screen.width;
            int height = Screen.height;
            Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);

            // Read screen contents into the texture
            tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
            tex.Apply();
            byte[] bytes = tex.EncodeToJPG();

            File.WriteAllBytes(datapath, bytes);

Code for creating trackable behavior:


            var objectTracker = TrackerManager.Instance.GetTracker<ObjectTracker>();
            var runtimeImageSource = objectTracker.RuntimeImageSource;
            //Problem here
            bool result = runtimeImageSource.SetFile(VuforiaUnity.StorageType.STORAGE_ABSOLUTE, datapath, 0.15f, "Temp");
            // create a new dataset and use the source to create a new trackable
            var dataset = objectTracker.CreateDataSet();
            
            var trackableBehaviour = dataset.CreateTrackable(runtimeImageSource, "Temp");
            // add the DefaultTrackableEventHandler to the newly created game object
            trackableBehaviour.gameObject.AddComponent<DefaultTrackableEventHandler>();

This works well in my windows laptop but not on my android phone. After some debugging, I found out that bool result is being false and as a result, var trackableBehaviour is becoming null.

Edit: The issue is not with write permissions. I can see the images getting saved at the persistentDataPath. The problem is with setting runtime image source to the saved image.

Edit 2: I tried using the texture directly instead of file like this bool result = runtimeImageSource.SetImage(tex, 1f, "Temp");. Still result is false. Therefore, I believe the issue is not with path resolution but something else.

1 Answers
  • First, Try to use VuforiaUnity.StorageType.STORAGE_APP instead of absolute.
  • 2nd: try bool result = DataSet.Exists (String path, VuforiaUnity.StorageType storageType) and to debug which path will return true on phone enter image description here
  • Application.persistentDataPath return the path without back slash, which means try to add"/image.jpg" instead of image.jpg, this may be the issue as well.
Related