Java Padding Image File using Imgscalr

Tags: Imgscalr BufferedImage ImageIO File Image File Color Pad Image Scalr

In this Java tutorial we learn how to add a border around an image file using the Scalr class of Imgscalr 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 pad an image in Java

The Imgscalr library provides the Scalr.pad() method to apply padding around an image.

The following Java code example shows you how to use the Scalr.pad() method to add a border around the image with default border color Color.BLACK.

PaddingImageExample1.java

import org.imgscalr.Scalr;

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

public class PaddingImageExample1 {
    public static void main(String... args) {
        String originalFilePath = "D:\\TestData\\logo.jpg";
        String targetFilePath = "D:\\TestData\\logo-pad.jpg";
        int targetSize = 100;

        padImage(originalFilePath, targetFilePath, targetSize);
    }

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

            BufferedImage resizedImage = Scalr.pad(originalImage, padding);

            File resizedFile = new File(targetFilePath);
            ImageIO.write(resizedImage, "jpg", resizedFile);

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

Java Padding Image File using Imgscalr

The Scalr.pad() method also allows you to provide the color for the border.

PaddingImageExample2.java

import org.imgscalr.Scalr;

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

public class PaddingImageExample2 {
    public static void main(String... args) {
        String originalFilePath = "D:\\TestData\\logo.jpg";
        String targetFilePath = "D:\\TestData\\logo-pad.jpg";
        int targetSize = 100;
        Color paddingColor = Color.RED;

        padImage(originalFilePath, targetFilePath, targetSize, paddingColor);
    }

    public static void padImage(String originalFilePath, String targetFilePath, int padding, Color color) {
        try {
            File sourceFile = new File(originalFilePath);
            BufferedImage originalImage = ImageIO.read(sourceFile);

            BufferedImage resizedImage = Scalr.pad(originalImage, padding, color);

            File resizedFile = new File(targetFilePath);
            ImageIO.write(resizedImage, "jpg", resizedFile);

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

Java Padding Image File using Imgscalr

Happy Coding 😊