本文主要是介绍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截屏的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!