前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >html转图片

html转图片

作者头像
阿超
发布2022-08-16 18:36:44
13.8K0
发布2022-08-16 18:36:44
举报
文章被收录于专栏:快乐阿超

真话说一半常是弥天大谎。——富兰克林

引入依赖

代码语言:javascript
复制
<!-- html转图片 -->
<dependency>
    <groupId>com.github.xuwei-k</groupId>
    <artifactId>html2image</artifactId>
    <version>0.1.0</version>
</dependency>

我这里稍微封装了一下

代码语言:javascript
复制
package com.ruben.utils;

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

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;

/**
 * @ClassName: HtmlUtils
 * @Description: 我还没有写描述
 * @Date: 2020/12/29 0029 20:21
 * *
 * @author: <achao1441470436@gmail.com>
 * @version: 1.0
 * @since: JDK 1.8
 */
public class HtmlUtils {
    /**
     * @MethodName: htmlSaveAsImage
     * @Description: html字符串转图片,保存到本地<多用于富文本场景>
     * @Date: 2020/12/29 0029 20:25
     * *
     * @author: <achao1441470436@gmail.com>
     * @param: [html, targetPath]
     * @returnValue: void
     */
    public static void htmlSaveAsImage(String html, String targetPath) {
        // 读取html
        HtmlImageGenerator imageGenerator = new HtmlImageGenerator();
        // 加载html模版
        imageGenerator.loadHtml(html);
        // 写入本地
        imageGenerator.saveAsImage(targetPath);
    }

    /**
     * @MethodName: htmlToImageByte
     * @Description: html 转换成byte数组
     * @Date: 2020/12/29 0029 21:16
     * *
     * @author: <achao1441470436@gmail.com>
     * @param: [html]
     * @returnValue: byte[]
     */
    public static byte[] htmlToImageByte(String html) {
        // 读取html
        HtmlImageGenerator imageGenerator = new HtmlImageGenerator();
        // 加载html模版
        imageGenerator.loadHtml(html);
        //获取图片
        BufferedImage image = imageGenerator.getBufferedImage();
        byte[] bytes = null;
        try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
            // 写如ByteArrayOutputStream
            ImageIO.write(image, "png", os);
            // 转成数组
            bytes = os.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bytes;
    }


    public static void main(String[] args) {
        htmlSaveAsImage("<h1 color='red'>ruben</h1>", "D:\\file\\files\\target\\ruben.png");
        byte[] bytes = htmlToImageByte("<h1 color='red'>ruben</h1>");
    }
}

执行后结果

image-20201229212123768
image-20201229212123768
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-12-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档