简体   繁体   English

java.awt.Robot-屏幕截图需要多少时间

[英]java.awt.Robot - How much time screenshot takes

I just faced a strange issue; 我只是面临一个奇怪的问题。 As I can get it the screen capturing code takes not-fixed time: 如我所知,屏幕捕获代码花费的时间不固定:

startTime = System.currentTimeMillis();                 

endTime   = System.currentTimeMillis();
totalTime = endTime - startTime;
System.out.println(this.getId()+" A: TIME TAKEN BETWEEN SCREENSHOTS: "+totalTime);


java.awt.image.BufferedImage image = new java.awt.Robot().createScreenCapture(new java.awt.Rectangle(250,150,500,500));

endTime   = System.currentTimeMillis();
totalTime = endTime - startTime;
System.out.println(this.getId()+" B: TIME TAKEN BETWEEN SCREENSHOTS: "+totalTime);

which is quite strange cause sometimes it is 108mls and next time it is lets say 480mls :( So its pretty hard to figure out the screenshot algorithm :S 这是很奇怪的原因,有时是108mls,下一次是480mls :(因此很难确定截图算法:S

So my question is... how to find out what time a screenshot will take or how to make screenshot time fixable (limited) in 0.5sec only? 所以我的问题是...如何找出屏幕截图将花费什么时间,或者如何使屏幕截图时间仅在0.5秒内可修复(受限制)?

ps PS

  • jre 1.7 杰瑞1.7

  • jdk 1.7 杰克1.7

  • OS: linux 操作系统:Linux

Thanks 谢谢

I'm not entirely sure what you are trying to accomplish, but I don't think it's likely you can limit the time the code executes, or force it to run less then 500 ms. 我不确定您要完成的工作,但我认为您可能无法限制代码执行的时间,也可以强制其运行少于500毫秒。

Maybe the following could be a solution to the problem? 也许以下可能是该问题的解决方案?

You could perform the task of creating a screenshot in a different thread, then in the main thread you can check any condition you want (for instance how long it took to create the screenshot). 您可以在另一个线程中执行创建屏幕截图的任务,然后在主线程中可以检查所需的任何条件(例如,创建屏幕截图所花费的时间)。

If the screenshot taking thread hasn't responded within the timelimit, or if other conditions have or haven't been met you can discard the result of the screenshot taking thread. 如果截屏线程在时限内没有响应,或者是否满足其他条件,则可以丢弃截屏线程的结果。

This post ( Killing thread after some specified time limit in Java ) also has some suggestion to actually kill the thread if it hasn't finished in time. 这篇文章( 在Java中指定的时间限制杀死线程 )也提出了一些建议,如果未及时完成,则实际上要终止该线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM