springboot html转图片

引入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;
}
来做第一个评论吧!~

发送评论 编辑评论


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