【问题标题】:java print screen two monitorsjava打印屏幕两个显示器
【发布时间】:2013-08-20 12:28:16
【问题描述】:

我正在尝试使用打印屏幕图像区域来获取 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"));

【问题讨论】:

    标签: java bufferedimage awtrobot multiple-monitors printscreen


    【解决方案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();
            }
    
        }
    }
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 如果我们只需要第二个屏幕……怎么样?我们可以相交吗?代码呢……
      【解决方案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"));
      

      【讨论】:

      • 谢谢您的回复,我试过了,它可以工作,但问题是:程序拍摄主显示器和右侧显示器的照片(但我左侧有第二个显示器)
      • 据我所知,如果您的屏幕通过工具包方法给出的矩形大小获取屏幕大小,则您会传递矩形大小。尝试更大的尺寸怎么样?也许它有效..
      • @LMG 问题不在于大小,问题在于位置,因为默认为 0,0 但这是在右侧屏幕上,左侧屏幕被忽略
      • 太好了,抱歉我没有阅读编辑的部分。应该可以工作,但是如果两个显示器的长度不相等怎么办?它一直持有吗??
      • @LMG 这是一个很好的评论,是的,实际代码不会涵盖这一点,但应该很容易保存第一个显示器的宽度
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-22
      • 1970-01-01
      • 2018-11-15
      • 2015-07-11
      相关资源
      最近更新 更多