How to resize an image
Java Image IO provides pluggable architecture for accessing images transparently.
It is simple, flexible and allows us to plugin various image readers and writers with ease.
The following example shows resizing an image using Java Image IO.
package com.bethecoder.tutorials.imageio;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ResizeImageTest {
/**
* @param args
* @throws IOException
*/
public static void main ( String [] args ) throws IOException {
File file = new File ( "C:/Temp/stpatricks_08.gif" ) ;
BufferedImage image = ImageIO.read ( file ) ;
int toWidth = 124 ;
int toHeight = 44 ;
BufferedImage resizedImage = new BufferedImage (
toWidth, toHeight, BufferedImage.TYPE_INT_ARGB ) ;
Graphics2D graphics = resizedImage.createGraphics () ;
graphics.drawImage ( image, 0 , 0 , toWidth, toHeight, null ) ;
graphics.dispose () ;
ImageIO.write ( resizedImage, "png" , new File ( "c:/Temp/myresized_image.png" )) ;
}
}
It gives the following output,