Posted By:
Anonymous
Posted On:
Thursday, March 22, 2001 04:11 AM
I want to resize an JPG image that has been uploaded from a browser. I use the JPEGCodec and decode the file to a BufferedImage. Now I want to resize it. At first I made a Graphics2D object and tried to perform an AffineTransform on it. Then I encode to JPEG and look at the result. But I end up getting the original image with a thumbnial version (the scale I wanted) on top of it. I still have the full size of the BufferedImage/Graphics2D. See some code: // DECODE from JPEG JPEGImageDecoder decode = JPEGCodec.createJPEGDecoder(in); BufferedImage bi = null; try { bi = decode.decodeAsBufferedImage(); height = bi.getHeight(); width = bi.getWi
More>>
I want to resize an JPG image that has been uploaded from a browser.
I use the JPEGCodec and decode the file to a BufferedImage.
Now I want to resize it.
At first I made a Graphics2D object and tried to perform an AffineTransform on it. Then I encode to JPEG and look at the result. But I end up getting the original image with a thumbnial version (the scale I wanted) on top of it. I still have the full size of the BufferedImage/Graphics2D.
See some code:
// DECODE from JPEG
JPEGImageDecoder decode = JPEGCodec.createJPEGDecoder(in);
BufferedImage bi = null;
try {
bi = decode.decodeAsBufferedImage();
height = bi.getHeight();
width = bi.getWidth();
} catch (IOException ie) {
System.out.println("Decode error");
ie.printStackTrace();
}
System.out.println("Height : " + height );
System.out.println("Width : " + width );
// Make a graphics element
Graphics2D g2d = bi.createGraphics();
// Scale
Container cont = new Container();
AffineTransform trans = new AffineTransform();
trans.scale(.5, .5);
// Add the scaled image
g2d.drawImage((Image)bi, trans, cont);
/** @todo Here I want to crop the image before I encode it to JPEG */
// ENCODE to JPEG
JPEGImageEncoder encode = JPEGCodec.createJPEGEncoder(out);
try {
encode.encode(bi);
}
catch (ImageFormatException ife) {
ife.printStackTrace();
}
catch (IOException ie) {
ie.printStackTrace();
}
System.out.println("File written...");
- Svein Are (sag@portalen.no)
<<Less