- Cobra 是一个纯 Java 的HTML 解析和生成器,支持 HTML 4 、JavaScript、CSS 2
- 这个方法的依赖包,暂时我没找到相应的版本,所以我就没测试
- 案例:
public class Cobra {
public static void main(String[] args) throws Exception {
JFrame window = new JFrame();
HtmlPanel panel = new HtmlPanel();
window.getContentPane().add(panel);
window.setSize(600, 400);
window.setVisible(true);
new SimpleHtmlRendererContext(panel, new SimpleUserAgentContext())
.navigate("http://www.hefeipet.com/client/chongwuzhishi/shenghuozatan/2012/0220/95.html");
BufferedImage image = new BufferedImage(panel.getWidth(),
panel.getHeight(), BufferedImage.TYPE_INT_ARGB);
// paint the editor onto the image
SwingUtilities.paintComponent(image.createGraphics(), panel,
new JPanel(), 0, 0, image.getWidth(), image.getHeight());
// save the image to file
ImageIO.write((RenderedImage) image, "png", new File("html.png"));
}
}