java print screen two monitors

巧了我就是萌 提交于 2019-12-21 04:23:09

问题


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 robot = new Robot();    
        Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
        BufferedImage capture = new Robot().createScreenCapture(screenRect);
        ImageIO.write(capture, "bmp", new File("printscreen.bmp"));

回答1:


Union together the bounds of each screen:

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



回答2:


You could try:

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

This should calculate the total width of multiple screens. Obviously it only supports horizontally aligned screens in the form above - you'd have to analyse the bounds of the graphics configurations to handle other monitor alignments (depends how bulletproof you want to make it).

If your Main Monitor is on the right side and you want to get an image even with the left side, use this:

Rectangle screenRect = new Rectangle(-(width / 2), 0, width, height);
BufferedImage capture = new Robot().createScreenCapture(screenRect);
ImageIO.write(capture, "bmp", new File("printscreen.bmp"));



回答3:


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

    }
}


来源:https://stackoverflow.com/questions/18301429/java-print-screen-two-monitors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!