我试图使用打印屏幕图像区域来获得两个显示器,但只适用于一个显示器。你能告诉我如何获得图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"));
将每个屏幕的边界合并在一起:
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);
您可以尝试:
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"));
这是我使用和测试过的代码,它很有效,它在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();
}
}
}