Memory efficient jpeg crop?

Hi,

I need to do 2 things with some big jpeg images:

1) get its height/width

2) create a crop, extract some part of the image.

I'd like to know if you know any way (or library) that could create a crop (a 300x300 subimage) out of a jpeg image. It is absoluely necessary that the process be memory efficient.

Loading the whole image into memory takes , well, too much RAM.

Any solution?

BTW, this is what I do today to get the image size and the crop:

// load image

BufferedImage image = ImageIO.read(fileOriginal);

int bigWidth = image.getWidth();

int bigHeight = image.getHeight();

// create crop

BufferedImage detail =new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);

Graphics2D g = detail.createGraphics();

g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

g.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);

g.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);

g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);

g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);

g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

g.drawImage(image, 0, 0, imageWidth, imageHeight,null);

//... some code to get x, y ...

detail = detail.getSubimage(x, y, edge, edge);

// save thumbnail image to file

out =new BufferedOutputStream(new FileOutputStream(file));

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(detail);

param.setQuality(compressionRatio,false);

encoder.setJPEGEncodeParam(param);

encoder.encode(detail);

[2293 byte] By [urddda] at [2007-11-26 15:04:44]
# 1

This is a sample for reading part of a JPEG from the disk:

ImageReader reader = ImageIO.getImageReadersByFormatName("JPEG").next();

ImageInputStream iis = ImageIO.createImageInputStream(new FileInputStream("c:\\IMG_3477.JPG"));

reader.setInput(iis);

System.out.println("width = " + reader.getWidth(0));

System.out.println("height = " + reader.getHeight(0));

ImageReadParam param = reader.getDefaultReadParam();

param.setSourceRegion(new Rectangle(100, 100, 100, 100));

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

Rodney_McKaya at 2007-7-8 8:54:28 > top of Java-index,Security,Cryptography...
# 2
I can't beleive that it works soooo well !Thank you so much!
urddda at 2007-7-8 8:54:28 > top of Java-index,Security,Cryptography...
# 3
You need to have more faith.That's what we're here for...
Rodney_McKaya at 2007-7-8 8:54:28 > top of Java-index,Security,Cryptography...