Increasing screen capture speed when using Java and awt.Robot

后端 未结 2 1350
生来不讨喜
生来不讨喜 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

提交回复
热议问题