Link
Skip to main content

Crop an image

What does it do?

The crop operation crops the image at the position, width and height of the specified rectangle.

Example in Java to crop an image

Add JDeli to your project via Maven or Gradle, or see using JDeli with Java modules.

ImageProcessingOperations uses a builder pattern — chain transformation methods to define how the image should be processed, then pass the object to JDeli.convert() to apply them during format conversion.

ImageProcessingOperations operations = new ImageProcessingOperations();

// Chain multiple operations such as scale, blur, rotate, sharpen, and more
operations.crop(new Rectangle(10, 10, 100, 150)); // crop to a 100x150 rectangle at position 10,10 
        
// Apply the operations to a BufferedImage
BufferedImage modifiedImage = operations.apply(BufferedImage originalImage);

JDeli.convert() — Input/Output Methods

Pass an ImageProcessingOperations object to any JDeli.convert() overload to apply transformations during conversion. JDeli supports three input/output models:

Using File

Read from and write to disk paths directly.

File inputFile = new File("path/to/file");
File outputFile = new File("path/to/output-cropped-file");
JDeli.convert(inputFile, outputFile, operations);

Using InputStream and OutputStream

Use for network streams, servlet responses, or any scenario where file paths are unavailable.

final InputStream inputStream = new FileInputStream(inputFile);
final OutputStream outputStream = new FileOutputStream(outputFile);
final String outputFormat = "png"; // Supported formats: avif, bmp, gif, heic, jpeg, pdf, png, tiff, webp
JDeli.convert(inputStream, outputStream, outputFormat, operations);

Using byte[]

Use for in-memory processing pipelines where data is already loaded.

byte[] inputData = Files.readAllBytes(Paths.get("/path/to/file"));
final String outputFormat = "png"; // Supported formats: avif, bmp, gif, heic, jpeg, pdf, png, tiff, webp
byte[] outputData = JDeli.convert(inputData, outputFormat, operations);

Frequently Asked Questions

Can I chain multiple image processing operations together?

Yes. ImageProcessingOperations is a builder — call as many methods as needed before passing the object to JDeli.convert(). Operations are applied in the order they are chained.

Can I apply operations without converting the image format?

Yes. Call operations.apply(BufferedImage originalImage) directly to get a processed BufferedImage without any format conversion.

Which input types does JDeli support for image processing?

JDeli supports File, InputStream/OutputStream, and byte[]. All three overloads of JDeli.convert() accept an ImageProcessingOperations parameter.

What output formats can I convert to when applying operations?

JDeli can write to AVIF, BMP, GIF, HEIC, JPEG, PDF, PNG, TIFF, and WebP when using JDeli.convert() with an ImageProcessingOperations object.

View Javadoc on the crop operation


Why JDeli?

  • Support image formats such as AVIF, HEIC and JPEG XL that are not supported in Java.
  • Process images up to 3x faster than ImageIO and alternative Java image libraries.
  • Prevent JVM crashes caused by native code in other image libraries such as ImageIO.
  • Handle JPEG, PNG, TIFF image file formats fully in Java.
  • Keep your Image files secure as JDeli makes no calls to any external system or third party library.

Learn more about JDeli

Start Your Free Trial