正如标题所说的那样简单:您只能使用Java命令来截取并保存屏幕截图吗?或者,我是否需要使用特定于操作系统的程序来截取屏幕截图,然后将其从剪贴板上抓取?
发布于 2008-09-12 04:56:21
信不信由你,实际上你可以使用java.awt.Robot
来“创建一个包含从屏幕读取的像素的图像”。然后,您可以将该映像写入磁盘上的文件。
我刚试过,整件事最终就像这样:
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage capture = new Robot().createScreenCapture(screenRect);
ImageIO.write(capture, "bmp", new File(args[0]));
注:这将仅捕获主监视器。有关多显示器支持,请参阅GraphicsConfiguration。
发布于 2012-05-29 17:01:25
我从来不喜欢使用机器人,所以我制作了自己的简单方法来制作JFrame对象的屏幕截图:
public static final void makeScreenshot(JFrame argFrame) {
Rectangle rec = argFrame.getBounds();
BufferedImage bufferedImage = new BufferedImage(rec.width, rec.height, BufferedImage.TYPE_INT_ARGB);
argFrame.paint(bufferedImage.getGraphics());
try {
// Create temp file
File temp = File.createTempFile("screenshot", ".png");
// Use the ImageIO API to write the bufferedImage to a temporary file
ImageIO.write(bufferedImage, "png", temp);
// Delete temp file when program exits
temp.deleteOnExit();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
发布于 2013-08-10 06:17:48
如果你想捕获所有的监视器,你可以使用下面的代码:
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] screens = ge.getScreenDevices();
Rectangle allScreenBounds = new Rectangle();
for (GraphicsDevice screen : screens) {
Rectangle screenBounds = screen.getDefaultConfiguration().getBounds();
allScreenBounds.width += screenBounds.width;
allScreenBounds.height = Math.max(allScreenBounds.height, screenBounds.height);
}
Robot robot = new Robot();
BufferedImage screenShot = robot.createScreenCapture(allScreenBounds);
https://stackoverflow.com/questions/58305
复制相似问题