views:

3322

answers:

4

I am searching for an open source Java library to generate thumbnails for a given URL. I need to bundle this capability, rather than call out to external services, such as Amazon or websnapr.

http://www.webrenderer.com/ was mentioned in this post: Server generated web screenshots, but it is a commercial solution.

I'm hoping for a Java based solution, but may need to look into executing an external process such as khtml2png, or integrating something like html2ps.

Any suggestions?

A: 

You're essentially asking for a complete rendering engine accessible by Java. Personally, I would save myself the hassle and call out to a child process.

Otherwise, I ran into this pure Java browser: Lobo

Frank Krueger
A: 

wasn't there a QA/test website/service which would let you specify a web page that you wanted to be rendered in a certain browser(IE, FIREFOX, SAFARI version x,y,z) and they would mail the snapshot back to you. '

I can't remember the service -- maybe other developers who frequent ajaxian might remember it ?

anjanb
+1  A: 

The first thing that comes to mind is using AWT to capture a screen grab (see code below). You could look at capturing the JEditorPane, the JDIC WebBrowser control or the SWT Browser (via the AWT embedding support). The latter two embed native browsers (IE, Firefox), so introduce dependencies; the JEditorPane HTML support stopped at HTML 3.2. It may be that none of these will work on a headless system.

import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JLabel;

public class Capture {

    private static final int WIDTH = 128;
    private static final int HEIGHT = 128;

    private BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
      BufferedImage.TYPE_INT_RGB);

    public void capture(Component component) {
     component.setSize(image.getWidth(), image.getHeight());

     Graphics2D g = image.createGraphics();
     try {
      component.paint(g);
     } finally {
      g.dispose();
     }
    }

    private BufferedImage getScaledImage(int width, int height) {
     BufferedImage buffer = new BufferedImage(width, height,
       BufferedImage.TYPE_INT_RGB);
     Graphics2D g = buffer.createGraphics();
     try {
      g.drawImage(image, 0, 0, width, height, null);
     } finally {
      g.dispose();
     }
     return buffer;
    }

    public void save(File png, int width, int height) throws IOException {
     ImageIO.write(getScaledImage(width, height), "png", png);
    }

    public static void main(String[] args) throws IOException {
     JLabel label = new JLabel();
     label.setText("Hello, World!");
     label.setOpaque(true);

     Capture cap = new Capture();
     cap.capture(label);
     cap.save(new File("foo.png"), 64, 64);
    }

}
McDowell
A: 

Try calling ImageMagick. I know it's not a Java solution, but you can call it from Java, and there's even a Java front-end, although I've had less success with that.

Sietse