gpt4 book ai didi

java - 从屏幕捕获并保存到磁盘多线程

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:51:11 26 4
gpt4 key购买 nike

接下来的问题应该是观察屏幕、记录一个事件(测量文本框变为绿色)并记录导致它发生的所有事件,从而制作出导致它发生的事件的“电影”。不幸的是,需要记录整个屏幕。到目前为止,我已经完成了认可的部分。但是我每秒几乎没有两帧。我想要大约 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();
}
}
}

最佳答案

我试着稍微编辑一下你的代码,而不是创建 png 文件,我尝试创建 bmp 文件,它消除了数据压缩的开销时间,但代价是磁盘空间。

结果:我不知道如何计算 fps,但解决方案比您的解决方案更快。 :-)

关于java - 从屏幕捕获并保存到磁盘多线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12346762/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com