(水印)html转图片

<dependency>
    <groupId>gui.ava</groupId>
    <artifactId>html2image</artifactId>
    <version>2.0.1</version>
</dependency>
<dependency>
    <groupId>xml-apis</groupId>
    <artifactId>xml-apis</artifactId>
    <version>1.4.01</version>
</dependency

html


String htmlTemplate = "<div>" +
        "<span>IP地址:127.0.0.1</span><span style=\"margin-left: 60px\">国家:AAA</span><br>\n" +
        "<span>省份:AAAA</span><span style=\"margin-left: 88px\">位置:XXXXXXX</span></div>";
HtmlParser htmlParser = new HtmlParserImpl();
htmlParser.loadHtml(htmlTemplate);
ImageRenderer imageRenderer = new ImageRendererImpl(htmlParser);
imageRenderer.saveImage("D:\\hello-world.png");

图片添加水印


/**
 * 复制图片文件,并添加水印
 * @param srcImgPath  原图片路径
 * @param outImgPath  生成的新图片路径
 * @param waterMarkContent  水印内容
 * @param markContentColor  水印颜色
 * @param font  水印字体
 */
public static void waterPress(String srcImgPath, String outImgPath, String waterMarkContent, Color markContentColor, Font font) {
    try {
        // 读取原图片信息
        File srcImgFile = new File(srcImgPath);
        Image srcImg = null;
        if (srcImgFile.exists() && srcImgFile.isFile() && srcImgFile.canRead()) {
            srcImg = ImageIO.read(srcImgFile);
        }
        // 宽、高
        int srcImgWidth = srcImg.getWidth(null);
        int srcImgHeight = srcImg.getHeight(null);
        // 加水印
        BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bufImg.createGraphics();
        g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);

        //设置水印颜色
        g.setColor(markContentColor);
        g.setFont(font);
        // 抗锯齿
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        int fontLength = getWatermarkLength(waterMarkContent, g);
        // 实际生成的水印文字,实际文字行数
        Double textLineCount = Math.ceil(Integer.valueOf(fontLength).doubleValue() / Integer.valueOf(srcImgWidth).doubleValue());
        int fontSize = font.getSize();
        // 实际所有的水印文字的高度
        int textHeight = textLineCount.intValue() * fontSize;
        // 相对与X的起始的位置
        int originX = 0;
        // 相对与Y的起始的位置
        int originY = 0;
        // 实际文字大于1行,则x则为默认起始0,
        if (1 == textLineCount.intValue()) {
            // 实际文字行数是1,1/2个图片高度,减去1/2个字符高度
            originY = srcImgHeight / 2 - fontSize / 2;
            // 实际文字行数是1,计算x的居中的起始位置
            originX = (srcImgWidth - fontLength) / 2;
        } else {
            // 实际文字行数大于1,1/2个图片高度减去文字行数所需的高度
            originY = (srcImgHeight - textHeight) / 10;
        }
        System.out.println("水印文字总长度:" + fontLength + ",图片宽度:" + srcImgWidth + ",字符个数:" + waterMarkContent.length());
        //文字叠加,自动换行叠加
        int tempX = originX;
        int tempY = originY;
        int tempCharLen = 0;//单字符长度
        int tempLineLen = 0;//单行字符总长度临时计算
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < waterMarkContent.length(); i++) {
            char tempChar = waterMarkContent.charAt(i);
            tempCharLen = getCharLen(tempChar, g);
            if (tempLineLen >= srcImgWidth) {
                // 绘制前一行
                g.drawString(stringBuffer.toString(), tempX, tempY);
                //清空内容,重新追加
                stringBuffer.delete(0, stringBuffer.length());
                //文字长度已经满一行,Y的位置加1字符高度
                tempY = tempY + fontSize;
                tempLineLen = 0;
            }
            //追加字符
            stringBuffer.append(tempChar);
            tempLineLen += tempCharLen;
        }
        //最后叠加余下的文字
        g.drawString(stringBuffer.toString(), tempX, tempY);
        g.dispose();
        // 输出图片
        FileOutputStream outImgStream = new FileOutputStream(outImgPath);
        ImageIO.write(bufImg, "png", outImgStream);
        outImgStream.flush();
        outImgStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static int getCharLen(char c, Graphics2D g) {
    return g.getFontMetrics(g.getFont()).charWidth(c);
}
/**
 * 获取水印文字总长度
 *
 * @paramwaterMarkContent水印的文字
 * @paramg
 * @return水印文字总长度
 */
public static int getWatermarkLength(String waterMarkContent, Graphics2D g) {
    return g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length());
}

//测试复制图片并添加水印
public static void testWaterPressFile(){
    // 原图位置, 输出图片位置, 水印字体,水印文字样式,水印文字颜色, 水印文字大小,水印文字内容
    Font font = new Font("微软雅黑",Font.BOLD+ Font.ITALIC, 18);   //水印字体
    Color color = Color.cyan;
    String content = "图片来源:Gaoxs";
    waterPress("D:\\hello-world.png",
            "D:\\hello-world22.png",
            content, color, font);
}
public static void main(String[] args) {
   testWaterPressFile();
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Peak_Gao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值