java代碼實(shí)現(xiàn)截圖功能(屏幕截圖)
更新時(shí)間:2013年12月19日 09:50:03 作者:
java代碼實(shí)現(xiàn)截圖功能,該JavaBean可以直接在其他Java應(yīng)用程序中調(diào)用,默認(rèn)的文件前綴為GuiCamera,文件格式為PNG格式,直接使用下面的類吧
復(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ì)象寫入圖像文件
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();
}
}
相關(guān)文章
java實(shí)現(xiàn)簡(jiǎn)易貪吃蛇游戲
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)易貪吃蛇游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12
Java多線程 生產(chǎn)者消費(fèi)者模型實(shí)例詳解
這篇文章主要介紹了Java多線程 生產(chǎn)者消費(fèi)者模型實(shí)例詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
深入了解Java中的過濾器Filter和監(jiān)聽器Listener
這篇文章主要為大家詳細(xì)介紹了Java中的過濾器Filter和監(jiān)聽器Listener的使用以及二者的區(qū)別,文中的示例代碼講解詳細(xì),需要的可以參考一下2022-06-06
使用java代碼實(shí)現(xiàn)一個(gè)月內(nèi)不再提醒,通用到期的問題
這篇文章主要介紹了使用java代碼實(shí)現(xiàn)一個(gè)月內(nèi)不再提醒,通用到期的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01
java DataInputStream和DataOutputStream詳解及實(shí)例代碼
這篇文章主要介紹了java DataInputStream和DataOutputStream詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-01-01
Spring動(dòng)態(tài)代理實(shí)現(xiàn)日志功能詳解
這篇文章主要為大家詳細(xì)介紹了Spring動(dòng)態(tài)代理實(shí)現(xiàn)日志功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
淺談spring中isolation和propagation的用法
這篇文章主要介紹了淺談spring中isolation 和propagation的用法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07

