Programmatic Image Resizing in Android, Memory Issues

Status
Not open for further replies.

dmitriyk

Ars Tribunus Angusticlavius
8,804
Days, I've spent working on this. Weeks, perhaps. Literally. :(

So I've got an image on an SD card that more than likely came out of the built-in camera. I want to take that image and downsample it to an arbitrary size (but always smaller and never larger). My code uses standard Android Bitmap methods to decode, resize, recompress, and save the image. Everything works fine as long as the final image is smaller than 3MP or so. If the image is larger, or if I try to do several of these at once, the application crashes with an OutOfMemoryError. I know why that's happening, and I know it's happening for a perfectly legitimate reason, I just want it to not happen anymore.

Look, I'm not trying to launch a rocket here. All I want to do is resize a camera image and dump it to an OutputStream or even a temporary file. Surely someone out there must have done such a thing. I don't need you to write my code for me, and I don't need my hand held. But between my various programming abortions and days of obsessed Googling, I don't even know which direction to head in. Roughly speaking, does anyone know how to decode a JPEG, downsample it, re-compress it in JPEG, and send it out on an OutputStream without allocating a massive amount of memory?

Please? :)
 

dmitriyk

Ars Tribunus Angusticlavius
8,804
Right, I've seen that. The inSampleSize parameter does avoid the issue quite handily. The problem is that with inSampleSize = 1, the SDK returns the full-sized image (resulting in a crash); with inSampleSize = 2, the SDK returns an image that is 50% of the original size, which - most of the time - is too small. There are no valid values between those two, so when it comes to resampling the image to - f'rinstance - 80%, I'm just screwed.
 

kuya

Ars Scholae Palatinae
881
from wikipedia's jpeg entry:
598px-JPEG_process.svg.png


so from a 10000 foot overview, i think you could:
1) entropy decode
2) dequantize
3) apply inverse DCT to the 8x8 block
4) downsample from 8x8 to something less than that (pick an interpolation algorithm, maybe bilinear)
5) apply DCT to a new 8x8 block (necessarily involves more than one downsampled block)
6) quantize
7) entropy encode to a new downsampled .jpeg
8) PROFIT!
 

dmitriyk

Ars Tribunus Angusticlavius
8,804
Really? Really?
Android is going to make me re-implement the JPEG compression algorithm just to resize a camera image? On an ARM CPU running interpreted Java bytecode? Really?

I guess I'm desperate enough to bust out the NDK for this, but... wow. That sucks.
I'll need some time to come to terms with this. Maybe in the meantime either Google or the Android community will come up with something less soul-crushing.

I still can't believe I have to do this. It's just resizing a freaking JPEG! :(
 

topham

Ars Legatus Legionis
13,756
Subscriptor
dmitriyk":3uhpsg7f said:
Really? Really?
Android is going to make me re-implement the JPEG compression algorithm just to resize a camera image? On an ARM CPU running interpreted Java bytecode? Really?

I guess I'm desperate enough to bust out the NDK for this, but... wow. That sucks.
I'll need some time to come to terms with this. Maybe in the meantime either Google or the Android community will come up with something less soul-crushing.

I still can't believe I have to do this. It's just resizing a freaking JPEG! :(

It's just resizing a JPEG image in a memory constrained environment.

You should cheat; take the image down to 50% and apply bilinear interpolation to resize the image larger; lower image quality, but perhaps not enough to be an issue.
 

redleader

Ars Legatus Legionis
36,024
dmitriyk":36mwpo0p said:
Really? Really?
Android is going to make me re-implement the JPEG compression algorithm just to resize a camera image? On an ARM CPU running interpreted Java bytecode? Really?

No, thats just one (very common) solution to JPEG resizing. If thats too complicated for you, why not just write a bilinear downsampler? These can be written to use extremely small amounts of memory with JPEG and generally look pretty good. IIRC for an MxN pixel image resized to AxB pixels you can get pretty close to Mx(N+8) +(AxB)*3 bytes of total memory, and possibly much less if you dont' mind overwriting the original image as you go.

Theres tons of example c-ish code for doing this out there. Should be quite simple to convert to Java.
 
The BitMapFactory looks like it is only for loading an image into memory. If the bitmap exceeds the memory budget then you are hosed, bitmaps chew up memory.

I didn't see anything in the API that will help you. You will need to write your own that reads from the file in chunks and does the transformation with as little memory as possible, not an easy task.

This is an embedded platform and not a desktop, every byte counts. Welcome to the joys of embedded development ;)
 

dmitriyk

Ars Tribunus Angusticlavius
8,804
noise":203swy4s said:
The BitMapFactory looks like it is only for loading an image into memory. If the bitmap exceeds the memory budget then you are hosed, bitmaps chew up memory.

I didn't see anything in the API that will help you. You will need to write your own that reads from the file in chunks and does the transformation with as little memory as possible, not an easy task.

This is an embedded platform and not a desktop, every byte counts. Welcome to the joys of embedded development ;)
Yay...

topham":203swy4s said:
You should cheat; take the image down to 50% and apply bilinear interpolation to resize the image larger; lower image quality, but perhaps not enough to be an issue.
I like it! It's certainly not a long-term solution, but it should work just fine for a little while while I figure out... that. All that above.
 
Status
Not open for further replies.