gpt4 book ai didi

java - java.awt.Robot.createScreenCapture 的更快替代品?

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

我正在制作一个程序,要求每秒至少捕获 24 个屏幕截图。目前使用下面的代码,我每大约 94 毫秒只能得到 1 个,所以大约每秒 10 个。

我不想使用任何第 3 方库,因为我试图让它尽可能小,但如果我能获得显着的性能提升,我愿意这样做。我也在努力保持这个平台的独立性,但同样,如果这真的能带来显着的性能提升,我愿意将它限制在 Windows 上。

编辑:我现在也尝试了两种不同的方式;使用在 oracles 网站上找到的片段和下面评论中指出的片段。这三个时间几乎相同,为 2.1-2.2 百万纳秒,这非常低效。

public abstract class Benchmark {

private final int iterations;

public Benchmark(int iterations) {
this.iterations = iterations;
}

public abstract void logic();

public void start() {
long start = System.nanoTime();
for (int iteration = 0; iteration < iterations; iteration++) {
long iterationStart = System.nanoTime();
logic();
System.out.println("iteration: " + iteration + " took: " + (System.nanoTime() - iterationStart) + " nanoseconds.");
}
long total = (System.nanoTime() - start);
System.out.println(iterations + " iterations took: " + total + " nanoseconds. Average iteration was: " + (total / iterations));
}
}

_

import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;

public class RobotBenchmark extends Benchmark {

private final Robot robot;
private final Rectangle screen;

public static void main(String[] args) {
Benchmark benchmark;
try {
benchmark = new RobotBenchmark(24);
benchmark.start();
} catch (AWTException e) {
e.printStackTrace();
}
}

public RobotBenchmark(int iterations) throws AWTException {
super(iterations);
robot = new Robot();
screen = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
}

@Override
public void logic() {
robot.createScreenCapture(screen);
}

}

_

import java.awt.AWTException;
import java.awt.GraphicsDevice;
import java.awt.HeadlessException;
import java.awt.Rectangle;

public class DirectRobotBenchmark extends Benchmark {

private final GraphicsDevice device;
private final Rectangle screenRectangle;
private final DirectRobot robot;

private int[] screen;

public static void main(String[] args) {
Benchmark benchmark;
try {
benchmark = new DirectRobotBenchmark(24);
benchmark.start();
} catch (HeadlessException | AWTException e) {
e.printStackTrace();
}
}

public DirectRobotBenchmark(int iterations) throws HeadlessException, AWTException {
super(iterations);
device = DirectRobot.getDefaultScreenDevice();
screenRectangle = new Rectangle(1920, 1080);
robot = new DirectRobot(device);
screen = new int[screenRectangle.width * screenRectangle.height];
}

@Override
public void logic() {
screen = robot.getRGBPixels(screenRectangle);
}
}

_

import java.awt.AWTException;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.peer.RobotPeer;

import sun.awt.SunToolkit;

@SuppressWarnings("restriction")
public class RobotPeerBenchmark extends Benchmark {

private final SunToolkit toolkit;
private final RobotPeer peer;
private final Rectangle screenRectangle;

private int[] screen;

public static void main(String[] args) {
try {
Benchmark robotPeerBenchmark = new RobotPeerBenchmark(24);
robotPeerBenchmark.start();
} catch (AWTException e) {
e.printStackTrace();
}
}

public RobotPeerBenchmark(int iterations) throws AWTException {
super(iterations);
toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
peer = toolkit.createRobot(new Robot(), GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice());
screenRectangle = new Rectangle(toolkit.getScreenSize());
screen = new int[screenRectangle.width * screenRectangle.height];
}

@Override
public void logic() {
screen = peer.getRGBPixels(screenRectangle);
}
}

最佳答案

唯一的方法是通过 JNI 或 JNA。我做了一些基准测试和 native 屏幕捕获 API,它能够维持大约 45 FPS,而 Robots 为 8 FPS。我可能会在不久的将来开始一个 JNI 项目来解决这个问题。如果继续进行,我将使用项目 URL 更新这篇文章。

关于java - java.awt.Robot.createScreenCapture 的更快替代品?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17665529/

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