In this article I will show you how to scale an Image 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!!!
Java scaling options
There are always trade-offs in image scaling. Are you prepared to sacrifice image quality? Is speed or quality critical? Does final file size or memory usage matter?
There are a number of scaling options supported in Java. The AffineTransformOp class defines 3 scaling options (nearest neighbour, bilinear and bicubic). Bicubic scaling is slowest / best quality, nearest neighbour scaling is the opposite end of the spectrum and bilinear scaling is a middle compromise.
Other Java libraries may allow you to use different scaling algorithms. Our JDeli library supports all the standard Java options and also adds a really high quality Lancosz3 option (slightly slower than bicubic but much better quality).
Image Magick (which has a Java wrapper) has a very wide selection if you to try all possible options.
Are you a Java Developer working with Image files?
// Read an image
BufferedImage bufferedImage = JDeli.read(dicomImageFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(heicImageFile);
// Write an image
JDeli.write(bufferedImage, "heic", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(jpegImageFile);
// Write an image
JDeli.write(bufferedImage, "jpeg", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(jpeg2000ImageFile);
// Write an image
JDeli.write(bufferedImage, "jpx", outputStreamOrFile);
// Write an image
JDeli.write(bufferedImage, "pdf", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(pngImageFile);
// Write an image
JDeli.write(bufferedImage, "png", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(tiffImageFile);
// Write an image
JDeli.write(bufferedImage, "tiff", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(webpImageFile);
// Write an image
JDeli.write(bufferedImage, "webp", outputStreamOrFile);
Why do developers choose JDeli over free alternatives?
- Works with newer image formats such as AVIF, HEIC, JPEG XL, WEBP
- Better support than alternatives for JPEG, PNG, TIFF.
- Prevent JVM crashes caused by native code in other image libraries
- Better performance than other popular Java image libraries