0

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?

Mike Laren
  • 8,028
  • 17
  • 51
  • 70
user390525
  • 263
  • 1
  • 2
  • 18
  • *So my question how to get Image of screenshot to make possible IO?* Uh... what? – Turing85 Jul 20 '15 at 19:53
  • @Turing85 The code above grabber.grabImage() see docs https://github.com/bytedeco/javacv/blob/master/src/main/java/org/bytedeco/javacv/FFmpegFrameGrabber.java returns Frame which is neither stream nor image :( So how to get a usable screenshot which can be saved on file for example? – user390525 Jul 21 '15 at 17:17
  • [This class](http://bytedeco.org/javacv/apidocs/org/bytedeco/javacv/Java2DFrameConverter.html) looks like it can convert a `Frame` into a `BufferedImage`. – Turing85 Jul 21 '15 at 17:22
  • @Turing85 Is there a way not to convert (which may be slow) but to use a standard "how-to-make-screenshot-with-javacv" solution? There should be a byte array for example but I didn't find it my bad :( please help me – user390525 Jul 21 '15 at 17:27
  • The class I presented is part of the framework you use. I think that this is the "standard-way". OpenCV does not necessarily operate in the "bytearray"-way. `Frame` does not define a method returning a `byte[]`. – Turing85 Jul 21 '15 at 17:30
  • @Turing85 I tried to convert and it kind of working; I am not sure yet concerning how fast the way is anyways please post your answer me to accept it; – user390525 Jul 22 '15 at 17:13

1 Answers1

1

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

Turing85
  • 18,217
  • 7
  • 33
  • 58
  • I just tried "CanvasFrame frame" and, as I can get it, its scale is by default the minumum one though I set "frame.setSize(100,100)" but still the frame seems to be 10x10 pxl :( I don't get it how to set CanvasFrame width and height correct? – user390525 Jul 22 '15 at 20:21
  • Look at the API. I cannot do more than this myself. – Turing85 Jul 22 '15 at 20:23
  • I understand but the CanvasFrame maybe has some another way to set width and height? – user390525 Jul 22 '15 at 20:27
  • Maybe. If it has, you will find it in the API. – Turing85 Jul 22 '15 at 20:28
  • Looking at the API, a `CanvasFrame` has nothing to do with a `Frame`. A `CanvasFrame` is more like ja `JFrame`. [This](http://stackoverflow.com/questions/5853879/swing-obtain-image-of-jframe) might help. – Turing85 Jul 22 '15 at 20:42
  • I tried to paint the grabbed and converted screenshot on JLabel in a usual way as "super.getGraphics().drawImage(img, 0, 0, 500,300,null);" but still I have a strange 10x10 red square in the left upper corner of the frame; I set frame background color as red so it is certainly the frame arftifact or similar which doesn't allow to make screenshot preview with the FrameCanvas a good looking one :'( – user390525 Jul 22 '15 at 20:49
  • Have you tried querying the image directly about its width and height? – Turing85 Jul 22 '15 at 20:52
  • I guess image by itself should be fine but to preview screenshots in CanvasFrame causes artifacts issue; A 10x10, as I can get it JFrame's content pane, square is painted over JLabel which contains image :*( Some layers issue or I don't get it... – user390525 Jul 22 '15 at 21:30