how to reduce image memory usage, thanks

There is a photo which is 5Mega pixels.When i read it in to the memory as a bufferedimage, the memory usage increase more than 10M. How can we use less memory, for instance 2M, to display this photo?thank you!
[237 byte] By [bert_suna] at [2007-11-26 18:40:02]
# 1
Do you want to have image with the same size/color depth as original picture ? If so, then looks like there is no way to achieve this. Otherwise, you can use 2d api to draw it into the image with less color depth and (or) scaled by some factor.
AlexeyUshakova at 2007-7-9 6:14:04 > top of Java-index,Security,Cryptography...
# 2
I don't want the displaying image with the same color/depth as the orgin image (the displaying image will be not as high quality as the orgin one).Could you give me some tips about how to use the 2d api to draw image with less memory usage?Thank you:)
bert_suna at 2007-7-9 6:14:04 > top of Java-index,Security,Cryptography...
# 3

You can draw original image into lower color depth image using code like this:

BufferedImage origImage = ImageIO.read(new File(args[0]));

BufferedImage lowImage = new BufferedImage(

origImage.getWidth(), origImage.getHeight(),

BufferedImage.TYPE_USHORT_555_RGB);

lowImage.getGraphics().drawImage(origImage,0,0,null);

So, if there is no more usage of the origImage it will be collected and you will have lower memory usage. I've checked the approach above with 1932X2576 image and it saves me 5mb memory comparing with just using origImage.

AlexeyUshakova at 2007-7-9 6:14:04 > top of Java-index,Security,Cryptography...
# 4
thanks, i will try this
bert_suna at 2007-7-9 6:14:04 > top of Java-index,Security,Cryptography...
# 5

If you're ok with subsampling you can try this approach which will reduce your memory consumption by 75%.

ImageInputStream iis = ImageIO.createImageInputStream(new File("d:/IMG_5167.JPG"));

Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);

ImageReader reader = iter.next();

reader.setInput(iis);

ImageReadParam param = reader.getDefaultReadParam();

param.setSourceSubsampling(2, 2, 0, 0);

BufferedImage image = reader.read(0, param);

The added value here is that you don't load the full image at any time, whereas all the other solutions that you can find involve loading the full image first which means using the full memory size of the image for a certain period of time.

Rodney_McKaya at 2007-7-9 6:14:04 > top of Java-index,Security,Cryptography...