Java Create High Quality Thumbnail Images using Imgscalr

Tags: Imgscalr BufferedImage ImageIO Image File Resize Image Thumbnail Scalr

In this tutorial we learn how to create high quality thumbnail images using the Scalr class of Imgscalr Java library.

How to add Imgscalr library to the Java project

To use the Imgscalr library in the Gradle build project, add the following dependency into the build.gradle file.

implementation 'org.imgscalr:imgscalr-lib:4.2'

To use the Imgscalr library in the Maven build project, add the following dependency into the pom.xml file.

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

To have more information about the Imgscalr library you can visit the project repository at github.com/rkalla/imgscalr

How to create high quality thumbnail images in Java

The Imgscalr library provides method Scalr.resize() to allow resizing a given image. By using the Scalr.resize() method we can create high quality thumbnail images with Scalr.Method.ULTRA_QUALITY parameter. In the following Java example code we show you how to create a high quality thumbnail image from a given image file.

HighQualityThumbnailImageExample.java

import org.imgscalr.Scalr;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class HighQualityThumbnailImageExample {
    public static void main(String... args) {
        String originalFilePath = "D:\\TestData\\logo.jpg";
        String thumbnailFilePath = "D:\\TestData\\thumbnail.jpg";
        int thumbnailSize = 64;

        createThumbnailImage(originalFilePath, thumbnailFilePath, thumbnailSize);
    }

    public static void createThumbnailImage(String originalFilePath, String targetFilePath, int thumbnailSize) {
        try {
            File sourceFile = new File(originalFilePath);
            BufferedImage originalImage = ImageIO.read(sourceFile);

            BufferedImage thumbnailImage = Scalr.resize(originalImage, Scalr.Method.ULTRA_QUALITY, thumbnailSize);

            File thumbnailFile = new File(targetFilePath);
            ImageIO.write(thumbnailImage, "jpg", thumbnailFile);

            originalImage.flush();
            thumbnailImage.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
The output is:

Java Create High Quality Thumbnail Images using Imgscalr

Happy Coding 😊