Java截图功能实现 - 使用Robot类获取屏幕截图
在Java中,可以使用Robot类来实现截图功能。以下是一个实现截图功能的示例代码:
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class ScreenShot {
public static void main(String[] args) {
try {
// 创建Robot对象
Robot robot = new Robot();
// 获取屏幕大小
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
// 截取屏幕图像
BufferedImage image = robot.createScreenCapture(screenRect);
// 将截图保存为文件
File file = new File('screenshot.png');
ImageIO.write(image, 'png', file);
System.out.println('截图成功,保存为:' + file.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行该程序后,会在当前目录下生成一个名为'screenshot.png'的截图文件。可以根据需要调整截图范围和保存文件的路径。
原文地址: https://www.cveoy.top/t/topic/oa5L 著作权归作者所有。请勿转载和采集!