Loading Image Target from persistentdatapath [Android]

+1 vote
asked Apr 15 by creartivity (210 points)
edited Apr 15 by creartivity

Hello!

I'm triyng to load target images from unity persistentdatapath. It works as expected in Unity Editor but does not work on Android Build. On android the behaviour is very strange, it sometimes works, but only for the fist app launch after installing it. Most times it doesn't work at all.

What i mean with doesnt work is:

- The image is present in persistentdatapath folder

- I can load it with other methods correcly (as image/sprite ecc)

- The target is instatiated correctly

- The target recognition does not work (only in build)

MY CODE (working with TargetOnTheFly sample)

private IEnumerator CreateInstantTarget(byte[] fileBytes, string targetID)
        {     

            string photoPath =   Path.Combine(Application.persistentDataPath + "/", targetID + ".jpg");
           //CACHE IF NOT EXISTS
            if (!File.Exists(photoPath))
            {
                File.WriteAllBytes(photoPath, fileBytes);
                Debug.Log("Instant Target cached");
            }

            yield return new WaitForEndOfFrame();

            CreateImageTarget(targetID, photoPath);
        }

        private void CreateImageTarget(string targetName, string targetPath)
        {
            GameObject imageTarget = new GameObject(targetName);
            currentActiveTargets.Add(imageTarget);
            var controller = imageTarget.AddComponent<ImageTargetController>();
            controller.ImageFileSource.Scale = 1;
            controller.SourceType = ImageTargetController.DataSource.ImageFile;
            controller.ImageFileSource.PathType = PathType.Absolute;

           controller.ImageFileSource.Path = "file://" + targetPath;

            Debug.Log("LOADING PATH: " + controller.ImageFileSource.Path);

            controller.ImageFileSource.Name = targetName;
            controller.Tracker = Filter;

            controller.TargetFound += Controller_TargetFound;
            controller.TargetLost += Controller_TargetLost;        
        }

1 Answer

0 votes
answered Jul 30 by spaceagent (1,290 points)

Same issue here with EasyAR 4.3. What I get in the console is:

Exception: invalid buffer

I have checked that the file is readable, and that the FileUtil.LoadFile does indeed get the bytestream. It seems that this line generates an empty _return_value

Detail.easyar_ImageTarget_createFromTargetData(buffer.cdata, out _return_value_);

Any idea how to get around this issue on Android?

commented Jul 30 by kenn (15,150 points)
Have checked if the sample works?
commented Jul 30 by spaceagent (1,290 points)

Hello @kenn,

I've tested the sample and I get very strange behaviour. It seems that all of the sample images work, but only a part of the images tested go beyond that "createFromTargetData" method.

Please keep in mind that all of the following images work in the editor, but only two of them work on Android. The loading path is the StreamingAssets folder, just like in the example:

https://cdn.shopify.com/s/files/1/0272/4770/6214/articles/when-do-puppies-start-walking.jpg?v=1593020034 - works

https://dogtime.com/assets/uploads/2018/10/puppies-cover-1280x720.jpg - des not work

https://images.squarespace-cdn.com/content/v1/53a60116e4b0488fb14d69d8/1597336605370-5BEYBPCYKAZEMWC4J7KQ/image-asset.jpeg?format=1000w - works

https://i.guim.co.uk/img/media/f50d146515d8f8b50e819d672918938ace28795c/0_133_3991_2395/master/3991.jpg?width=1200&height=1200&quality=85&auto=format&fit=crop&s=f5d1910f1a752dfab3181cd180512e7e - does not work

For one of my images I got it to work by converting it to PNG. But another one did not work after converting to PNG.

Please let me know if there is a workaround to bypass or replace the ImageHelper.decode method.

commented Jul 30 by kenn (15,150 points)
Same error here. Thank you for the images and explanation!

We will analyze the error and give you feedback next week.
commented Jul 30 by spaceagent (1,290 points)
Hello @kenn,

I just tested that if I save my jpeg files with the progressive option NOT checked (I used GIMP for this), then easyAR will load the image.

Hope this helps.

I'd really appreciate it if you find a solution to this issue.

Best.
commented Aug 1 by kenn (15,150 points)
We have found the cause and fixed the problem. Thank you again!

Unfortunately the fix is inside the EasyAR library so it won't be available since the next release (scheduled this quarter).

To workaround, disable the progressive option will definitely help, shrink the image size may also help in some conditions.
commented Nov 8 by kenn (15,150 points)
Update: fix included in version 4.4.
Welcome to EasyAR SDK Q&A, where you can ask questions and receive answers from other members of the community.
...