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

首頁 > 學院 > 開發(fā)設(shè)計 > 正文

Java屏幕截圖及剪裁

2019-11-14 21:08:29
字體:
供稿:網(wǎng)友
java屏幕截圖及剪裁

  Java標準API中有個Robot類,該類可以實現(xiàn)屏幕截圖,模擬鼠標鍵盤操作這些功能。這里只展示其屏幕截圖。

  截圖的關(guān)鍵方法createScreenCapture(Rectangle rect) ,該方法需要一個Rectangle對象,Rectangle就是定義屏幕的一塊矩形區(qū)域,構(gòu)造Rectangle也相當容易:

newRectangle(int x, int y, int width, int height),四個參數(shù)分別是矩形左上角x坐標,矩形左上角y坐標,矩形寬度,矩形高度。截圖方法返回BufferedImage對象,示例代碼:

 1     /** 2      * 指定屏幕區(qū)域截圖,返回截圖的BufferedImage對象 3      * @param x 4      * @param y 5      * @param width 6      * @param height 7      * @return  8      */ 9     public BufferedImage getScreenShot(int x, int y, int width, int height) {10         BufferedImage bfImage = null;11         try {12             Robot robot = new Robot();13             bfImage = robot.createScreenCapture(new Rectangle(x, y, width, height));14         } catch (AWTException e) {15             e.PRintStackTrace();16         }17         return bfImage;18     }

如果需要把截圖保持為文件,使用ImageIO.write(RenderedImage im, String formatName, File output) ,示例代碼:

 1     /** 2      * 指定屏幕區(qū)域截圖,保存到指定目錄 3      * @param x 4      * @param y 5      * @param width 6      * @param height 7      * @param savePath - 文件保存路徑 8      * @param fileName - 文件保存名稱 9      * @param format - 文件格式10      */11     public void screenShotAsFile(int x, int y, int width, int height, String savePath, String fileName, String format) {12         try {13             Robot robot = new Robot();14             BufferedImage bfImage = robot.createScreenCapture(new Rectangle(x, y, width, height));15             File path = new File(savePath);16             File file = new File(path, fileName+ "." + format);17             ImageIO.write(bfImage, format, file);18         } catch (AWTException e) {19             e.printStackTrace();    20         } catch (IOException e) {21             e.printStackTrace();22         }23     }

捕捉屏幕截圖后,也許,我們需要對其剪裁。主要涉及兩個類CropImageFilter和FilteredImageSource,關(guān)于這兩個類的介紹,看java文檔把。

 1     /** 2      * BufferedImage圖片剪裁 3      * @param srcBfImg - 被剪裁的BufferedImage 4      * @param x - 左上角剪裁點X坐標 5      * @param y - 左上角剪裁點Y坐標 6      * @param width - 剪裁出的圖片的寬度 7      * @param height - 剪裁出的圖片的高度 8      * @return 剪裁得到的BufferedImage 9      */10     public BufferedImage cutBufferedImage(BufferedImage srcBfImg, int x, int y, int width, int height) {11         BufferedImage cutedImage = null;12         CropImageFilter cropFilter = new CropImageFilter(x, y, width, height);  13         Image img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(srcBfImg.getSource(), cropFilter));  14         cutedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  15         Graphics g = cutedImage.getGraphics();  16         g.drawImage(img, 0, 0, null);  17         g.dispose(); 18         return cutedImage;19     }

如果剪裁后需要保存剪裁得到的文件,使用ImageIO.write,參考上面把截圖保持為文件的代碼。


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 句容市| 子长县| 武定县| 博爱县| 金坛市| 合川市| 清镇市| 台江县| 南雄市| 霍州市| 南靖县| 永昌县| 无锡市| 会理县| 辽宁省| 灵武市| 阜阳市| 长岛县| 平度市| 九龙县| 自治县| 唐山市| 和顺县| 黎城县| 沁源县| 伊吾县| 美姑县| 如皋市| 乌鲁木齐县| 河南省| 沂南县| 府谷县| 金溪县| 新源县| 农安县| 江川县| 斗六市| 长顺县| 雷州市| 法库县| 镇康县|