Java如何在两个显示器上进行屏幕截图

12

我想使用打印屏幕图像区域来获取两个显示器的图像,但只能获取一个显示器的图像。请问您如何让它适用于两个显示器?

        Robot robot = new Robot();    
        Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
        BufferedImage capture = new Robot().createScreenCapture(screenRect);
        ImageIO.write(capture, "bmp", new File("printscreen.bmp"));
3个回答

21

将每个屏幕的边界合并在一起:

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);

1
如果我们只需要第二个屏幕怎么办?我们能做交集吗?代码怎么样? - gumuruh

3
您可以尝试以下方法:
int width = 0;
int height = 0;

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();

for (GraphicsDevice curGs : gs)
{
  DisplayMode mode = curGs.getDisplayMode();
  width += mode.getWidth();
  height = mode.getHeight();
}

这应该计算多个屏幕的总宽度。显然,它只支持上述形式中水平对齐的屏幕 - 您需要分析图形配置的边界来处理其他监视器对齐方式(取决于您要使其多么健壮)。
如果您的主监视器在右侧,并且您想获得与左侧相同的图像,请使用以下内容:
Rectangle screenRect = new Rectangle(-(width / 2), 0, width, height);
BufferedImage capture = new Robot().createScreenCapture(screenRect);
ImageIO.write(capture, "bmp", new File("printscreen.bmp"));

谢谢您的回复,我尝试了一下,它可以工作,但问题是:程序拍摄的是主监视器和右侧的监视器(但我有第二个监视器在左边)。 - Faken143
据我所理解,您传递的是工具包方法获取屏幕大小的矩形尺寸。那么尝试使用更大的尺寸呢?也许会起作用。 - LMG
@LMG,问题不在于大小,而在于位置,因为默认情况下它是0,0,但这是在右屏幕上,左屏幕被忽略了。 - Dennis Kriechel
太好了,抱歉我没有读编辑部分...应该可以工作,但如果两个显示器长度不相等怎么办?它会继续保持吗? - LMG
@LMG 这是一个好评论,实际的代码不会涉及到这个问题,但保存第一台显示器的宽度应该很容易。 - Dennis Kriechel

1
这是我使用并测试过的代码,它可以工作,在res文件夹中创建了两个png文件(请将其更改为您的文件夹),一个用于我的主屏幕,另一个用于辅助屏幕。 我还打印了有关显示器边界信息,如果您想要在一张图片中显示两个显示器,只需添加两个监视器的宽度即可。
public static void screenMultipleMonitors() {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] gDevs = ge.getScreenDevices();

    for (GraphicsDevice gDev : gDevs) {
        DisplayMode mode = gDev.getDisplayMode();
        Rectangle bounds = gDev.getDefaultConfiguration().getBounds();
        System.out.println(gDev.getIDstring());
        System.out.println("Min : (" + bounds.getMinX() + "," + bounds.getMinY() + ") ;Max : (" + bounds.getMaxX()
                + "," + bounds.getMaxY() + ")");
        System.out.println("Width : " + mode.getWidth() + " ; Height :" + mode.getHeight());

        try {
            Robot robot = new Robot();

            BufferedImage image = robot.createScreenCapture(new Rectangle((int) bounds.getMinX(),
                    (int) bounds.getMinY(), (int) bounds.getWidth(), (int) bounds.getHeight()));
            ImageIO.write(image, "png",
                    new File("src/res/screen_" + gDev.getIDstring().replace("\\", "") + ".png"));

        } catch (AWTException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接