java代碼實(shí)現(xiàn)截圖功能(屏幕截圖)
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優(yōu)先隊(duì)列?priority?queue
本文主要介紹了Java優(yōu)先隊(duì)列?priority?queue,優(yōu)先隊(duì)列是一種特殊的數(shù)據(jù)結(jié)構(gòu)隊(duì)列中每一個(gè)元素都被分配到一個(gè)優(yōu)先權(quán)值,出隊(duì)順序按照優(yōu)先權(quán)值來劃分。一般有兩種出隊(duì)順序高優(yōu)先權(quán)出隊(duì)或低優(yōu)先權(quán)出隊(duì),想了解具體內(nèi)容的小伙伴可以參考下文內(nèi)容,希望對(duì)你有所幫助2021-12-12idea為java程序添加啟動(dòng)參數(shù)的問題解析(program?arguments,vm?arguments,Envi
這篇文章主要介紹了idea為java程序添加啟動(dòng)參數(shù)的問題解析(program?arguments,vm?arguments,Environment?variable)并在程序中獲取使用,本文給大家分享問題描述及解決方法,需要的朋友可以參考下2023-09-09Springboot+Mybatis實(shí)現(xiàn)分頁(yè)加條件查詢功能
這篇文章主要為大家詳細(xì)介紹了Springboot+Mybatis實(shí)現(xiàn)分頁(yè)加條件查詢,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Spring Cloud應(yīng)用實(shí)現(xiàn)配置自動(dòng)刷新過程詳解
這篇文章主要介紹了Spring Cloud應(yīng)用實(shí)現(xiàn)配置自動(dòng)刷新過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12Springboot項(xiàng)目對(duì)數(shù)據(jù)庫(kù)用戶名密碼實(shí)現(xiàn)加密過程解析
這篇文章主要介紹了Springboot項(xiàng)目對(duì)數(shù)據(jù)庫(kù)用戶名密碼實(shí)現(xiàn)加密過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06arthas?jprofiler做復(fù)雜鏈路的調(diào)用分析
這篇文章主要為大家介紹了arthas?jprofiler做復(fù)雜鏈路的調(diào)用分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06淺談java反射和自定義注解的綜合應(yīng)用實(shí)例
本篇文章主要介紹了java反射和自定義注解的綜合應(yīng)用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09