每隔半小时截屏(18000ms)保存在本地D盘目录下以时间戳作为文件名。
pom依赖:
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
源码:
package com.example.demo.screenshot;
import javax.imageio.ImageIO;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
public class GuiCamera {
private String fileName;
private String imageFormat;//图像文件的格式
private String defaultName="GuiCamera";
private String defaultImageFormat="jpg";
static int serialNum=0;
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
public GuiCamera(){
fileName=defaultName;
imageFormat=defaultImageFormat;
}
public GuiCamera(String s,String format) {
fileName=s;
imageFormat=format;
}
public void snapshot(){
try {
//拷贝屏幕到一个BufferedImage对象screenshot
BufferedImage screenshot=(new Robot()).createScreenCapture(
new Rectangle(0,0,(int)d.getWidth(),(int)d.getHeight()));
serialNum++;
//根据文件前缀变量和文件格式变量,自动生成文件名
String name=fileName+String.valueOf(serialNum)+"."+imageFormat;
System.out.println(name);
File f=new File(name);
System.out.println("Save File-"+name);
//将screenshot对象写入图像文件
ImageIO.write(screenshot, imageFormat, f);
System.out.println("..Finished");
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) throws InterruptedException, MessagingException {
while (true){
long currentTimeMillis=System.currentTimeMillis();
GuiCamera cam=new GuiCamera("D:\\"+currentTimeMillis,"png");
cam.snapshot();
//半小时
Thread.sleep(1800000);
}
}
}
【星光不问赶路人,加油各位!】