Java Crop Image File using Imgscalr

Tags: Imgscalr BufferedImage ImageIO File Image File Crop Image Scalr

In this Java tutorial we learn how to crop 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 crop an image in Java

The Imgscalr library provides the Scalr.crop() method to crop a given image.

The following Java code shows you how to implement a method to crop a given image file using the Scalr.crop() method with provided target width and height.

CropImageExample1.java

import org.imgscalr.Scalr;

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

public class CropImageExample1 {
    public static void main(String... args) {
        String originalFilePath = "D:\\TestData\\logo.jpg";
        String targetFilePath = "D:\\TestData\\logo-crop.jpg";
        int width = 800;
        int height = 600;

        cropImage(originalFilePath, targetFilePath, width, height);
    }

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

            BufferedImage resizedImage = Scalr.crop(originalImage, width, height);

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

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

Java Crop Image File using Imgscalr

The Scalr.crop() also allows you to crop an image from a given starting x and y coordinate.

CropImageExample2.java

import org.imgscalr.Scalr;

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

public class CropImageExample2 {
    public static void main(String... args) {
        String originalFilePath = "D:\\TestData\\logo.jpg";
        String targetFilePath = "D:\\TestData\\logo-crop.jpg";
        int x = 300;
        int y = 300;

        int width = 800;
        int height = 600;

        cropImage(originalFilePath, targetFilePath, x, y, width, height);
    }

    public static void cropImage(String originalFilePath, String targetFilePath, int x, int y, int width, int height) {
        try {
            File sourceFile = new File(originalFilePath);
            BufferedImage originalImage = ImageIO.read(sourceFile);

            BufferedImage resizedImage = Scalr.crop(originalImage, x, y, width, height);

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

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

Java Crop Image File using Imgscalr

Happy Coding 😊