Java

How To Resize/Convert Images In Java

admin  

Java comes by default with some classes to manipulate images. Those classes are located in java.awt package and are intended to be used for awt applications. For this reason they are not too rich in functionality to have enough options to control the resize quality. In the following snippet BufferedImage and ImageIO can be used to load an image in memory and to convert it to different format:

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;

...

public static void convertAnyImageToPng(String filename) 
                throws IOException
{
    BufferedImage bufferedImage = loadBufferedImage("toconvert.jpg");

    ImageIO.write(bufferedImage, "png", new File("converted.png"));
}

public static void convertAnyImageToJpg(String filename) 
                throws IOException
{
    BufferedImage bufferedImage = loadBufferedImage("toconvert.gif");

    ImageIO.write(bufferedImage, "jpg", new File("converted.jpg"));
}

public static BufferedImage loadBufferedImage(String filename)
                throws IOException
{
    File file = new File(filename);
    Image image = ImageIO.read(file);
    
    BufferedImage bufferedImage = new BufferedImage(
                    image.getWidth(null)
                    , image.getHeight(null)
                    , BufferedImage.TYPE_INT_RGB);

    Graphics2D g = bufferedImage.createGraphics();
    g.drawImage(image, null, null);
    // if the image is rendered in a awt gui we have to wait until
    //  until the rendering is finished(otherwise is not required):
    // waitForImage(bufferedImage); 
    
    return bufferedImage;		
}

If you want to select the jpeg quality compression you can use the ImageWriter class as shown in the following snippet:

public static void convertAnyImageToJpg(String filename) 
                throws IOException
{
    BufferedImage bufferedImage = loadBufferedImage("toconvert.png");

    ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next();
                
    ImageWriteParam param = writer.getDefaultWriteParam();
    param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); 
    param.setCompressionQuality(0.85F); // Highest quality is 1.0F
                
    ImageOutputStream  ios =  ImageIO.createImageOutputStream(new File("converted.img"));
    writer.setOutput(ios);
    writer.write(null, new IIOImage(destinationBufferedImage,null,null), param);
    writer.dispose();
                
    ios.close();
}

Imgscalr is a simple java native library that can be used to resize/manipulate images in memory. One of the best options is the fact that it uses different interpolation methods to have high quality resized images. To use it download it and add the imgscalr jar to you project. Here is a simple snippet to convert and resize a png image:

BufferedImage bufferedImage = loadBufferedImage("toconvert.jpg");

    destinationBufferedImage = Scalr.resize(bufferedImage
                    , Scalr.Method.QUALITY
                    , Scalr.Mode.FIT_EXACT
                    , 300
                    , 200
                    , Scalr.OP_ANTIALIAS);

    ImageIO.write(destinationBufferedImage
                    , ".png"
                    , new File("converted.png"));
    Java