Nirali Nirali is a Java Developer at IDR Solutions and also enjoys being part of the business development team. She is currently studying for her MSc Computing at De Montfort University.

Image Scaling in Java

1 min read

magnifying glass

This article is part of our series index Image Processing in Java. Other articles include Image Rotation In Java,  Image Scaling options in Java.

The simplest way to scale an image in Java is to use the AffineTransformOp class. You can load an image into Java as a BufferedImage and then apply the scaling operation to generate a new BufferedImage. You can use Java’s ImageIO or a third-party image library such as JDeli to load and save the image. We have used JDeli in our example below because it works with all JPEG files and the widest range of filetypes.

Step:1 Load an image file into Java as a BufferedImage

You can load an image file using Java ImageIO (which is built into Java )

BufferedImage image = ImageIO.read(new File("C:\\path\\to\\image.jpg"));

You can also load image files using other open source or commercial libraries. Here is an example using JDeli Image library.

BufferedImage image = JDeli.read(new File("C:\\path\\to\\image.jpg"));

Step:2 Scale the BufferedImage in Java

This code will scale the image by a factor of 2. 
final int w = image.getWidth();
final int h = image.getHeight();
BufferedImage scaledImage = new BufferedImage((w * 2),(h * 2), BufferedImage.TYPE_INT_ARGB);
final AffineTransform at = AffineTransform.getScaleInstance(2.0, 2.0);
final AffineTransformOp ato = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC);

scaledImage = ato.filter(image, scaledImage);

final Scale scaler = new Scale(2);
BufferedImage scaledImage= scaler.apply(image);

Step:3 Resave the BufferedImage to a new File

You can save an image file using Java ImageIO (which is built into Java )

ImageIO.write(scaledImage, "JPG", new File("C:\\path\\to\\scaledImage.jpg"));

You can also save image files using other open source or commercial libraries. Here is an example using JDeli Image library.

JDeli.write(scaledImage, "JPG", new File("C:\\path\\to\\scaledImage.jpg"));

And that is all you need to do!!!

If you are looking for a simple solution using just PNG, TIFF or JPG, ImageIO works well. If you want to use less common image file formats, have more control over the image output then you will need to look at third-party libraries such as our JDeli Image library. Please feel free to post your suggestions for other libraries.

If you want to learn more about image scaling in Java, we have a detailed article on Image Scaling Options in Java.

 

IDRsolutions develop a Java PDF Viewer and SDK, an Adobe forms to HTML5 forms converter, a PDF to HTML5 converter and a Java ImageIO replacement. On the blog our team post anything interesting they learn about.

Nirali Nirali is a Java Developer at IDR Solutions and also enjoys being part of the business development team. She is currently studying for her MSc Computing at De Montfort University.

3 reasons Java developers switch to JDeli from ImageIO

ImageIO is build into the JDK and provides basic image support in Java. JDeli is a commercial image library for Java Developers from IDRsolutions....
Mark Stephens
1 min read

Leave a Reply

Your email address will not be published. Required fields are marked *

IDRsolutions Ltd 2019. All rights reserved.