tags:

views:

140

answers:

1

I'm creating an applet that send pictures to a servlet.

The applet first action is to resize and compress the picture. I'm using ImageIO to load the picture into a BufferedImage. Next I resize the picture and then use ImageWriter to compress it in jpeg. That's work fine.

The problem is when the user send a very big pic(up to 70MP). The loading of the image in a BufferedImage exceeds the heap size and throws an "out of memory" exception.

How can I advoid that ?

Is it possible to compress a picture without fully load it in memory ? Maybe using stream only ? Or load subImage ?

Excuse me for my bad english, Thanks for your anwsers.

A: 

The short answer is "increase your heap size" but I take it you are really concerned about reading that much into memory, even if it is possible.

Since ImageIO.write() needs a RenderedImage, and the only implementation available is BufferedImage, and it reads into memory, I'd say your next best bet is to implement the RenderedImage interface with a class that can read parts of the image file on demand, and not the entire thing in memory. That may be terribly slow, depending on how you implement it and how the encoder accesses the image.

I suppose you could consider saving to disk and maybe invoking a command line program that can perform the translation. It would be kludgy, but it's possible some of the nice command-line tools out there can do these operations much more efficiently, perhaps not in memory.

Sean Owen