Is there a free tool that can read given webpage and take a screenshot of it?
-
Are you going to fire some browser, open the page in it and then capture the website or would you like the website rendered by the java code and than written into image? – quosoo Oct 01 '09 at 13:53
6 Answers
I had best results with Selenium Webdriver using a VirtualFramebuffer and Firefox Binary. This is tested under ubuntu. You need to have xvfb and firefox installed
First install firefox and virtual framebuffer:
aptitude install xvfb firefox
Compile and run this class, open /tmp/screenshot.png afterwards
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
public class CaptureScreenshotTest
{
private static int DISPLAY_NUMBER = 99;
private static String XVFB = "/usr/bin/Xvfb";
private static String XVFB_COMMAND = XVFB + " :" + DISPLAY_NUMBER;
private static String URL = "http://www.google.com/";
private static String RESULT_FILENAME = "/tmp/screenshot.png";
public static void main ( String[] args ) throws IOException
{
Process p = Runtime.getRuntime().exec(XVFB_COMMAND);
FirefoxBinary firefox = new FirefoxBinary();
firefox.setEnvironmentProperty("DISPLAY", ":" + DISPLAY_NUMBER);
WebDriver driver = new FirefoxDriver(firefox, null);
driver.get(URL);
File scrFile = ( (TakesScreenshot) driver ).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(RESULT_FILENAME));
driver.close();
p.destroy();
}
}

- 8,877
- 9
- 71
- 102
-
1
-
-
@janning Is it possible to get url's of current window selected using java...bcoz my webpage has multible tabs and each time I click on each of the tabs corresponding url should be passed as URL field – chopss Apr 28 '15 at 09:06
-
@chopu Don't know what you mean, please ask new question on stackoverflow – Janning Vygen Apr 28 '15 at 13:04
-
You will need this as well: https://github.com/bonigarcia/webdrivermanager – seinecle Dec 10 '17 at 15:16
-
A few years later I use Chrome Remote Headless with https://github.com/GoogleChrome/puppeteer – Janning Vygen Apr 23 '18 at 13:39
To build on two of the answers above:
Rendering the HTML in Java then saving to an image - A few Java based HTML renders exist, all with different sets of drawbacks. The most common is the one built in. This is quite simple and can only render fairly basic HTML. The most intresting I know of is The Flying Saucer Project. This can render fairly complex XHTML but you will have to convert HTML before you can use it (JTindy may be able to help here). Taking a Swing component and creating a image is fairly simple, you just pass an BufferedImage
s graphics object and pass it to the Swing components paint method. Then splat that out with ImageIO.
A big advantage to this would be that the renderer would be headless. The disadvantage is that it would not be a perfect rendering and it would lack any plugins.
The second option requires you to start a web browser, work out where it is and then take a screen shot. Optionally you may also wish to strip out all the Firefox/IE/Opera/etc menus leaving you with just image. To get the dimensions of the web browser the simplest option would be to start it full screen. The other option would be to use something like JDICs browser component to include it as part of the Java application. It would then be able to specify where the HTML is being rendered on screen and then simply use Robot to create a screen shot of that area.
The big advantage to this is that it will give a perfect rendering (for a given browser). The two disadvantages is that it would require native code (or at least using a native component) and it could not be headless¹.
1) You could use a virtual frame buffer. But that is outside Java.

- 14,561
- 3
- 44
- 81
-
1There is a browser in Eclipse. Have you experience with it? – Thorbjørn Ravn Andersen Oct 01 '09 at 14:40
-
Other than as a user I have not (the SWT APIs scare me). But a good point, that is likely better then the default built in one and better HTML support than Flying Saucer. – Michael Lloyd Lee mlk Oct 01 '09 at 14:58
-
JTidy has only recently seen recent contributions. You might also try HtmlUnit (.sourceforge.net). +1 for the JDIC browser reference, it's a really ambitious project and needs more support. – Karl the Pagan Oct 01 '09 at 15:55
-
https://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/text/html/HTMLEditorKit.html is not longer valid – jbuddy Jan 14 '21 at 08:06
It's not Java, but after coming here, it's what I ended up using, so I think it's worth a mention. With PhantomJs you can run a headless version of webkit, and then access the functionality through a built in mongoose webserver, which can handle requests for screencaptures, and store them locally. You can use Java to the make the request, and the response can have the url to the image on the server, so you could grab that too-

- 11,488
- 9
- 84
- 97
-
Unfortunately, the PhantonJS project has stopped development and the project has been archived. – Reg Apr 12 '23 at 09:37
-
1
You can use the createScreenCapture method in awt.Robot. This method allows you to specify which portion of the screen to capture. So, you would still need to determine the coordinates of the window that contains the web page that you want to capture.

- 321
- 1
- 3
To render the HTML in pure java, you might take a look at Flying Saucer.
http://code.google.com/p/flying-saucer//
It renders XML/XHTML/CSS 2.1
I believe it only works on valid XML or XHTML, so if you need to render non-valid HTML, use a tool like neko to clean it up before passing it to flying saucer.

- 10,559
- 3
- 54
- 60