Java Program to Crop Image Using BufferedImage Class Last Updated : 17 Oct, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report In the Java programming language, we need some classes to crop an image. So these classes are as follows: 1. To read and write an image file we have to import the File class. This class represents file and directory path names in general. import java.io.File 2. To handle errors we use the IOException class. import java.io.IOException 3. To hold the image we create the BufferedImage object for that we use BufferedImage class. This object is used to store an image in RAM. import java.awt.image.BufferedImage 4. To perform the image read-write operation we will import the ImageIO class. This class has static methods to read and write an image. import javax.imageio.ImageIO Approach: Change dimensions of the imageUsing some in-built methods of BufferedImage class and Color c Example: Java // Java Program to Crop Image Using BufferedImage Class // Importing required packages import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; // Main class public class GFG { // Main driver method public static void main(String[] args) { // Try block to check for exceptions try { // Reading original image from local path by // creating an object of BufferedImage class BufferedImage originalImg = ImageIO.read( new File("D:/test/Image.jpeg")); // Fetching and printing alongside the // dimensions of original image using getWidth() // and getHeight() methods System.out.println("Original Image Dimension: " + originalImg.getWidth() + "x" + originalImg.getHeight()); // Creating a subimage of given dimensions BufferedImage SubImg = originalImg.getSubimage(50, 50, 50, 50); // Printing Dimensions of new image created System.out.println("Cropped Image Dimension: " + SubImg.getWidth() + "x" + SubImg.getHeight()); // Creating new file for cropped image by // creating an object of File class File outputfile = new File("D:/test/ImageCropped.jpeg"); // Writing image in new file created ImageIO.write(SubImg, "png", outputfile); // Display message on console representing // proper execution of program System.out.println( "Cropped Image created successfully"); } // Catch block to handle the exceptions catch (IOException e) { // Print the exception along with line number // using printStackTrace() method e.printStackTrace(); } } } Output: Cropped Image created successfully Also, after executing the program console will show an executed message and a new cropped image will be created at the path entered which is shown below: Comment More infoAdvertise with us Next Article Java Program to Extract a Image From a PDF S sam_2200 Follow Improve Article Tags : Misc Java Java Programs Image-Processing Practice Tags : JavaMisc Similar Reads Java Program to Blur Images using OpenCV Blurring is a simple and frequently used image processing operation. It is also called as Smoothing. OpenCV library provides many functions to apply diverse linear filters to smooth images or blur images. Smoothing of an image removes noisy pixels from the image and applying a low-pass filter to an 4 min read Java Program to Create Grayscale Image The RGB model where red, green and blue lights are added in various different intensities and permutations and combinations of them producing broad spectrum array of colors whereas grayscale refers to the white and black colors only where the weakest intensity gives birth to black color and stronges 4 min read Java Program to Blur Image using Smoothing Blurring is a simple and frequently used image processing operation. It is also called as Smoothing. Smoothing of an image removes noisy pixels from the image and applying a low-pass filter to an image. A low-pass filter means removing noise from an image while leaving the majority of the image unda 4 min read Java Program to Extract a Image From a PDF Program to extract an image from a PDF using Java. The external jar file is required to import in the program. Below is the implementation for the same. Algorithm: Extracting image using the APACHE PDF Box module.Load the existing PDF document using file io.Creating an object of PDFRenderer class.Re 2 min read Java Program to Convert Byte Array to Image A byte array is the array of bytes that is used to store the collection of binary data. For example, the byte array of an image stores the information of every pixel of the image. In the byte array, we can store the content of any file in binary format. We can initialize the byte array with the byte 3 min read Java Program to Copy and Paste an image in OpenCV OpenCV is a Machine Learning and open-source computer vision software library, the main purpose for which it was developed is to enable a common medium to increase the use of machine perception in commercial businesses and accelerate the development of computer vision applications, it is a smooth an 3 min read Like