html转图片(html2image)

需要jar包

https://repo.spring.io/plugins-release/gui/ava/html2image/0.9/html2image-0.9.jar

实现

package cn;

//import com.yanhui.util._html2jpg;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;

import javax.imageio.ImageIO;

import gui.ava.html.image.generator.HtmlImageGenerator;

public class Test {
	public static void main(String[] args) {
		 HtmlImageGenerator imageGenerator = new HtmlImageGenerator();  
		 String str="";
		 File file=new File("D://222.html");//本地文件
		 try {
			 FileInputStream in=new FileInputStream(file);
			 // size 为字串的长度 ,这里一次性读完
			 int size=in.available();
			 byte[] buffer=new byte[size];
			 in.read(buffer);
			 in.close();
			 str=new String(buffer,"GBK");
			} catch (IOException e) {
			 e.printStackTrace();
			}
        imageGenerator.loadHtml(str);
        imageGenerator.getBufferedImage();  
        try {
			Thread.sleep(800);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        imageGenerator.saveAsImage("d:/hell.png");  
//	        imageGenerator.saveAsHtmlWithMap(”hello-world.html”, “hello-world.png”);  
        System.out.println("test");
        
	    }
	}
	

 

 

// 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. (设置生成图片大小)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值