1 year ago

#308991

test-img

gfrung4

Method to decode partial JPEG byte array in Android

I'm working on an Android app where I have a byte array of a JPEG image and need to display it. Currently, I'm decoding it in Android with this code:

Bitmap bitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
ImageView imageDisplay = findViewById(R.id.image_display);
imageDisplay.setImageBitmap(bitmap);

This works great, if I have the full image. However, if I take the byte array and chop off, say, the final 25% of it, BitmapFactory.decodeByteArray returns null. According to the documentation, the method BitmapFactory.decodeByteArray returns:

The decoded bitmap, or null if the image could not be decoded.

This surprises me because I thought partial JPEGs could be decoded. As an experiment, I opened a JPEG in a hex editor and chopped it down to 75%, 50%, and 25% of its original size, and, in all cases, Windows was still able to show the image (I was able to open each image in Paint.NET too!)

standard JPEG example

If I saved the image as a "progressive" JPEG, the performance was even better! Below, the partial-25.jpg file is only the first quarter of the original file's bytes, but it displays quite well. If you zoom in, there is less detail, but it's pretty good!

progressive JPEG example

I'm looking for this same behavior in Android. When encountering a JPEG byte array that ends prematurely, I want to draw whatever I have so far.

Is there some way to get BitmapFactory.decodeByteArray to give me this behavior, or is there another Android included method that could do so? If not, I'm willing to use a library for it.

(The reason I'm doing this is because the image data is actually arriving very slowly (think bytes per second slowly), and I'd like to draw it as it comes in, rather than waiting until the end to draw the whole thing.)

android

bitmap

jpeg

bitmapfactory

0 Answers

Your Answer

Accepted video resources