java print screen two monitors

前端 未结 3 620
遇见更好的自我
遇见更好的自我 2021-02-13 20:03

I\'m trying to use print screen image area to get 2 monitors, but only works for one monitor. Can you advise me how to get figure 2 monitors?

        Robot         


        
3条回答
  •  逝去的感伤
    2021-02-13 20:35

    This is the code I have used and tested , it works , it creates two png files inside the res folder(change it to your folder) one for my primary and the other for the secondary screen . i had also printed the Bounds information about the Displays , if you want both displays in one image , just add the width of both monitors and you will have it

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

提交回复
热议问题