Java 实现HTML 页面转成image 图片
2013-03-20 16:05
423 查看
前言
在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(); } } }
java-html2image
下载地址:http://code.google.com/p/java-html2image/
开源, 使用上很简洁。API Doc 和source code 都有。
import gui.ava.html.image.generator.HtmlImageGenerator; 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
收费的。http://www.webrenderer.com/
下载包里有包含很多例子, 效果类似在swing 中使用browser的功能
总结
不管是哪一种,多于样式复杂的页面,产生的效果都不尽如人意。。。
相关文章推荐
- Java 实现HTML 页面转成image 图片
- java库Html2Image:实现html转图片功能
- java库Html2Image:实现html转图片功能
- java库Html2Image:实现html转图片功能
- java Html2Image 实现html转图片功能
- 用java代码根据html页面生成图片
- 在页面中,我们经常看到,一个button按钮,如果属标点击,就会触发一个窗口的显示,如果二次点击并可以隐藏,那么如何通过JAVA配合html来实现这一功能呢?
- (Java实现)HTML转JPG,TIFF等图片格式和TIFF图片合并功能解决方案。
- 实现HTML页面保存为图片
- java通过commons-fileupload实现多张图片的上传(jsp页面)
- java web应用中自动生成文章html页面的实现
- java 调用jacob和java2word导出word,并实现将带有格式的html文本和图片插入word
- Python语言实现将图片转化为html页面
- java实现html转图片功能或直接后台截图
- java实现当前页面保存为图片
- Java使用wkhtmltox实现HTML代码生成PDF文档或者图片
- Java实现缓存页面中不变的元素,JS,CSS,图片等
- Java Image Filters实现图片特效处理
- html页面图片和文字实现自适应
- JavaScript+Java实现HTML页面转为PDF文件保存的方法