This is an entry level article, Master please skip.
In this article we will learn how to tailor images in Java and save the trimmed parts to a file separately.
We will learn from the following steps:
1. Input image, specify the path of image to be processed
2. Allow users to drag and drop parts to trim
3. Use the Robot class to determine the trim part's coordinates after selection
4. Trim the selected image and keep
Next we start the coding section.
LISTING1: The introduced class
Copy Code code as follows:
Import Java.awt.Graphics;
Import Java.awt.Rectangle;
Import Java.awt.Robot;
Import java.awt.event.MouseEvent;
Import Java.awt.event.MouseListener;
Import Java.awt.event.MouseMotionListener;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import Javax.imageio.ImageIO;
Import Javax.swing.JFrame;
Description
The 1.Graphics class contains methods for drawing rectangles
2. We use the Rectangle class as a drag-and-drop rectangular area for tailoring.
3.Robot class for capturing screenshots
4. Use the mouse listener to get the mouse drag time
5.Robot class uses BufferedImage to do image processing
The 6.File class is used to open an image file
The 7.ImageIO class is used to write an image to a PNG or JPG image file
8.JFrame for display interface
Now we write the entry class that contains the Main method
LISTING2: Entrance class
Copy Code code as follows:
public class Cropimage extends JFrame implements MouseListener, Mousemotionlistener
{
int drag_status=0,c1,c2,c3,c4;
public static void Main (String args[])
{
New Cropimage (). Start ();
}
Description
1. A class named Cropimage was written
2. This class extends the JFrame to achieve all the functions of the frame
3. Implements a different mouse event listener to know when the user starts dragging the mouse pointer
The 4.drag_status variable is used to save the coordinates when the mouse starts dragging
5. We define the main method to invoke a Start method, which is defined below
Next is the Start method.
Listing 2
Copy Code code as follows:
public void Start ()
{
Imagepanel im=new Imagepanel ("f:\\wallpaper\\wallpapers\\1.jpg");
Add (IM);
SetSize (400,400);
SetVisible (TRUE);
Addmouselistener (this);
Addmousemotionlistener (this);
Setdefaultcloseoperation (Exit_on_close);
}
Description
1. We define a class called Imagepanel, using the image to be processed as a parameter
2. Place the Imagepanel in the JFrame to display the picture and start listening for mouse events
Let's define a method for handling mouse events
Listing 3: Mouse event handler function
Copy Code code as follows:
@Override
public void mouseclicked (MouseEvent arg0) {
}
@Override
public void mouseentered (MouseEvent arg0) {
}
@Override
public void mouseexited (MouseEvent arg0) {
}
@Override
public void mousepressed (MouseEvent arg0) {
Repaint ();
C1=arg0.getx ();
C2=arg0.gety ();
}
@Override
public void mousereleased (MouseEvent arg0) {
Repaint ();
if (drag_status==1)
{
C3=arg0.getx ();
C4=arg0.gety ();
Try
{
Draggedscreen ();
}
catch (Exception e)
{
E.printstacktrace ();
}
}
}
@Override
public void mousedragged (MouseEvent arg0) {
Repaint ();
Drag_status=1;
C3=arg0.getx ();
C4=arg0.gety ();
}
@Override
public void mousemoved (MouseEvent arg0) {
}
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);
}
Description
1. Store the current coordinates when the mouse is pressed to C1 and C2
2. Set the drag state variable Drag_status to True when the mouse is pressed and starts dragging
3. When the mouse button is released to indicate that the image clipping area has been selected, call the Draggedscreen method
The 4.paint method is used to drag the rectangular display, drawing the rectangle through the coordinates of the current and initial records.
The following is the code for the Draggedscreen method
Listing 4:draggedscreen method
Copy Code code as follows:
public void Draggedscreen () throws Exception
{
int w = C1-C3;
int h = C2-C4;
w = w *-1;
H = h *-1;
Robot Robot = new Robot ();
BufferedImage img = robot.createscreencapture (New Rectangle (C1, c2,w,h));
File Save_path=new file ("Screen1.jpg");
Imageio.write (IMG, "JPG", Save_path);
System.out.println ("Cropped image saved successfully.");
}}
Description
1. Calculate the height and width of the image first
2. Use the Robot class to take a screenshot of the trimmed area and hold it to another file screen1.jpg
The complete code
Listing 5:imagepanel.java
Copy Code code as follows:
Import java.awt.Dimension;
Import Java.awt.Graphics;
Import Java.awt.Image;
Import Javax.swing.ImageIcon;
Import Javax.swing.JPanel;
Class Imagepanel extends JPanel {
Private Image img;
Public Imagepanel (String img) {
This (new ImageIcon (IMG). GetImage ());
}
public Imagepanel (Image img) {
this.img = img;
Dimension size = new Dimension (Img.getwidth ( NULL), img.getheight (null));
//Dimension size = new Dimension (10,10);
setpreferredsize (size);
setminimumsize (size);
setmaximumsize (size);
setSize (size);
setlayout (null);
}
public void Paintcomponent (Graphics g) {
G.drawimage (IMG, 0, 0, NULL);
}
}
Listing 6:cropimage.java
Copy Code code as follows:
Import Java.awt.Graphics;
Import Java.awt.Rectangle;
Import Java.awt.Robot;
Import java.awt.event.MouseEvent;
Import Java.awt.event.MouseListener;
Import Java.awt.event.MouseMotionListener;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import Javax.imageio.ImageIO;
Import Javax.swing.JFrame;
public class Cropimage extends JFrame implements MouseListener, Mousemotionlistener
{
int drag_status=0,c1,c2,c3,c4;
public static void Main (String args[])
{
New Cropimage (). Start ();
}
public void Start ()
{
Imagepanel im=new Imagepanel ("f:\\wallpaper\\wallpapers\\1.jpg");
Add (IM);
SetSize (400,400);
SetVisible (TRUE);
Addmouselistener (this);
Addmousemotionlistener (this);
Setdefaultcloseoperation (Exit_on_close);
}
public void Draggedscreen () throws Exception
{
int w = C1-C3;
int h = C2-C4;
w = w *-1;
H = h *-1;
Robot Robot = new Robot ();
BufferedImage img = robot.createscreencapture (New Rectangle (C1, c2,w,h));
File Save_path=new file ("Screen1.jpg");
Imageio.write (IMG, "JPG", Save_path);
System.out.println ("Cropped image saved successfully.");
}
@Override
public void mouseclicked (MouseEvent arg0) {
}
@Override
public void mouseentered (MouseEvent arg0) {
}
@Override
public void mouseexited (MouseEvent arg0) {
}
@Override
public void mousepressed (MouseEvent arg0) {
Repaint ();
C1=arg0.getx ();
C2=arg0.gety ();
}
@Override
public void mousereleased (MouseEvent arg0) {
Repaint ();
if (drag_status==1)
{
C3=arg0.getx ();
C4=arg0.gety ();
Try
{
Draggedscreen ();
}
catch (Exception e)
{
E.printstacktrace ();
}
}
}
@Override
public void mousedragged (MouseEvent arg0) {
Repaint ();
Drag_status=1;
C3=arg0.getx ();
C4=arg0.gety ();
}
@Override
public void mousemoved (MouseEvent arg0) {
}
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);
}
}