java实现截屏

这是一个用于Java应用程序的简单屏幕截图工具类。它能够捕获整个屏幕的内容并将其保存为指定格式的图片文件,默认使用GuiCamera作为文件前缀和png格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1 import java.awt.Dimension;
 2 import java.awt.Rectangle;
 3 import java.awt.Robot;
 4 import java.awt.Toolkit;
 5 import java.awt.image.BufferedImage;
 6 import java.io.File;
 7 
 8 import javax.imageio.ImageIO;
 9 
10 /*******************************************************************
11 * 该JavaBean可以直接在其他Java应用程序中调用,实现屏幕的"拍照"
12 * This JavaBean is used to snapshot the GUI in a
13 * Java application! You can embeded
14 * it in to your java application source code, and us
15 * it to snapshot the right GUI of the application
16 * @see javax.ImageIO
17 * @author liluqun (liluqun@263.net)
18 * @version 1.0
19 *
20 *****************************************************/
21 
22 public class GuiCamera
23 {  
24     private String fileName; //文件的前缀
25     private String defaultName = "GuiCamera";
26     static int serialNum=0;
27     private String imageFormat; //图像文件的格式
28     private String defaultImageFormat="png";
29     Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
30 
31     /****************************************************************
32      * 默认的文件前缀为GuiCamera,文件格式为PNG格式
33      * The default construct will use the default
34      * Image file surname "GuiCamera",
35      * and default image format "png"
36      ****************************************************************/
37     public GuiCamera() {
38       fileName = defaultName;
39       imageFormat=defaultImageFormat;
40    
41     }
42 
43     /****************************************************************
44      * @param s the surname of the snapshot file
45      * @param format the format of the  image file,
46      * it can be "jpg" or "png"
47      * 本构造支持JPG和PNG文件的存储
48      ****************************************************************/
49     public GuiCamera(String s,String format) {
50    
51       fileName = s;
52       imageFormat=format;
53     }
54    
55     /****************************************************************
56      * 对屏幕进行拍照
57      * snapShot the Gui once
58      ****************************************************************/
59     public void snapShot() {
60    
61       try {
62       //拷贝屏幕到一个BufferedImage对象screenshot
63         BufferedImage screenshot = (new Robot()).createScreenCapture(new
64             Rectangle(0, 0, (int) d.getWidth(), (int) d.getHeight()));
65         serialNum++;
66         //根据文件前缀变量和文件格式变量,自动生成文件名
67         String name=fileName+String.valueOf(serialNum)+"."+imageFormat;
68         File f = new File(name);
69         System.out.print("Save File "+name);
70       //将screenshot对象写入图像文件
71         ImageIO.write(screenshot, imageFormat, f);
72         System.out.print("..Finished!\n");
73       }
74       catch (Exception ex) {
75         System.out.println(ex);
76       }
77     }
78 
79     public static void main(String[] args)
80     {
81         GuiCamera cam= new GuiCamera("d:\\Hello", "png");//
82 
83         cam.snapShot();
84     }
85 }

原文:http://bbs.chinaunix.net/thread-770968-1-4.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值