我最近研究了如何使用Robot类在 java 中捕获屏幕截图。我为此实现了以下代码。
public class Beginning implements Runnable {
private Thread thread;
private static long counter = 0;
private final int FRAME_CAPTURE_RATE = 124;
private Robot robot;
public Beginning() throws Exception {
robot = new Robot();
thread = new Thread(this);
thread.start();
}
public static void main(String[] args) throws Exception {
Beginning beginning = new Beginning();
}
public void run() {
for (;;) {
try {
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage bufferedImage = robot.createScreenCapture(screenRect);
ImageIO.write(bufferedImage, "png", new File("D:\\CapturedFrame\\toolImage" + counter + ".png"));
counter++;
thread.sleep(FRAME_CAPTURE_RATE);
} catch (Exception e) {
System.err.println("Something fishy is going on...");
}
}
}
}
作为此代码的输出,我得到尺寸为 1024x1024 的图像,但这些不是高清图像,谁能建议我如何修改此代码以捕获高清格式的屏幕截图。提前致谢。
我只想这样,目前在我存储的 .png 图像中,单个像素值以 8 位存储,我想将其更改为 16 位或更高。