//java Html2Image 實現html轉圖片功能
// html2image
HtmlImageGenerator imageGenerator = new HtmlImageGenerator();
String htmlstr = "<table width='654' cellpadding='0' cellspacing='0' bordercolor='#FFFFFF'><tr><td><img src='http://www.apkfather.com/yhqserver/images/mdl.jpg'/></td><td><img src='http://www.apkfather.com/yhqserver/images/mdl.jpg'/></td><td><img src='http://www.apkfather.com/yhqserver/images/mdl.jpg'/></td></tr><tr><td><img src='http://www.apkfather.com/yhqserver/images/mdl.jpg'/></td><td><img src='http://www.apkfather.com/yhqserver/images/mdl.jpg'/></td><td><img src='http://www.apkfather.com/yhqserver/images/mdl.jpg'/></td></tr><tr><td><img src='http://www.apkfather.com/yhqserver/images/mdl.jpg'/></td><td><img src='http://www.apkfather.com/yhqserver/images/mdl.jpg'/></td><td><img src='http://www.apkfather.com/yhqserver/images/mdl.jpg'/></td></tr><tr><td><img src='http://www.apkfather.com/yhqserver/images/mdl.jpg'/></td></tr></table>";
imageGenerator.loadHtml(htmlstr);
imageGenerator.getBufferedImage();
imageGenerator.saveAsImage("d:/hello-world.png");
imageGenerator.saveAsHtmlWithMap("hello-world.html", "hello-world.png");
// html2image api介紹
HtmlImageGenerator Methods
- loadUrl(url) - Loads HTML from URL object or URL string. (從url載入html)
- loadHtml(html) - Loads HTML source. (載入本地html)
- saveAsImage(file) - Save loaded HTML as image. (以圖片形式儲存html)
- saveAsHtmlWithMap(file, imageUrl) - Creates an HTML file containing client-side image-map <map>generated from HTML's links. (建立一個HTML檔案包含客戶端image-map)
- getLinks() - List all links in the HTML document and their corresponding href, target, title, position and dimension. (列出所有在HTML文件的連結和相應href、目標、頭銜、位置和尺寸)
- getBufferedImage() - Get AWT buffered image of the HTML. (獲得awt,html緩衝後的圖片)
- getLinksMapMarkup(mapName) - Get HTML snippet of the client-side image-map <map> generated from the links. (HTML程式碼段裡獲得的客戶端image-map <地圖>產生的連結)
- get/setOrientation(orientation) - Get/Set document orientation (left-to-right or right-to-left). (get/set文字定位)
- get/setSize(dimension) - Get/Set size of the generated image. (設定生成圖片大小)