1. 程式人生 > >用java做出螢幕截圖

用java做出螢幕截圖

java提供了一個機器人Robot類,利用這個類的createScreenCapture方法即可。

關於主介面和按鈕的其部分,相信大家都知道,我就不多說了,抓取圖片的程式碼如下:

Robot robot = new Robot();
Toolkit toolkit = getToolkit().getDefaultToolkit();
Rectangle rect = new Rectangle(0, 0, 
                      toolkit.getScreenSize().width,
                      toolkit.getScreenSize().height);
this.setVisible(false);
image = robot.createScreenCapture(rect);
                        this.setVisible(true);
repaint();

這裡我給出我的示例程式碼


import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
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.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Demo11_5 extends JFrame implements ActionListener{
JButton button1,button2;
JPanel panel1,panel2;
BufferedImage image;
public Demo11_5() {
super("螢幕抓取");
setBounds(600,400,600,600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setDefaultLookAndFeelDecorated(true);
setVisible(true);
button1 = new JButton("抓取圖片");
button2 = new JButton("儲存圖片");
button1.addActionListener(this);
button2.addActionListener(this);
panel1 = new JPanel();
panel2 = new JPanel();
panel1.add(button1);
panel1.add(button2);
add(panel1,BorderLayout.NORTH);
add(panel2,BorderLayout.CENTER);
validate();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button1) {
takeImage();
}
if(e.getSource() == button2) {
File file = new File("2.jpg");
try {
ImageIO.write(image, "JPEG", file);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
public void takeImage() {
try {
Robot robot = new Robot();
Toolkit toolkit = getToolkit().getDefaultToolkit();
Rectangle rect = new Rectangle(0, 0, 
                      toolkit.getScreenSize().width,
                      toolkit.getScreenSize().height);
this.setVisible(false);
image = robot.createScreenCapture(rect);
this.setVisible(true);
repaint();
} catch (AWTException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Demo11_5();
}

}

圖片儲存預設路徑是建專案的路徑下。

效果如下: