12

我正在尝试使用打印屏幕图像区域来获取 2 台显示器,但仅适用于一台显示器。你能告诉我如何获得数字 2 显示器吗?

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

3 回答 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);
于 2013-08-18T17:32:57.350 回答
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"));
于 2013-08-18T16:49:24.387 回答
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();
        }

    }
}
于 2016-06-19T03:08:42.120 回答