Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get over 30FPS using Java in a Screen Capture Program?

I'm currently using the Robot classes in Java to record the screen. However, it does not achieve the minimum of 30 frames per second. I'm not re-creating objects, and am being as efficient as I can, but I only average around 15 frames per second. Robot is simply not cutting it.

What can I use to capture the screen? I've tried Xuggle, but I can't seem to get that to capture fast enough either.

like image 947
Rainfur Avatar asked Jan 23 '26 11:01

Rainfur


1 Answers

For operating systems following the X11 standard (Linux, FreeBSD, Solaris, etc.), we can do it this way via JavaCV and FFmpeg:

import com.googlecode.javacv.*;

public class ScreenGrabber {
    public static void main(String[] args) throws Exception {
        int x = 0, y = 0, w = 1024, h = 768; // specify the region of screen to grab
        FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(":0.0+" + x + "," + y);
        grabber.setFormat("x11grab");
        grabber.setImageWidth(w);
        grabber.setImageHeight(h);
        grabber.start();

        CanvasFrame frame = new CanvasFrame("Screen Capture");
        while (frame.isVisible()) {
            frame.showImage(grabber.grab());
        }
        frame.dispose();
        grabber.stop();
    }
}

I don't know about Windows or Mac OS X, but I suspect we would need to access native APIs directly. Nevertheless, JavaCPP could help with that.

like image 149
Samuel Audet Avatar answered Jan 25 '26 01:01

Samuel Audet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!