Mark Stephens Mark has been working with Java and PDF since 1999 and is a big NetBeans fan. He enjoys speaking at conferences. He has an MA in Medieval History and a passion for reading.

How to crop an image in Java

1 min read

Crop an image in java

In this article, I will show you how to crop an image in Java using just ImageIO or JDeli. JDeli is a high-performance Java image library designed for enterprise-level use, offering efficient handling of image formats and processing tasks.

Crop an image in Java with:

ImageIO

  1. Load the image file using Java ImageIO (which is built into Java)
  2. Get a cropped version (x, y, width, height) (0,0 is top left corner)
  3. Save the image back to a File

Note it is important to save the image before applying any other changes as the BufferedImage object is shared between the original and new crop object.

JDeli

  1. Create a Crop ImageOperation in JDeli (here is how to setup JDeli)
  2. Get a cropped version (x, y, width, height) (0,0 is top left corner)

And that is all you need to do!

Below is an example of the cropped and original image

You can also extract GPS data from an image, here is a simple guide to show you, with multiple different tools.

Handling Different Image Formats

JDeli supports numerous modern image formats:

  • JPEG, PNG, TIFF
  • JPEG2000 (JPX)
  • HEIC, AVIF, WebP
  • PDF image extraction

Common Issues & Troubleshooting

  • Crop area outside image bounds: Ensure your (x, y, width, height) parameters fit within the image dimensions to avoid exceptions.
  • Image format support: Native ImageIO may not support newer formats like HEIC or AVIF—consider JDeli for wide format coverage.
  • Thread safety: ImageIO operations are usually thread-safe but always test in concurrent environments.

FAQs

Q: Can I crop images with transparency?

A: Yes, both ImageIO and JDeli support formats with transparency such as PNG.

Q: How do I crop dynamically based on user input?

A: Capture user-selected coordinates and pass them to getSubimage or JDeli’s crop rectangle.



Are you a Java Developer working with Image files?

Mark Stephens Mark has been working with Java and PDF since 1999 and is a big NetBeans fan. He enjoys speaking at conferences. He has an MA in Medieval History and a passion for reading.