编辑:如果有其他建议可以提高屏幕捕捉性能,请随时分享,可能会完全解决我的问题!
大家好,开发者们:
我正在为自己编写一些基本的屏幕捕捉软件。截至目前,我已经拥有了一些概念验证/调试代码,它使用java.awt.Robot来捕捉屏幕作为BufferedImage。然后,我会对此进行捕捉指定时间,并在之后将所有图片转储到磁盘中。根据我的测试,我每秒得到约17帧。
试用#1
持续时间:15秒 捕获图像:255个
试用#2
持续时间:15秒 捕获图像:229个
显然,这对于真正的屏幕捕获应用程序来说还远远不够好。特别是因为这些捕获只是我在IDE中选择文本而已,没有任何涉及图形方面的内容。
我现在有两个类,一个是Main类,另一个是"Monitor"类。Monitor类包含捕捉屏幕的方法。我的Main类有一个基于时间的循环,调用Monitor类并将其返回的BufferedImage存储到一个ArrayList中。 如果我修改我的主类以生成多个线程,每个线程执行该循环并收集关于捕获图像时间的信息,那么我能提高性能吗?我的想法是使用一个共享数据结构,在插入它们时自动根据捕获时间对帧进行排序,而不是将连续的图像插入到ArrayList中的单个循环。
代码:
监视器
public class Monitor {
/**
* Returns a BufferedImage
* @return
*/
public BufferedImage captureScreen() {
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage capture = null;
try {
capture = new Robot().createScreenCapture(screenRect);
} catch (AWTException e) {
e.printStackTrace();
}
return capture;
}
}
主要
public class Main {
public static void main(String[] args) throws InterruptedException {
String outputLocation = "C:\\Users\\ewillis\\Pictures\\screenstreamer\\";
String namingScheme = "image";
String mediaFormat = "jpeg";
DiscreteOutput output = DiscreteOutputFactory.createOutputObject(outputLocation, namingScheme, mediaFormat);
ArrayList<BufferedImage> images = new ArrayList<BufferedImage>();
Monitor m1 = new Monitor();
long startTimeMillis = System.currentTimeMillis();
long recordTimeMillis = 15000;
while( (System.currentTimeMillis() - startTimeMillis) <= recordTimeMillis ) {
images.add( m1.captureScreen() );
}
output.saveImages(images);
}
}