In this article, I will show you how to crop an image in Java using just ImageIO or JDeli.
Crop an image in Java with:
ImageIO
- Load the image file using Java ImageIO (which is built into Java)
BufferedImage image = ImageIO.read(new File("C:\\path\\to\\kitten.jpg"));
- Get a cropped version (x, y, width, height) (0,0 is top left corner)
BufferedImage crop = image.getSubimage(0,0, 100, 100);
- Save the image back to a File
ImageIO.write(crop, "JPEG", new File("C:\\path\\to\\picture.jpeg"));
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
- Create a Crop ImageOperation in JDeli (here is how to setup JDeli)
ImageProcessingOperations imageOps = new ImageProcessingOperations();
imageOps.crop(new Rectangle(0, 0, 100, 100)); //you can add other Operations as well - Get a cropped version (x, y, width, height) (0,0 is top left corner)
JDeli.convert(new File("C:\\path\\to\\picture.jpeg"), new File("C:\\path\\to\\crop.jpeg"), imageOps);
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.
Are you a Java Developer working with Image files?
// Read an image
BufferedImage bufferedImage = JDeli.read(dicomImageFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(heicImageFile);
// Write an image
JDeli.write(bufferedImage, "heic", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(jpegImageFile);
// Write an image
JDeli.write(bufferedImage, "jpeg", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(jpeg2000ImageFile);
// Write an image
JDeli.write(bufferedImage, "jpx", outputStreamOrFile);
// Write an image
JDeli.write(bufferedImage, "pdf", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(pngImageFile);
// Write an image
JDeli.write(bufferedImage, "png", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(tiffImageFile);
// Write an image
JDeli.write(bufferedImage, "tiff", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(webpImageFile);
// Write an image
JDeli.write(bufferedImage, "webp", outputStreamOrFile);
What is JDeli?
JDeli is a commercial Java Image library that is used to read, write, convert, manipulate and process many different image formats.
Why use JDeli?
To handle many well known formats such as JPEG, PNG, TIFF as well as newer formats like AVIF, HEIC and JPEG XL in java with no calls to any external system or third party library.
What licenses are available?
We have 3 licenses available:
Server for on premises and cloud servers, Distribution for use in a named end user applications, and Custom for more demanding requirements.
How does JDeli compare?
We work hard to make sure JDeli performance is better than or similar to other java image libraries. Check out our benchmarks to see just how well JDeli performs.