java截屏
作者:互联网
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.filechooser.FileSystemView;
/**
* @author ycs
*/
public class ScreenCamera {
/**
* 桌面路径
*/
public static final String DESKTOP_PATH =
FileSystemView.getFileSystemView().getHomeDirectory().getAbsolutePath();
/**
* 文件名
*/
public static String FILENAME = "screen";
/**
* 编号
*/
public static int SERIAL_NUM = 0;
/**
* 图像文件的格式
*/
public static final String FORMAT = "jpg";
/**
* 屏幕尺寸
*/
public static Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
/**
* 截屏
**/
public static void snapshot() {
try {
// 拷贝屏幕到缓存区
BufferedImage screenshot = new Robot().createScreenCapture(
new Rectangle(0, 0, (int) dimension.getWidth(), (int) dimension.getHeight()));
SERIAL_NUM++;
// 生成路径 + 文件名
String pathName = DESKTOP_PATH + "/" + FILENAME + SERIAL_NUM + "." + FORMAT;
// 将screenshot图像写入文件
ImageIO.write(screenshot, FORMAT, new FileOutputStream(pathName));
System.out.println("Finish, Save File - " + pathName);
} catch (Exception e) {
throw new RuntimeException("截屏失败!\n" + e);
}
}
/**
* 执行程序
*/
public static void exec() {
try {
Runtime.getRuntime().exec("C:\\Windows\\system32\\calc.exe");
} catch (IOException e) {
throw new RuntimeException("执行程序失败!\n" + e);
}
}
public static void main(String[] args) throws InterruptedException {
// 测试
exec();
Thread.sleep(1000);
snapshot();
}
}
标签:java,String,截屏,static,new,import,public 来源: https://blog.csdn.net/weixin_44215249/article/details/122195281