html转图片

第一种

直接生成base64为编码,代码简洁

引入pom

<dependency>
	<groupId>gui.ava</groupId>
	<artifactId>html2image</artifactId>
	<version>0.9</version>
</dependency>

完整示例

public static void main(String[] args) {
	HtmlImageGenerator imageGenerator = new HtmlImageGenerator();

	String htmlstr = "<span>test</span>";

	/*html代码元素会有限制,项input标签元素不能用,img没测试,应该可以*/
	imageGenerator.loadHtml(htmlstr);
	BufferedImage bufferedImage = getGrayPicture(imageGenerator.getBufferedImage());
	ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
	try {
		ImageIO.write(bufferedImage, "jpg", outputStream);
		String base64Img = Base64.encodeBase64String(outputStream.toByteArray());
		String res = "data:image/jpg;base64," + base64Img.toString();
		System.out.println(res);
	} catch (IOException e) {
		e.printStackTrace();
	}finally {
		if(outputStream != null){
			try {
				outputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}


public static BufferedImage getGrayPicture(BufferedImage originalImage)
{
	BufferedImage grayPicture;
	int imageWidth = originalImage.getWidth();
	int imageHeight = originalImage.getHeight();

	grayPicture = new BufferedImage(imageWidth, imageHeight,
			BufferedImage.TYPE_INT_RGB);
	ColorConvertOp cco = new ColorConvertOp(ColorSpace
			.getInstance(ColorSpace.CS_GRAY), null);
	cco.filter(originalImage, grayPicture);
	return grayPicture;
}

第二种

第二种可以读取本地html文件,并且把jpg写到指定位置

引入

<dependency>
	<groupId>com.github.xuwei-k</groupId>
	<artifactId>html2image</artifactId>
	<version>0.1.0</version>
</dependency>
public static void main(String[] args) {
	System.out.println(html2Img(getHtmlContent("C:/aaa.html","utf-8"),"C:/123.jpg"));
}


/**
 *
 * @Description 读取HTML文件,获取字符内容
 * @param filePath
 * @param charset
 * @return
 */
public static String getHtmlContent(String filePath, String charset){

	String line = null;
	StringBuilder sb = new StringBuilder();
	BufferedReader reader = null;

	try {
		reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath)),charset));
		while ((line = reader.readLine()) != null) {
			sb.append(line + "\n");
		}
	} catch (IOException e) {
		e.printStackTrace();
		throw new RuntimeException("读取HTML文件,获取字符内容异常");
	} finally {
		try {
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("关闭流异常");
		}
	}
	return sb.toString();
}


/**
 *
 * @Description HTML转Image
 * @param htmText HTML文本字符串
 * @return 希望生成的Image Location
 */
public static String html2Img(String htmText, String saveImageLocation){

	HtmlImageGenerator imageGenerator = new HtmlImageGenerator();
	try {
		imageGenerator.loadHtml(htmText);
		Thread.sleep(100);
		imageGenerator.getBufferedImage();
		Thread.sleep(200);
		imageGenerator.saveAsImage(saveImageLocation);
		//imageGenerator.saveAsHtmlWithMap("hello-world.html", saveImageLocation);
		//不需要转换位图的,下面三行可以不要
		BufferedImage sourceImg = ImageIO.read(new File(saveImageLocation));
		sourceImg = transform_Gray24BitMap(sourceImg);
		ImageIO.write(sourceImg, "BMP", new File(saveImageLocation));
	} catch (Exception e) {
		e.printStackTrace();
		throw new RuntimeException("将HTML文件转换成图片异常");
	}
	return saveImageLocation;
}

/**
 *
 * @Description 转换成24位图的BMP
 * @param image
 * @return
 */
public static BufferedImage transform_Gray24BitMap(BufferedImage image){
	int h = image.getHeight();
	int w = image.getWidth();
	int[] pixels = new int[w * h]; // 定义数组,用来存储图片的像素
	int gray;
	PixelGrabber pg = new PixelGrabber(image, 0, 0, w, h, pixels, 0, w);
	try {
		pg.grabPixels(); // 读取像素值
	} catch (InterruptedException e) {
		throw new RuntimeException("转换成24位图的BMP时,处理像素值异常");
	}

	for (int j = 0; j < h; j++){ // 扫描列
		for (int i = 0; i < w; i++) { // 扫描行
			// 由红,绿,蓝值得到灰度值
			gray = (int) (((pixels[w * j + i] >> 16) & 0xff) * 0.8);
			gray += (int) (((pixels[w * j + i] >> 8) & 0xff) * 0.1);
			gray += (int) (((pixels[w * j + i]) & 0xff) * 0.1);
			pixels[w * j + i] = (255 << 24) | (gray << 16) | (gray << 8) | gray;
		}
	}

	MemoryImageSource s= new MemoryImageSource(w,h,pixels,0,w);
	Image img =Toolkit.getDefaultToolkit().createImage(s);
	BufferedImage buf = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);//如果要转换成别的位图,改这个常量即可
	buf.createGraphics().drawImage(img, 0, 0, null);
	return buf;
}
来做第一个评论吧!~

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇