Problem: I am using JCEF(java-Chromium Embedded Framework) in a java project, now I want to get the screenshot of web page in CEF browser, but I didn't find API for this. Is there some way to do that? Thanks so much!
-
3Possible duplicate of [How to integrate Chromium Embedded Framework (CEF) with java](http://stackoverflow.com/questions/21192279/how-to-integrate-chromium-embedded-framework-cef-with-java) – George G Aug 22 '16 at 11:08
-
@GeorgeGarchagudashvili But I still can not find any method to do that... – Bode Aug 23 '16 at 01:38
1 Answers
As far as I can tell, CefBrowser is AWT-based. To create a screen shot of such components, you can (have to?) create a capture of the entire screen, limited to the area that is covered by the component.
Something like this will work:
// Your browser instance.
org.cef.browser.CefBrowser browser = ...
// Obtain the component that you want to capture in a screenshot.
java.awt.Component component = browser.getUIComponent();
// Determine what area of the entire screen is covered by the component.
java.awt.Point p = new java.awt.Point(0, 0);
javax.swing.SwingUtilities.convertPointToScreen(p, component);
java.awt.Rectangle region = component.getBounds();
region.x = p.x;
region.y = p.y;
// Store the selected area from the screen in a image buffer.
java.awt.image.BufferedImage image = new java.awt.Robot().createScreenCapture( region );
To save to buffer to a file, create a File
instance (using a JFileChooser
if you want to present a nice save-as dialog to your user) and use javax.imageio.ImageIO#write(RenderedImage, String, File)
to store the image into the file. The second argument refers to the file format (png, bmp, etc.) that you want to use.
If anyone can provide a code sample that stores the component directly, without capturing it as part of the larger screen (which will also capture other components that are on top of the on that you're interested in), I'd be very interested.

- 2,986
- 2
- 21
- 32