Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling progressive jpeg images in Libgdx

I am trying to implement a simple Facebook profile picture request functionality into a game that is being developed with Libgdx engine. I would like to get the requested picture and eventually display it on the screen. Preferably, I want this to work both on my desktop and Android implementation.

The problem occurs when I try to create a Pixmap object using the profile picture, as the profile picture is a progressive jpeg, which libgdx cannot load. Sample code:

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 a downloaded one, I don't have the option to convert the image to a regular jpeg or png format using a software. I've tried using ImageIO package(among some others) to decode the image, but it is not able to handle the progressive jpeg either. I wasn't able to find a solution that would work for both platforms.

Any suggestions to overcome this problem? At least, If I could handle it for Android, maybe I could think of something else for the desktop implementation.

like image 451
Emre Dirican Avatar asked Jun 19 '13 10:06

Emre Dirican


1 Answers

It is not the exact solution to the problem, but what I did is;

I've decided to put a static image in Desktop implementation, and download the profile pictures for Android implementation.

First, I get the picture and decode it as Bitmap. Sample code:

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 a byte array 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 one can use the byte array to create a Pixmap object and load the image.

like image 165
Emre Dirican Avatar answered Oct 21 '22 01:10

Emre Dirican