Java编写的全屏截图小程序

 1 package cnom.test.testUtils;
 2
 3 import java.awt.AWTException;
 4 import java.awt.Dimension;
 5 import java.awt.Rectangle;
 6 import java.awt.Robot;
 7 import java.awt.Toolkit;
 8 import java.awt.image.BufferedImage;
 9 import java.io.File;
10 import java.io.IOException;
11
12 import javax.imageio.ImageIO;
13
14 /**
15  * 显示器截图
16  */
17
18 public class LightScreen {
19
20     private static String imageFormat = "png"; // 图像文件的格式
21
22     private static String filePath = "D:/显示器截图/"; // 图像文件的格式
23
24     public void snapShot(String format, String fileName) throws IOException, AWTException {
25         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
26         Rectangle screenRectangle = new Rectangle(screenSize);
27         Robot robot = new Robot();
28         BufferedImage image = robot.createScreenCapture(screenRectangle);
29         ImageIO.write(image, format, new File(fileName));
30     }
31
32     public static void main(String[] args) throws IOException, AWTException {
33
34         LightScreen cam = new LightScreen();//
35         String fileName = filePath + "截图." + imageFormat;
36
37         cam.snapShot(imageFormat, fileName);
38
39     }
40 }
01-21 22:58