Java Forum / GUI / February 2005
High Quality Resizing
Chris McMahon - 30 Jan 2005 20:43 GMT I'm trying to write a Java application that resizes JPEG images at very high quality. However, most of the code examples I've tried result in less than ideal results. I've tried the following:
1) AffineTransform trans = new AffineTransform(); trans.setToScale( 0.4, 0.4 ); AffineTransformOp op = new AffineTransformOp( trans, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); theNewImage = op.createCompatibleDestImage( theOrigImage, null ); op.filter( theOrigImage, theNewImage );
2) AffineTransform tx = new AffineTransform(); tx.scale( 0.4, 0.4 ); g.drawImage( theOrigImage, tx, null );
3) g.drawImage( theOrigImage.getScaledInstance( pNewWidth, pNewHeight, Image.SCALE_SMOOTH ), 0, 0, new Frame() );
4) RenderingHints renderHints = new RenderingHints( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); renderHints.put( RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g.setRenderingHints( renderHints );
g.drawImage( theOrigImage, 0, 0, pNewWidth, pNewHeight, null );
None of these gives satisfactory results. The more you zoom in, the more pronounced the problems become. The only thing I've used so far that does resizing nicely is Photoshop.
Are there any ways to resize through Java that I'm missing?
Andrey Kuznetsov - 30 Jan 2005 22:40 GMT > I'm trying to write a Java application that resizes JPEG images at very > high quality. However, most of the code examples I've tried result in > less than ideal results. ...
> Are there any ways to resize through Java that I'm missing? a) you can use java.awt.image.AreaAveragingScaleFilter. b) Imagero Reader has (down) scaling feature - advantages: high quality (like Photoshop), read direct in desired size, read once - create multiple scalings. this code sample shows how to scale images with Imagero Reader http://reader.imagero.com/tutorial/scale_image.html download Imagero Reader here http://reader.imagero.com/download/
 Signature Andrey Kuznetsov http://uio.dev.java.net Unified I/O for Java http://reader.imagero.com Java image reader http://jgui.imagero.com Java GUI components and utilities
Chris McMahon - 31 Jan 2005 02:42 GMT > a) you can use java.awt.image.AreaAveragingScaleFilter. > b) Imagero Reader has (down) scaling feature - advantages: [quoted text clipped - 4 lines] > http://reader.imagero.com/tutorial/scale_image.html > download Imagero Reader here http://reader.imagero.com/download/ I tried the following code using Imagero, but got a black border on the right and bottom of my image, and the image had bad artifacts:
File theSrcFile = new File( "test.jpg" );
BufferedImage theOrigImage = ImageIO.read( theSrcFile );
// create a new image of the smaller size BufferedImage theNewImage = new BufferedImage( theOrigImage.getWidth(), theOrigImage.getHeight(), theOrigImage.getType());
Graphics2D g = theNewImage.createGraphics();
JpegReader decoder = new JpegReader( theSrcFile, 1 );
final ImageProducerAdapter producer = (ImageProducerAdapter) decoder.getProducer(0);
final Image image = Toolkit.getDefaultToolkit().createImage(producer);
g.drawImage(image, 0, 0, null);
g.dispose();
File theTargetFile = new File( "test-new.jpg" );
ImageIO.write(theNewImage, "JPEG", theTargetFile);
Am I doing this wrong?
Andrey Kuznetsov - 31 Jan 2005 10:27 GMT > I tried the following code using Imagero, but got a black border on the > right and bottom of my image, and the image had bad artifacts: [quoted text clipped - 27 lines] > > Am I doing this wrong? yes, you doing it wrong. Didn't you read tutorial? Why do you loading image twice?
JpegReader decoder = new JpegReader(f, 1); final ImageProducerAdapter producer = (ImageProducerAdapter) decoder.getProducer(0); producer.setScaleX(0.4f); producer.setScaleY(0.4f); Image image = Toolkit.getDefaultToolkit().createImage(producer);
//ensure that image loaded ImageIcon icon = new ImageIcon(image);
BufferedImage bi = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB); Graphics g = bi.getGraphics(); g.drawImage(image, 0, 0, null); g.dispose();
File theTargetFile = new File( "test-new.jpg" ); ImageIO.write(bi, "JPEG", theTargetFile);
 Signature Andrey Kuznetsov http://uio.dev.java.net Unified I/O for Java http://reader.imagero.com Java image reader http://jgui.imagero.com Java GUI components and utilities
Chris McMahon - 31 Jan 2005 23:31 GMT > yes, you doing it wrong. > Didn't you read tutorial? [quoted text clipped - 18 lines] > File theTargetFile = new File( "test-new.jpg" ); > ImageIO.write(bi, "JPEG", theTargetFile); I did read the tutorial at the link you posted. That's what my previous code was based on.
I tried the code above, but it doesn't work. The image is scaled way smaller than 40% and the quality is very bad.
I'm not a graphics expert, so maybe I'm leaving something out?
Andrey Kuznetsov - 01 Feb 2005 00:49 GMT > I did read the tutorial at the link you posted. That's what my previous > code was based on. [quoted text clipped - 3 lines] > > I'm not a graphics expert, so maybe I'm leaving something out? hmm, could you send me one of your images? my email is here: http://reader.imagero.com/mailto.php
 Signature Andrey Kuznetsov http://uio.dev.java.net Unified I/O for Java http://reader.imagero.com Java image reader http://jgui.imagero.com Java GUI components and utilities
Chris McMahon - 02 Feb 2005 03:23 GMT >>I did read the tutorial at the link you posted. That's what my previous >>code was based on. [quoted text clipped - 6 lines] > hmm, could you send me one of your images? > my email is here: http://reader.imagero.com/mailto.php The problem was when I was writing the file. This method is what I use now to write the JPG files in high quality (for very high quality, try pQuality > 0.9):
private File write(BufferedImage pImage, String pFileName, double pQuality) throws IOException { // create File object for specified file name File theTargetFile = new File( pFileName );
// set the output ImageOutputStream out = ImageIO.createImageOutputStream( theTargetFile ); ImageWriter theWriter = (ImageWriter)ImageIO.getImageWritersBySuffix( FORMAT_JPEG ).next(); theWriter.setOutput( out ); // set the quality ImageWriteParam theParam = theWriter.getDefaultWriteParam(); theParam.setCompressionMode( ImageWriteParam.MODE_EXPLICIT ); theParam.setCompressionQuality( (float)pQuality ); // write file to output theWriter.write( null, new IIOImage( pImage, null, null ), theParam );
return theTargetFile; }
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|