Java如何使用Robot类创建屏幕截图?
在此示例中,您将看到如何创建屏幕截图/屏幕截图并将其另存为图像文件(例如PNG图像)。有些类是在这个方案包括使用java.awt.Robot,java.awt.image.BufferedImage和javax.imageio.ImageIO。
package org.nhooo.example.awt;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.io.File;
import java.io.IOException;
public class ScreenCapture {
public static void main(String[] args) {
try {
Robot robot = new Robot();
//从左上方以200x200像素的尺寸捕获屏幕。
BufferedImage bufferedImage = robot.createScreenCapture(
new Rectangle(new Dimension(200, 200)));
//捕获的图像将被写入一个名为
//screenshot.png
File imageFile = new File("screenshot.png");
ImageIO.write(bufferedImage, "png", imageFile);
} catch (AWTException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短