Skip to content

Instantly share code, notes, and snippets.

@etlapa
Forked from madan712/ImageCropper.java
Created November 8, 2019 13:05
Show Gist options
  • Save etlapa/125ff8ebb8b6d20732c183a41cf74d24 to your computer and use it in GitHub Desktop.
Save etlapa/125ff8ebb8b6d20732c183a41cf74d24 to your computer and use it in GitHub Desktop.
Java - Simple image cropping example
/* ImageCropper.java */
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageCropper {
public static void main(String[] args) {
try {
BufferedImage originalImgage = ImageIO.read(new File("C:/Test/pool.jpg"));
System.out.println("Original Image Dimension: "+originalImgage.getWidth()+"x"+originalImgage.getHeight());
BufferedImage SubImgage = originalImgage.getSubimage(300, 150, 200, 200);
System.out.println("Cropped Image Dimension: "+SubImgage.getWidth()+"x"+SubImgage.getHeight());
File outputfile = new File("C:/Test/croppedImage.jpg");
ImageIO.write(SubImgage, "jpg", outputfile);
System.out.println("Image cropped successfully: "+outputfile.getPath());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment