是否可以使用 Java 截取輔助 Windows 10 桌面的螢幕截圖?

是否可以使用 Java 截取輔助 Windows 10 桌面的螢幕截圖?

如果我想截取具有多個適配器/螢幕的擴展桌面的螢幕截圖,我可以使用以下程式碼:

static void takeScreenShot(String path) throws AWTException, IOException {

    Rectangle screenRect = new Rectangle(0, 0, 0, 0);
    for (GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()) {
        screenRect = screenRect.union(gd.getDefaultConfiguration().getBounds());
    }
    BufferedImage capture = new Robot().createScreenCapture(screenRect);

    File output = new File(path);
    ImageIO.write(capture, "bmp", output);
}

但是,就我而言,我只有 1 個 VGA 適配器(板載)、1 個顯示器 (HDMI),而且我正在使用新的 Windows 10 虛擬桌面功能。

Windows10MultipleDesktops.png

如何使用類似的 Java 程式截取另一個虛擬桌面的螢幕截圖?

我嘗試將 (1920x2) 的寬度加倍Rectangle,但從像素 1920 到 3840,截取的螢幕截圖上全是黑色。

黑螢幕截圖.png

static void takeScreenShot(String path) throws AWTException, IOException {

    Rectangle screenRect = new Rectangle(0, 0, 1920*2, 1080);
    BufferedImage capture = new Robot().createScreenCapture(screenRect);

    File output = new File(path);
    ImageIO.write(capture, "bmp", output);
}

相關內容