Follow these steps:
- Enter the image and specify the path of the image to be processed.
- Allow users to drag and drop the part to be cropped
- Select and use the robot class to determine the coordinates of the cropped part.
- 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: