在使用计算机时,有时会需要持久保存屏幕上显示的内容,此时可以使用截图软件将指定的区域制作成图片保存。比较好用的截图软件有Snagit、红蜻蜓等。本实例将使用Java的Robot类编写一个功能非常简单的截图软件。实例运行效果如图
Robot类用于为测试自动化、自运行演示程序和其他需要控制鼠标和键盘的应用程序生成本机系统输入事件。Robot的主要目的是便于Java平台实现自动测试。使用该类生成输入事件与将事件发送到AWT事件队列或AWT控件的区别在于,事件是在平台的本机输入队列中生成的。例如,Robot.mouseMove将实际移动鼠标光标,而不是只生成鼠标移动事件。注意,某些平台需要特定权限或扩展来访问低级输入控件。如果当前平台配置不允许使用输入控件,那么试图构造Robot对象时将抛出AWTException。为了截图需要使用createScreenCapture()方法,该方法的声明如下:
public BufferedImage createScreenCapture(Rectangle screenRect)
参数说明
screenRect:将在屏幕坐标中捕获的Rect。
(1)编写类ScreenCapture,该类继承了JFrame。在框架中包含了一个标签用来显示截图效果,一个“开始截图”按钮用来实现截图并在标签中显示。
(2)编写方法do_button_actionPerformed(),用来监听单击“开始截图”按钮事件。在该方法中,完成截图并在标签上显示截图的效果。代码如下:
package com.mingrisoft.robot;
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.awt.image.ImageProducer;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.UIManager;
import javax.swing.border.EtchedBorder;
public class ScreenCapture extends JFrame {
/**
*
*/
private static final long serialVersionUID = -5093584478732778943L;
private JPanel contentPane;
private JLabel imageLabel;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Throwable e) {
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ScreenCapture frame = new ScreenCapture();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ScreenCapture() {
setTitle("\u622A\u56FE\u8F6F\u4EF6");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel buttonPanel = new JPanel();
contentPane.add(buttonPanel, BorderLayout.SOUTH);
JButton button = new JButton("\u5F00\u59CB\u622A\u56FE");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
do_button_actionPerformed(e);
}
});
button.setFont(new Font("微软雅黑", Font.PLAIN, 18));
buttonPanel.add(button);
JPanel imagePanel = new JPanel();
contentPane.add(imagePanel, BorderLayout.CENTER);
imagePanel.setLayout(new BorderLayout(0, 0));
imageLabel = new JLabel("");
imageLabel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
imageLabel.setHorizontalAlignment(SwingConstants.CENTER);
imagePanel.add(imageLabel);
}
protected void do_button_actionPerformed(ActionEvent e) {
try {
Robot robot = new Robot();// 创建Robot对象
Toolkit toolkit = Toolkit.getDefaultToolkit();// 获得Toolkit对象
Rectangle area = new Rectangle(toolkit.getScreenSize());// 设置截取区域为全屏
// 将BufferedImage转换成Image
BufferedImage bufferedImage = robot.createScreenCapture(area);
ImageProducer producer = bufferedImage.getSource();
Image image = toolkit.createImage(producer);
imageLabel.setIcon(new ImageIcon(image));// 显示图片
} catch (AWTException e1) {
e1.printStackTrace();
}
}
}
心法领悟:截图程序的增强。
读者可以在本程序的基础上,增加让用户输入要截图的范围。还可以增加图片的保存功能,即使用输出流将图片写入到本地文件。