Crop images in Java

Source: Internet
Author: User

Follow these steps:

  1. Enter the image and specify the path of the image to be processed.
  2. Allow users to drag and drop the part to be cropped
  3. Select and use the robot class to determine the coordinates of the cropped part.
  4. Crop and maintain the selected Image

The Code consists of two files: imagepanel. Java and copyimage. java. The following is code implementation:

Imagepanel. the Java file is as follows: Package COM. application. sample; import Java. AWT. dimension; import Java. AWT. graphics; import Java. AWT. image; import javax. swing. imageicon; import javax. swing. jpanel; public class imagepanel extends jpanel {private image IMG; Public imagepanel (string path) {This (New imageicon (PATH ). getimage ();} public imagepanel (image IMG) {This. IMG = IMG; dimension size = new dimension (IMG. getwidth (null), IMG. getheight (null); setpreferredsize (size); setminimumsize (size); setmaximumsize (size); setsize (size); setlayout (null);} public void paintcomponent (Graphics g) {G. drawimage (IMG, 0, 0, null );}}
Copyimage. the Java file is as follows: Package COM. application. sample; import Java. AWT. graphics; import Java. AWT. rectangle; import Java. AWT. robot; import Java. AWT. event. *; import Java. io. *; import javax. swing. jframe; import javax. imageIO. imageIO; import Java. AWT. image. bufferedimage; import Java. math. *; public class copyimage extends jframe implements mouselistener, mousemotionlistener {int drag_status = 0, C1, C2, C3, C4; public static void main (string [] ARGs) {// todo auto-generated method stubnew copyimage (). start ("d :\\ .jpg");} public void start (string path) {imagepanel image = new imagepanel (PATH); add (image); setsize (400,400 ); setvisible (true); addmouselistener (this); addmousemotionlistener (this); this. setdefaclocloseoperation (exit_on_close);} public void draggedscreen () throws exception {int W = C1-C3; int H = c2-C4; W = math. ABS (w); H = math. ABS (h); Robot robot = new robot (); bufferedimage IMG = robot. createscreencapture (New rectangle (C1, C2, W, H); file save_path = new file ("D: \ screen1.jpg"); ImageIO. write (IMG, "jpg", save_path); system. out. println ("cropped image saved successfully. ") ;}@ overridepublic void mouseclicked (mouseevent e) {// todo auto-generated method stub} @ overridepublic void mouseentered (mouseevent E) {// todo auto-generated method stub} @ overridepublic void mouseexited (mouseevent e) {// todo auto-generated method stub} @ overridepublic void mousepressed (mouseevent E) {// todo auto-generated method stubrepaint (); C1 = E. getx (); C2 = E. gety () ;}@ overridepublic void mousereleased (mouseevent e) {// todo auto-generated method stubrepaint (); If (drag_status = 1) {C3 = E. getx (); C4 = E. gety (); try {draggedscreen ();} catch (Exception error) {error. printstacktrace () ;}}@ overridepublic void mousedragged (mouseevent e) {// todo auto-generated method stubrepaint (); drag_status = 1; C3 = E. getx (); C4 = E. gety () ;}@ overridepublic void mousemoved (mouseevent e) {// todo auto-generated method stub} public void paint (Graphics g) {super. paint (g); int W = C1-C3; int H = c2-C4; W = W *-1; H = H *-1; if (W <0) W = W *-1; G. drawrect (C1, C2, W, h );}}

The running result is as follows:

The original image is as follows:

The cropped image is as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.