Skip to content

Instantly share code, notes, and snippets.

@madan712
Created September 2, 2012 17:46
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save madan712/3602189 to your computer and use it in GitHub Desktop.
Save madan712/3602189 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();
}
}
}
@drmopper
Copy link

Was looking around for image cropping in java and this is just what I needed

@Yuvi111
Copy link

Yuvi111 commented Apr 24, 2018

nice thanks very much.

@naveenjothi
Copy link

what ever may be the picture the output is same would you send me the reason?

@ElZa3emMustafa
Copy link

Thanks very much this program what i need -- it allow u to position the part u want to crop then the size :)

@etlapa
Copy link

etlapa commented Nov 8, 2019

Simple and functional. Exactly what I was looking for. Thanks so much!

@reheter
Copy link

reheter commented Nov 20, 2019

work perfectly, Thank!!!

@noname2777
Copy link

Really the perfect solution. Thank you for sharing!

@ngocnd0242
Copy link

ngocnd0242 commented Mar 28, 2020

I don't know, why the size after resize is much smaller than the original size?
e.g.
1.4mb down to 77 kb
Could you explain it to me? (bow)

@SupriyaWebpioneer
Copy link

Error: Could not create the Java Virtual Machine.
coming

@SupriyaWebpioneer
Copy link

Unrecognized option: --enable-preview
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
/home/webadmin/.cache/netbeans/12.2/executor-snippets/run.xml:111: The following error occurred while executing this line:
/home/webadmin/.cache/netbeans/12.2/executor-snippets/run.xml:94: Java returned: 1
BUILD FAILED (total time: 0 seconds)

@SupriyaWebpioneer
Copy link

Thanks very much this program what i need -- it allow u to position the part u want to crop then the size :)

is this working ??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment