Processing progressive jpeg images in Libgdx

I am trying to implement a simple Facebook profile image request function in a game that is being developed with the Libgdx engine. I would like to receive the requested image and ultimately display it on the screen. It is advisable that this work both on my desktop and on Android.

The problem occurs when I try to create a Pixmap object using a profile image, since the profile image is a progressive jpeg that libgdx cannot load. Code example:

inStream = new URL(url).openStream(); byte[] buffer = new byte[1024 * 200]; int readBytes = 0; while (true) { int length = inStream.read(buffer, readBytes, buffer.length - readBytes); if (length == -1) break; readBytes += length; } Pixmap pixmap = new Pixmap(buffer, 0, readBytes); 

Since the image is uploaded, I have no way to convert the image to the usual jpeg or png format using software. I tried to use the ImageIO package (among some others) to decode the image, but it is also not capable of handling progressive jpeg. I could not find a solution that would work on both platforms.

Any suggestions to overcome this problem? At least if I could handle this for Android, maybe I could come up with something else to implement on the desktop.

+6
source share
3 answers

This is not an exact solution to the problem, but I did this:

I decided to place a static image in the Desktop implementation and upload profile images for the Android implementation.

First I get the image and decode it as a bitmap. Code example:

 URL url = new URL(src); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.connect(); InputStream input = connection.getInputStream(); Bitmap myBitmap = BitmapFactory.decodeStream(input); 

Then I create an array of bytes from the Bitmap object as follows:

 ByteArrayOutputStream outStream; int size = myBitmap.getWidth() * myBitmap.getHeight() * 2; while (true) { outStream = new ByteArrayOutputStream(size); if (myBitmap.compress(Bitmap.CompressFormat.PNG, 0, outStream)) break; size = size * 3 / 2; } byte[] buffer = outStream.toByteArray(); 

Now you can use the byte array to create the Pixmap object and load the image.

+1
source

Use the gdx-image extension to handle progressive jpg

0
source

Source: https://habr.com/ru/post/947645/


All Articles