以下代码不是本人所写,乃是从网上搜到,记录下来供以后参考。
1 package remotescreen;
2
3 import java.awt.AWTException;
4 import java.awt.BorderLayout;
5 import java.awt.Cursor;
6 import java.awt.Graphics;
7 import java.awt.Rectangle;
8 import java.awt.Robot;
9 import java.awt.Toolkit;
10 import java.awt.event.MouseAdapter;
11 import java.awt.event.MouseEvent;
12 import java.awt.event.MouseMotionAdapter;
13 import java.awt.image.BufferedImage;
14 import java.io.File;
15 import java.io.IOException;
16 import java.io.OutputStream;
17
18 import javax.imageio.ImageIO;
19 import javax.swing.Icon;
20 import javax.swing.ImageIcon;
21 import javax.swing.JDialog;
22 import javax.swing.JFrame;
23 import javax.swing.JLabel;
24 import javax.swing.JPanel;
25
26 /**
27 * @Author Qiu_BaiChao 一个简单的屏幕抓图
28 *
29 **/
30
31 public class ScreenCapture {
32 // test main
33 public static void main(String[] args) throws Exception {
34 String userdir = System.getProperty("user.dir");
35 File tempFile = new File("d:", "temp.png");
36 ScreenCapture capture = ScreenCapture.getInstance();
37 capture.captureImage();
38 JFrame frame = new JFrame();
39 JPanel panel = new JPanel();
40 panel.setLayout(new BorderLayout());
41 JLabel imagebox = new JLabel();
42 panel.add(BorderLayout.CENTER, imagebox);
43 imagebox.setIcon(capture.getPickedIcon());
44 capture.saveAsPNG(tempFile);
45 capture.captureImage();
46 imagebox.setIcon(capture.getPickedIcon());
47 frame.setContentPane(panel);
48 frame.setSize(400, 300);
49 frame.show();
50 System.out.println("Over");
51 }
52
53 private ScreenCapture() {
54
55 try {
56 robot = new Robot();
57 } catch (AWTException e) {
58 System.err.println("Internal Error: " + e);
59 e.printStackTrace();
60 }
61 JPanel cp = (JPanel) dialog.getContentPane();
62 cp.setLayout(new BorderLayout());
63 labFullScreenImage.addMouseListener(new MouseAdapter() {
64 public void mouseReleased(MouseEvent evn) {
65 isFirstPoint = true;
66 pickedImage = fullScreenImage
67 .getSubimage(recX, recY, recW, recH);
68 dialog.setVisible(false);
69 }
70 });
71
72 labFullScreenImage.addMouseMotionListener(new MouseMotionAdapter() {
73 public void mouseDragged(MouseEvent evn) {
74 if (isFirstPoint) {
75 x1 = evn.getX();
76 y1 = evn.getY();
77 isFirstPoint = false;
78 } else {
79 x2 = evn.getX();
80 y2 = evn.getY();
81 int maxX = Math.max(x1, x2);
82 int maxY = Math.max(y1, y2);
83 int minX = Math.min(x1, x2);
84 int minY = Math.min(y1, y2);
85 recX = minX;
86 recY = minY;
87 recW = maxX - minX;
88 recH = maxY - minY;
89 labFullScreenImage.drawRectangle(recX, recY, recW, recH);
90 }
91 }
92
93 public void mouseMoved(MouseEvent e) {
94 labFullScreenImage.drawCross(e.getX(), e.getY());
95 }
96 });
97
98 cp.add(BorderLayout.CENTER, labFullScreenImage);
99 dialog.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
100 dialog.setAlwaysOnTop(true);
101 dialog.setMaximumSize(Toolkit.getDefaultToolkit().getScreenSize());
102 dialog.setUndecorated(true);
103 dialog.setSize(dialog.getMaximumSize());
104 dialog.setModal(true);
105 }
106
107 // Singleton Pattern
108 public static ScreenCapture getInstance() {
109 return defaultCapturer;
110 }
111
112 /** 捕捉全屏慕 */
113 public Icon captureFullScreen() {
114 fullScreenImage = robot.createScreenCapture(new Rectangle(Toolkit
115 .getDefaultToolkit().getScreenSize()));
116 ImageIcon icon = new ImageIcon(fullScreenImage);
117 return icon;
118 }
119
120 /**
121 * 捕捉屏幕的一个矫形区域
122 */
123 public void captureImage() {
124 fullScreenImage = robot.createScreenCapture(new Rectangle(Toolkit
125 .getDefaultToolkit().getScreenSize()));
126 ImageIcon icon = new ImageIcon(fullScreenImage);
127 labFullScreenImage.setIcon(icon);
128 dialog.setVisible(true);
129 }
130
131 /** 得到捕捉后的BufferedImage */
132 public BufferedImage getPickedImage() {
133 return pickedImage;
134 }
135
136 /** 得到捕捉后的Icon */
137 public ImageIcon getPickedIcon() {
138 return new ImageIcon(getPickedImage());
139 }
140
141 /**
142 * 储存为一个文件,为PNG格式
143 *
144 * @deprecated replaced by saveAsPNG(File file)
145 **/
146 @Deprecated
147 public void saveToFile(File file) throws IOException {
148 ImageIO.write(getPickedImage(), defaultImageFormater, file);
149 }
150
151 /** 储存为一个文件,为PNG格式 */
152 public void saveAsPNG(File file) throws IOException {
153 ImageIO.write(getPickedImage(), "png", file);
154 }
155
156 /** 储存为一个JPEG格式图像文件 */
157 public void saveAsJPEG(File file) throws IOException {
158 ImageIO.write(getPickedImage(), "JPEG", file);
159 }
160
161 /** 写入一个OutputStream */
162 public void write(OutputStream out) throws IOException {
163 ImageIO.write(getPickedImage(), defaultImageFormater, out);
164 }
165
166 // singleton design pattern
167 private static ScreenCapture defaultCapturer = new ScreenCapture();
168 private int x1, y1, x2, y2;
169 private int recX, recY, recH, recW; // 截取的图像
170 private boolean isFirstPoint = true;
171 private BackgroundImage labFullScreenImage = new BackgroundImage();
172 private Robot robot;
173 private BufferedImage fullScreenImage;
174 private BufferedImage pickedImage;
175 private String defaultImageFormater = "png";
176 private JDialog dialog = new JDialog();
177 }
178
179 /** 显示图片的Label */
180 class BackgroundImage extends JLabel {
181
182 private static final long serialVersionUID = -7240053488099673743L;
183
184 public void paintComponent(Graphics g) {
185 super.paintComponent(g);
186 g.drawRect(x, y, w, h);
187 String area = Integer.toString(w) + " * " + Integer.toString(h);
188 g.drawString(area, x + (int) w / 2 - 15, y + (int) h / 2);
189 g.drawLine(lineX, 0, lineX, getHeight());
190 g.drawLine(0, lineY, getWidth(), lineY);
191 }
192
193 public void drawRectangle(int x, int y, int width, int height) {
194 this.x = x;
195 this.y = y;
196 h = height;
197 w = width;
198 repaint();
199 }
200
201 public void drawCross(int x, int y) {
202 lineX = x;
203 lineY = y;
204 repaint();
205 }
206
207 int lineX, lineY;
208 int x, y, h, w;
209 }
210