可能重复:
Is there a way to take a screenshot using Java and save it to some sort of image?
如何用Java截取屏幕截图?
答案 0 :(得分:77)
使用Robot#createScreenCapture()
。
BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(image, "png", new File("/screenshot.png"));
答案 1 :(得分:21)
您可以找到有用的代码。此代码将每隔10秒拍摄一次屏幕截图
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;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
public class screen2image
{
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd hh mm ss a");
public void robo() throws Exception
{
Calendar now = Calendar.getInstance();
Robot robot = new Robot();
BufferedImage screenShot = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(screenShot, "JPG", new File("d:\\"+formatter.format(now.getTime())+".jpg"));
System.out.println(formatter.format(now.getTime()));
}
public static void main(String[] args) throws Exception
{
screen2image s2i = new screen2image();
while(1==1)
{
s2i.robo();
Thread.sleep(10000);
}
}
}