这是一篇入门级文章,高手请略过。
在这篇文章中我们将学习如何用 Java 对图像进行剪裁并将剪裁出来的部分单独保存到文件中。
我们将通过以下步骤来学习:
接下来我们开始编码部分。
Listing1: 引入的类
- 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;
说明:
现在我们编写包含 main 方法的入口类
Listing2: 入口类
- 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();
- }
说明:
接下来是 start 方法
Listing 2
- 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);
- }
说明:
下面我们定义用于处理鼠标事件的方法
Listing 3: 鼠标事件处理函数
- @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);
- }
说明:
下面是 draggedscreen 方法的代码
Listing 4: draggedScreen 方法
- 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.");
- }}
说明:
完整的代码
Listing 5: ImagePanel.java
- 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
- 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);
- }
- }
英文原文:http://mrbool.com/how-to-crop-an-image-using-java/25272