views:

237

answers:

2

I am building a web application, in Java, where i want the whole screenshot of the webpage, if i give the URL of the webpage as input.

The basic idea i have is to capture the display buffer of the rendering component..I have no idea of how to do it.. plz help..

+3  A: 
OscarRyz
Note that this doesn't work on [headless](http://java.sun.com/javase/6/docs/api/java/awt/HeadlessException.html) servers.
BalusC
@Balusc Yeap, actually I ran it as an standalone application.
OscarRyz
+2  A: 

For a pure-java solution that can scale to support concurrent rendering, you could use a java HTML4/CSS2 browser, such as Cobra, that provides a Swing component for the GUI. When you instantiate this component, you can call it's paint(Graphics g) method to draw itself into an off-screen image

E.g.
Component c = ...; // the browser component
BufferedImage bi = new BufferedImage(c.getWidth(), c.getHeight(), TYPE_INT_RGB)
Graphics2d g = bi.createGraphics();    
c.paint(g);

You can then use the java image API to save this as a JPG.

JPEGImageEncoder encoder = JPEGCodec.createEncoder(new FileOutputStream("screen.jpg"));
enncoder.encode(bi);  // encode the buffered image

Java-based browsers typically pale in comparison with the established native browsers. However, as your goal is static images, and not an interactive browser, a java-based browser may be more than adequate in this regard.

mdma