Image Resizing

Hello everyone!

I have been working on a project where I need to show a picture on my device, but eventually these pictures might be an extremely high resolution and, as such, causes my app to crash on the target device. I have been trying to resize the image before loading it, but doing so through Texture2D doesn’t work. I attempted using other libraries such as libvips and imagemagick, both have worked in the editor, but naturally once I build to Android it fails on the device because it isn’t able to link to the DLLs. I am not very knowledgeable with building for Android, I tried to find versions of these libraries for Android but to my knowledge they don’t seem to exist. Has anyone been able to successfully use either of these libraries or some other that I haven’t been able to find to perform image processing on an Android device? I would greatly appreciate any help. Note that I cannot request the image to be resized beforehand since it comes directly from a wifi camera API, so I’m restricted to what this camera can provide.

Edit: I have a well defined target device that runs on Android 5.1.1 and ARMv7 architecture.

If you can save the camera’s image to an image file, then you can use NativeGallery.LoadImageAtPath with a specified maxSize to get an optimized Texture2D from that image file. You don’t need to use NativeGallery’s other features for LoadImageAtPath to work.

2 Likes

Great, thanks! I’ll try that. I was trying to find some lib to load it directly from memory (there are some really good and optimized ones for desktop but so far I only found Glide with an Android implementation).

Just in case you’re still interested in an approach using Texture2D, I’ve been using the following code successfully in a past project:

private static void Resize(Texture2D texture, int newWidth, int newHeight) {
    RenderTexture tmp = RenderTexture.GetTemporary(newWidth, newHeight, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default);
    RenderTexture.active = tmp;
    Graphics.Blit(texture, tmp);
    texture.Resize(newWidth, newHeight, texture.format, false);
    texture.filterMode = FilterMode.Bilinear;
    texture.ReadPixels(new Rect(Vector2.zero, new Vector2(newWidth, newHeight)), 0, 0);
    texture.Apply();
    RenderTexture.ReleaseTemporary(tmp);
}

(This code assumes that the texture you’re passing in is marked readable.)

3 Likes

Hey, thanks for the code. I think that code requires loading the texture, which is what causes the android device to crash (not enough memory for the uncompressed image), but thank you anyway! :slight_smile:

Hmm, I see. Out of curiosity: Is the data you’re getting from the camera API in some form compressed?

Yes. I download it from the camera as a JPG. The camera implements Google’s Open Spherical Camera API standard.

I tried it and it doesn’t seem to work. I checked the source code and on non-mobile it doesn’t use the size parameter, and running it in fact yielded the same resolution as a simple LoadImage sending the byte[ ] data. On android it uses an aar, which I couldn’t find the source code to, but running it on the device didn’t produce the expected result. I will try a bit more but for now it seems that unfortunately this library won’t solve the problem

I’ve assumed you were working on mobile devices only. That function’s editor functionality is limited, yeah. But maxSize must work on Android & iOS, I’ve never heard of it not working. Source code: UnityNativeGallery/.github/AAR Source (Android)/java/com/yasirkula/unity at master · yasirkula/UnityNativeGallery · GitHub

Main focus is Android, and that’s where I need the library to really work, if it doesn’t work on desktop that’s fine, I have other libraries that do (and the resolution is only a problem on mobile anyway). You’re right though, my mistake. It was a different problem on my end on Android and when I looked into the code and noticed on the desktop code that it wasn’t using the size parameter I incorrectly assumed that was the problem, sorry. I revised my code and now it is in fact working and resizing the image! Thanks a lot