问题描述
正如标题所说的那样简单:您可以只使用 Java 命令来截取屏幕截图并保存吗?或者,我是否需要使用特定于操作系统的程序来截取屏幕截图,然后将其从剪贴板上抓取?
Simple as the title states: Can you use only Java commands to take a screenshot and save it? Or, do I need to use an OS specific program to take the screenshot and then grab it off the clipboard?
推荐答案
信不信由你,你实际上可以使用 java.awt.Robot
以创建包含从屏幕读取的像素的图像."然后,您可以将该图像写入磁盘上的文件.
Believe it or not, you can actually use java.awt.Robot
to "create an image containing pixels read from the screen." You can then write that image to a file on disk.
我刚刚试了一下,整个事情的结局是这样的:
I just tried it, and the whole thing ends up like:
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage capture = new Robot().createScreenCapture(screenRect);
ImageIO.write(capture, "bmp", new File(args[0]));
注意:这只会捕获主监视器.有关多显示器支持,请参阅GraphicsConfiguration.
NOTE: This will only capture the primary monitor. See GraphicsConfiguration for multi-monitor support.
这篇关于有没有办法使用Java截取屏幕截图并将其保存为某种图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!