有没有办法使用Java截取屏幕截图并将其保存为某种图像? [英] Is there a way to take a screenshot using Java and save it to some sort of image?

查看:220
本文介绍了有没有办法使用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?

推荐答案

相信它或不,您实际上可以使用 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截取屏幕截图并将其保存为某种图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆