java打印屏幕两个监视器

java打印屏幕两个监视器,java,bufferedimage,awtrobot,multiple-monitors,printscreen,Java,Bufferedimage,Awtrobot,Multiple Monitors,Printscreen,我试图使用打印屏幕图像区域来获得两个显示器,但只适用于一个显示器。你能告诉我如何获得图2显示器吗 Robot robot = new Robot(); Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); BufferedImage capture = new Robot().createScreenCapture(screenR

我试图使用打印屏幕图像区域来获得两个显示器,但只适用于一个显示器。你能告诉我如何获得图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"));
你可以试试:

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

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

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

这是我使用和测试过的代码,它很有效,它在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();
        }

    }
}

谢谢你的回复,我试过了,但问题是:程序拍摄了主监视器和右侧监视器的照片(但我在左侧有第二个监视器),据我所知,如果你的屏幕通过工具箱方法get screen size给出的矩形大小,你可以通过。试试大号的怎么样??可能有用。@LMG问题不在于尺寸,而在于位置,因为默认值为0,0,但这在右屏幕上,左屏幕忽略了多好,对不起,我没有阅读编辑的部分。。应该可以工作,但是如果两个监视器的长度不相等怎么办?它是否保持不变???@LMG这是一个很好的评论,是的,实际的代码不会涵盖这一点,但它应该很容易保存第一个显示器的宽度,如果我们只需要第二个屏幕如何。。。。?我们能做到相交吗?那代码呢。。。。