How to Crop Image in Java

This tutorial addresses the very common requirement i.e. how to crop image in Java by providing the environment configuration, a detailed process in the form of easy-to-follow steps and a runnable sample code that can be used in any of the common operating systems. To crop bitmap Java based code contains a couple of lines of code which crop and save the resultant image as a BMP file.

Steps to Crop Image in Java

  1. Configure the project to add Aspose.Imaging from the Maven repository to crop an image
  2. Load the source image file into the Image class object
  3. Cast the Image object to RasterImage class
  4. Cache the image into memory for an enhanced performance
  5. Crop the image by sliding the sides inwards specifying the desired values
  6. Save the resultant image on the disk

These steps explain how in java crop bitmap by providing the details about the development environment configuration and a step-by-step process to be followed for this purpose. All the necessary classes and methods are mentioned that are required to perform this task. Here imaging cropping is described by sliding the sides towards the image center however, you can crop an image by defining a rectangle also.

Code to Crop Image in Java

These lines of code in Java crop image and save it back on the disk however you have a number of options to load the source image like you can load an image from a stream, set data recovery mode while loading an image, set background color incase the pixel value is corrupted and set the maximum allowed buffer size.

In this tutorial, we have learned to crop images however, if you want to learn how to compress images, refer to the article on how to compress image in Java.

 English