JAVA實現截圖小Demo
阿新 • • 發佈:2018-12-12
閒的沒事,寫個從書上看到的一個小例子,java獲取電腦螢幕內容,類似於qq截圖吧。直接上程式碼,都有註釋比較簡單。
public class ScreenPhoto { public static void main(String[] args) throws AWTException, IOException { // 獲取螢幕尺寸大小 Dimension size = Toolkit.getDefaultToolkit().getScreenSize(); Rectangle rectangle = new Rectangle(size); Robot robot = new Robot(); // 擷取螢幕 BufferedImage image = robot.createScreenCapture(rectangle); File screenFile = new File("D:"); if (!screenFile.exists()) { screenFile.mkdir(); } File f = new File(screenFile, "1.png"); // 寫入檔案 ImageIO.write(image, "png", f); // 自動開啟 if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.OPEN)) Desktop.getDesktop().open(f); } }
最後貼一張效果圖