有没有办法使用 Java 截取屏幕截图并将其保存为某种图像?
正如标题所示,很简单:可以只使用 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
不管你相信与否,你实际上可以使用
java.awt.Robot
“创建包含从屏幕读取的像素的图像”。然后您可以将该图像写入磁盘上的文件。我刚刚尝试过,整个事情的结果如下:
注意:这只会捕获主监视器。请参阅 GraphicsConfiguration 了解多显示器支持。
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:
NOTE: This will only capture the primary monitor. See GraphicsConfiguration for multi-monitor support.
我从来不喜欢使用 Robot,所以我制作了自己的简单方法来制作 JFrame 对象的屏幕截图:
I never liked using Robot, so I made my own simple method for making screenshots of JFrame objects:
如果您想捕获所有监视器,可以使用以下代码:
If you'd like to capture all monitors, you can use the following code:
bufferedImage 将包含完整的屏幕截图,这是用三个显示器进行测试的
bufferedImage will contain a full screenshot, this was tested with three monitors
Toolkit 根据 PPI 返回像素,因此,当使用 PPI> 时,不会为整个屏幕创建屏幕截图。 Windows 中 100%。
我建议这样做:
Toolkit returns pixels based on PPI, as a result, a screenshot is not created for the entire screen when using PPI> 100% in Windows.
I propose to do this:
您可以使用 java.awt.Robot 来完成此任务。
下面是服务器的代码,它将捕获的屏幕截图作为图像保存在您的目录中。
这是在线程上运行的客户端代码,几分钟后它就会捕获用户屏幕的屏幕截图。
You can use
java.awt.Robot
to achieve this task.below is the code of server, which saves the captured screenshot as image in your Directory.
And this is Client code which is running on thread and after some minutes it is capturing the screenshot of user screen.