Introduction

There are many ways to scale/resize images in Java, but it could be very tricky to make some of them produce satisfying output in terms of image quality and also, processing time. There are core Java approaches to this issue, but third-party libraries offer more optimization in the background so that we can scale an image without any specific additional configuration and get a satisfying result. In this article, we'll show you the easiest ways to scale, and still, keep image quality. 

From the core Java features, there are two main ways to scale image - using Graphics2D and Image.getScaledInstance(). They provide us with certain configuration options called "hints" that can help us balance between processing time and output image quality. They're not in the scope of this article.

From third-party libraries, we chose ImageScalr and Thumbnailator as libraries that would pick the best possible algorithm for you, based on input parameters - current image and target width/height. We tried both of them and the result was very good, comparing to core Java features where you have to tweak, but still not get what you want. Both of them support all file types that are supported by ImageIO.

Scale image using Thumbnailator

First, we need to add Maven dependency for this one:

<dependency>
    <groupId>net.coobird</groupId>
    <artifactId>thumbnailator</artifactId>
    <version>0.4.11</version>
</dependency>

This code sample shows how to scale image using Thumbnailator:

public class ThumbnailatorScaleExample {
    static BufferedImage scaleImage(BufferedImage originalImage, int targetWidth, int targetHeight) throws Exception {
        ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
        Thumbnails.of(originalImage)
            .size(targetWidth, targetHeight)
            .outputFormat("JPEG")
            .outputQuality(0.99)
            .toOutputStream(byteOutputStream);
        byte[] data = byteOutputStream.toByteArray();
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data);
        return ImageIO.read(byteArrayInputStream);
    }

    public static void main(String[] args) throws Exception {
        BufferedImage originalImage = ImageIO.read(new File("path/to/your/input/file.jpg"));
        BufferedImage outputImage = resizeImage(originalImage, 300, 300);
        ImageIO.write(outputImage, "jpg", new File("path/to/your/output/file.jpg"));
    }
}

Scale image using ImgScalr

The Maven dependency for ImgScalr:

<dependency>
    <groupId>org.imgscalr</groupId>
    <artifactId>imgscalr-lib</artifactId>
    <version>4.2</version>
</dependency>

This code sample shows how to scale image using ImgScalr library:

public class ImagescalrScaleExample {
    static BufferedImage scaleImage(BufferedImage originalImage, int targetWidth) throws Exception {
        return Scalr.resize(originalImage, targetWidth);
    }

    public static void main(String[] args) throws Exception {
        BufferedImage originalImage = ImageIO.read(new File("path/to/your/input/image.jpg"));
        BufferedImage outputImage = scaleImage(originalImage, 300, 300);
        ImageIO.write(outputImage, "jpg", new File("path/to/your/output/image.jpg"));
    }
}