国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Java > 正文

java代碼實(shí)現(xiàn)截圖功能(屏幕截圖)

2019-11-26 15:50:45
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

復(fù)制代碼 代碼如下:

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

/*******************************************************************
 * 該JavaBean可以直接在其他Java應(yīng)用程序中調(diào)用,實(shí)現(xiàn)屏幕的"拍照"
 * This JavaBean is used to snapshot the GUI in a
 * Java application! You can embeded
 * it in to your java application source code, and us
 * it to snapshot the right GUI of the application
 * @see javax.ImageIO
 * @author liluqun
 * @version 1.0
 *****************************************************/

public class Test
{
 private String fileName; //文件的前綴
 private String defaultName = "GuiCamera";
 static int serialNum=0;
 private String imageFormat; //圖像文件的格式
 private String defaultImageFormat="png";
 Dimension d = Toolkit.getDefaultToolkit().getScreenSize();

 /****************************************************************
 * 默認(rèn)的文件前綴為GuiCamera,文件格式為PNG格式
 * The default construct will use the default
 * Image file surname "GuiCamera",
 * and default image format "png"
 ****************************************************************/
 public Test() {
 fileName = defaultName;
 imageFormat=defaultImageFormat;

 }

 /****************************************************************
 * @param s the surname of the snapshot file
 * @param format the format of the image file,
 * it can be "jpg" or "png"
 * 本構(gòu)造支持JPG和PNG文件的存儲(chǔ)
 ****************************************************************/
 public Test(String s,String format) {

 fileName = s;
 imageFormat=format;
 }

 /****************************************************************
 * 對(duì)屏幕進(jìn)行拍照
 * snapShot the Gui once
 ****************************************************************/
 public void snapShot() {

 try {
 //拷貝屏幕到一個(gè)BufferedImage對(duì)象screenshot
 BufferedImage screenshot = (new Robot()).createScreenCapture(new
 Rectangle(0, 0, (int) d.getWidth(), (int) d.getHeight()));
 serialNum++;
 //根據(jù)文件前綴變量和文件格式變量,自動(dòng)生成文件名
 String name=fileName+String.valueOf(serialNum)+"."+imageFormat;
 File f = new File(name);
 System.out.print("Save File "+name);
 //將screenshot對(duì)象寫(xiě)入圖像文件
 ImageIO.write(screenshot, imageFormat, f);
 System.out.print("..Finished!/n");
 }
 catch (Exception ex) {
 System.out.println(ex);
 }
 }

 public static void main(String[] args)
 {
 Test cam= new Test("d://Hello", "png");//

 cam.snapShot();
 }
}

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 周至县| 平谷区| 白河县| 青海省| 沿河| 大埔县| 黄浦区| 平安县| 和龙市| 文昌市| 琼结县| 镇安县| 衡阳县| 年辖:市辖区| 岱山县| 武穴市| 峡江县| 阿城市| 饶平县| 昌邑市| 赤城县| 南雄市| 高邑县| 卢氏县| 镇巴县| 积石山| 崇阳县| 莱芜市| 布尔津县| 安丘市| 绥中县| 萍乡市| 安阳县| 梅河口市| 娄烦县| 祁东县| 兰州市| 扎兰屯市| 谢通门县| 玉门市| 汉阴县|