下面的问题应该观察屏幕,记录一个事件(一个测量文本框变成绿色)并记录所有导致它的事件,产生一个导致它的事件的“电影”。不幸的是,需要记录整个屏幕。到目前为止,我已经完成了识别参与的部分。但是我每秒几乎没有两帧。我想要大约25 到 30 fps。
我的想法是在两个单独的线程中进行写作和阅读。因为写入事件很少,并且可以在后台运行,所以录制事件会占用更多的时间并且运行得更快。不幸的是,整个事情似乎太慢了。我希望能够在事件发生前10 到 20 秒将屏幕写入磁盘。
编辑:如果可能的话,我想尽可能地保持平台独立。
编辑 2:Xuggler 似乎有一个独立于平台的 jar 文件。不幸的是,我真的不知道如何将它用于我的目的:记录导致 isarecord 被触发点的 20 秒。
这是我到目前为止所做的:
package fragrecord;
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
public class Main {
public static void main(String[] args) {
//The numbers are just silly tune parameters. Refer to the API.
//The important thing is, we are passing a bounded queue.
ExecutorService consumer = new ThreadPoolExecutor(1,4,30,TimeUnit.SECONDS,new LinkedBlockingQueue<Runnable>(100));
System.out.println("starting");
//No need to bound the queue for this executor.
//Use utility method instead of the complicated Constructor.
ExecutorService producer = Executors.newSingleThreadExecutor();
Runnable produce = new Produce(consumer);
producer.submit(produce);
try {
producer.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
consumer.shutdown();
try {
consumer.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Produce implements Runnable {
private final ExecutorService consumer;
public Produce(ExecutorService consumer) {
this.consumer = consumer;
}
boolean isarecord(BufferedImage image){
int x=10, y = 10;
Color c = new Color(image.getRGB(x,y));
int red = c.getRed();
int green = c.getGreen();
int blue = c.getBlue();
// Determine whether to start recording
return false;
}
@Override
public void run() {
Robot robot = null;
try {
robot = new Robot();
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//
// Capture screen from the top left to bottom right
//
int i = 0;
while(true) {
i++;
BufferedImage bufferedImage = robot.createScreenCapture(
new Rectangle(new Dimension(1024, 798)));
Runnable consume = new Consume(bufferedImage,i);
consumer.submit(consume);
}
}
}
class Consume implements Runnable {
private final BufferedImage bufferedImage;
private final Integer picnr;
public Consume(BufferedImage bufferedImage, Integer picnr){
this.bufferedImage = bufferedImage;
this.picnr = picnr;
}
@Override
public void run() {
File imageFile = new File("screenshot"+picnr+".png");
try {
System.out.println("screenshot"+picnr+".png");
ImageIO.write(bufferedImage, "png", imageFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}