java robot怎么建立_Java如何使用Robot类创建屏幕截图?

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();

// 从左上方以200 x 200像素的尺寸捕获屏幕。

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();

}

}

}

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(); // 从左上方以200 x 200像素的尺寸捕获屏幕。 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(); } } }
经验分享 程序员 微信小程序 职场和发展