如何在 Java 中截取屏幕截图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4490454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 18:00:08 来源:igfitidea点击:
How to take a screenshot in Java?
提问by Adesara
Possible Duplicate:
Is there a way to take a screenshot using Java and save it to some sort of image?
How to take a screenshot in Java?
如何在 Java 中截取屏幕截图?
采纳答案by BalusC
Use Robot#createScreenCapture()
.
使用Robot#createScreenCapture()
.
BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(image, "png", new File("/screenshot.png"));
回答by Raj Gupta
You can find this code useful. this code will take screenshot in every 10 seconds
您会发现此代码很有用。此代码将每 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);
}
}
}