Search code examples
javajavacv

javacv - fast screenshot - how to get image


I just found a good example of JavaCV which using FFmpegFrameGrabber to make screenshot code for linux which supposed to be "fast":

try {
    int x = 0, y = 0, w = 1366, h = 768;
    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");

    frame.showImage(grabber.grabImage());

    frame.dispose();
    grabber.stop();
} catch (FrameGrabber.Exception ex) {
    ex.printStackTrace();
}

...anyways I'm still not sure how to get image of the screenshot to save in a file. I couldn't find any related example so I do need your advice...

So my question how to get image of screenshot to make possible IO?


Solution

  • Looking at the API of JavaCV, there is an abstract FrameConverter<F> class. The concrete implementation Java2DFrameConverter looks like what you are looking for.