Java實現(xiàn)屏幕截圖及剪裁
Java標(biāo)準(zhǔn)API中有個Robot類,該類可以實現(xiàn)屏幕截圖,模擬鼠標(biāo)鍵盤操作這些功能。這里只展示其屏幕截圖。
截圖的關(guān)鍵方法createScreenCapture(Rectangle rect) ,該方法需要一個Rectangle對象,Rectangle就是定義屏幕的一塊矩形區(qū)域,構(gòu)造Rectangle也相當(dāng)容易:
new Rectangle(int x, int y, int width, int height),四個參數(shù)分別是矩形左上角x坐標(biāo),矩形左上角y坐標(biāo),矩形寬度,矩形高度。截圖方法返回BufferedImage對象,示例代碼:
/**
* 指定屏幕區(qū)域截圖,返回截圖的BufferedImage對象
* @param x
* @param y
* @param width
* @param height
* @return
*/
public BufferedImage getScreenShot(int x, int y, int width, int height) {
BufferedImage bfImage = null;
try {
Robot robot = new Robot();
bfImage = robot.createScreenCapture(new Rectangle(x, y, width, height));
} catch (AWTException e) {
e.printStackTrace();
}
return bfImage;
}
如果需要把截圖保持為文件,使用ImageIO.write(RenderedImage im, String formatName, File output) ,示例代碼:
/**
* 指定屏幕區(qū)域截圖,保存到指定目錄
* @param x
* @param y
* @param width
* @param height
* @param savePath - 文件保存路徑
* @param fileName - 文件保存名稱
* @param format - 文件格式
*/
public void screenShotAsFile(int x, int y, int width, int height, String savePath, String fileName, String format) {
try {
Robot robot = new Robot();
BufferedImage bfImage = robot.createScreenCapture(new Rectangle(x, y, width, height));
File path = new File(savePath);
File file = new File(path, fileName+ "." + format);
ImageIO.write(bfImage, format, file);
} catch (AWTException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
捕捉屏幕截圖后,也許,我們需要對其剪裁。主要涉及兩個類CropImageFilter和FilteredImageSource,關(guān)于這兩個類的介紹,看java文檔把。
/**
* BufferedImage圖片剪裁
* @param srcBfImg - 被剪裁的BufferedImage
* @param x - 左上角剪裁點X坐標(biāo)
* @param y - 左上角剪裁點Y坐標(biāo)
* @param width - 剪裁出的圖片的寬度
* @param height - 剪裁出的圖片的高度
* @return 剪裁得到的BufferedImage
*/
public BufferedImage cutBufferedImage(BufferedImage srcBfImg, int x, int y, int width, int height) {
BufferedImage cutedImage = null;
CropImageFilter cropFilter = new CropImageFilter(x, y, width, height);
Image img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(srcBfImg.getSource(), cropFilter));
cutedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = cutedImage.getGraphics();
g.drawImage(img, 0, 0, null);
g.dispose();
return cutedImage;
}
如果剪裁后需要保存剪裁得到的文件,使用ImageIO.write,參考上面把截圖保持為文件的代碼。
相關(guān)文章
Java JVM運行時數(shù)據(jù)區(qū)(Run-Time Data Areas)
運行時數(shù)據(jù)區(qū),是java虛擬機定義的在程序執(zhí)行期間使用的各種運行時的數(shù)據(jù)區(qū),通過JVM運行時數(shù)據(jù)區(qū)圖例給大家展示的很詳細,對JVM 運行時數(shù)據(jù)區(qū)相關(guān)知識感興趣的朋友跟隨小編一起看看吧2021-06-06
springboot使用定時器@Scheduled不管用的解決
這篇文章主要介紹了springboot使用定時器@Scheduled不管用的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
詳解OpenCV For Java環(huán)境搭建與功能演示
這篇文章主要介紹了x詳解OpenCV For Java環(huán)境搭建與功能演示,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04
Java集合排序規(guī)則接口Comparator用法解析
這篇文章主要介紹了Java集合排序規(guī)則接口Comparator用法解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09
Graceful Response 構(gòu)建 Spring Boot 響應(yīng)
Graceful Response是一個Spring Boot技術(shù)棧下的優(yōu)雅響應(yīng)處理器,提供一站式統(tǒng)一返回值封裝、全局異常處理、自定義異常錯誤碼等功能,本文介紹Graceful Response 構(gòu)建 Spring Boot 下優(yōu)雅的響應(yīng)處理,感興趣的朋友一起看看吧2024-01-01

