Java 实现HTML 页面转成image 图片

本文介绍几种使用Java将HTML页面转换为图片的方法,包括直接利用Java Core API、使用java-html2image库及Cobra等开源工具。这些方法通过在Swing组件上加载HTML内容并将其渲染为图像。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

在java 中把HTML转化成图档,思路基本上是现在 AWT or Swing 的Panel上显示网页,在把Panel输出为 image 文件。

java 本身的API有提供相关的结果,但是直接产生的效果不是很好,所以有出现一些 library.

Java Core API

public class HtmlToImage {

	protected static void generateOutput() throws Exception {
		
		//load the webpage into the editor
		//JEditorPane ed = new JEditorPane(new URL("http://www.google.com"));
		JEditorPane ed = new JEditorPane(new URL("http://www.hefeipet.com/client/chongwuzhishi/shenghuozatan/2012/0220/95.html"));
		ed.setSize(200,200);

		//create a new image
		BufferedImage image = new BufferedImage(ed.getWidth(), ed.getHeight(),
		                                        BufferedImage.TYPE_INT_ARGB);

		//paint the editor onto the image
		SwingUtilities.paintComponent(image.createGraphics(), 
		                              ed, 
		                              new JPanel(), 
		                              0, 0, image.getWidth(), image.getHeight());
		//save the image to file
		ImageIO.write((RenderedImage)image, "png", new File("html.png"));
	}
	public static void main(String[] args) {
		try {
			generateOutput();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

 

开源, 使用上很简洁。API Doc 和source code 都有。



public class Html2ImageTest {
	
	public static void main(String[] args) {
		HtmlImageGenerator imageGenerator = new HtmlImageGenerator();
//		imageGenerator
//				.loadHtml("<b>Hello World!</b> Please goto <a title=\"Goto Google\" href=\"http://www.google.com\">Google</a>.");
		//imageGenerator.loadUrl("http://mtkplmap2:7001/esdm_web"); 
		imageGenerator.loadUrl("http://www.hefeipet.com/client/chongwuzhishi/shenghuozatan/2012/0220/95.html"); 
		imageGenerator.saveAsImage("hello-world.png");
		imageGenerator.saveAsHtmlWithMap("hello-world.html", "hello-world.png");
	}

}

Cobra

免费,开源

http://lobobrowser.org/cobra.jsp

public class CobraTest {
	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"));
	}
}

 

WebRenderer

收费的。

WebRenderer - Standards compliant Java browser

下载包里有包含很多例子, 效果类似在swing 中使用browser的功能

总结

不管是哪一种,多于样式复杂的页面,

产生的效果都不尽如人意。。。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

oscar999

送以玫瑰,手留余香

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值