Increasing screen capture speed when using Java and awt.Robot

后端 未结 2 1351
生来不讨喜
生来不讨喜 2021-01-04 10:26

Edit: If anyone also has any other recommendations for increasing performance of screen capture please feel free to share as it might fully address my problem!

Hello

相关标签:
2条回答
  • 2021-01-04 10:56

    Re-using the screen rectangle and robot class instances will save you a little overhead. The real bottleneck is storing all your BufferedImage's into an array list.

    I would first benchmark how fast your robot.createScreenCapture(screenRect); call is without any IO (no saving or storing the buffered image). This will give you an ideal throughput for the robot class.

    long frameCount = 0;
    while( (System.currentTimeMillis() - startTimeMillis) <= recordTimeMillis ) {
        image = m1.captureScreen();
        if(image !== null) {
            frameCount++;
        }
        try {
            Thread.yield();
        } catch (Exception ex) {
        }
    }
    

    If it turns out that captureScreen can reach the FPS you want there is no need to multi-thread robot instances.

    Rather than having an array list of buffered images I'd have an array list of Futures from the AsynchronousFileChannel.write.

    • Capture loop
      • Get BufferedImage
      • Convert BufferedImage to byte array containing JPEG data
      • Create an async channel to the output file
      • Start a write and add the immediate return value (the future) to your ArrayList
    • Wait loop
      • Go through your ArrayList of Futures and make sure they all finished
    0 讨论(0)
  • 2021-01-04 11:06

    I guess that the intensive memory usage is an issue here. You are capturing in your tests about 250 screenshots. Depending on the screen resolution, this is:

    1280x800 : 250 * 1280*800  * 3/1024/1024 ==  732 MB data
    1920x1080: 250 * 1920*1080 * 3/1024/1024 == 1483 MB data
    

    Try caputuring without keeping all those images in memory.

    As @Obicere said, it is a good idea to keep the Robot instance alive.

    0 讨论(0)
提交回复
热议问题