图片水印文字换行,置于右下角

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;

public class MyTest2 {

    public void waterPress(String srcImgPath, String outImgPath,
                           Color markContentColor, int fontSize, String waterMarkContent) {
        try {
            String[] waterMarkContents = waterMarkContent.split("\\|\\|");
            // 读取原图片信息
            File srcImgFile = new File(srcImgPath);
            Image 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);
            Font font = new Font("宋体", Font.PLAIN, fontSize);
            // 根据图片的背景设置水印颜色
            g.setColor(markContentColor);
            // 设置水印文字字体
            g.setFont(font);
            // 数组长度
            int contentLength = waterMarkContents.length;
            // 获取水印文字中最长的
            int maxLength = 0;
            for (int i = 0; i < contentLength; i++) {
                int fontlen = getWatermarkLength(waterMarkContents[i], g);
                if (maxLength < fontlen) {
                    maxLength = fontlen;
                }
            }

            for (int j = 0; j < contentLength; j++) {
                waterMarkContent = waterMarkContents[j];
                int tempX = 10;
                int tempY = fontSize;
                // 单字符长度
                int tempCharLen = 0;
                // 单行字符总长度临时计算
                int tempLineLen = 0;
                StringBuffer sb = new StringBuffer();
                for (int i = 0; i < waterMarkContent.length(); i++) {
                    char tempChar = waterMarkContent.charAt(i);
                    tempCharLen = getCharLen(tempChar, g);
                    tempLineLen += tempCharLen;
                    if (tempLineLen >= srcImgWidth) {
                        // 长度已经满一行,进行文字叠加
                        g.drawString(sb.toString(), tempX, tempY);
                        // 清空内容,重新追加
                        sb.delete(0, sb.length());
                        tempLineLen = 0;
                    }
                    // 追加字符
                    sb.append(tempChar);
                }
                // 通过设置后两个输入参数给水印定位
                g.drawString(sb.toString(), srcImgWidth - maxLength, srcImgHeight - (contentLength - j - 1) * tempY);
            }
            g.dispose();

            // 输出图片
            FileOutputStream outImgStream = new FileOutputStream(outImgPath);
            ImageIO.write(bufImg, "jpg", outImgStream);
            outImgStream.flush();
            outImgStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public int getCharLen(char c, Graphics2D g) {
        return g.getFontMetrics(g.getFont()).charWidth(c);
    }

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

    public static void main(String[] args) {
        String font = "CSDN||STEPMORE||2020-07-29 10:30:00";
        new MyTest2().waterPress("/Users/yangyun/Documents/personal_doc/pic/test1.png",
                "/Users/yangyun/Documents/personal_doc/pic/yy.png", Color.red, 48, font);
    }
}

示例图片:
图片水印文字换行,置于右下角_第1张图片
水印图片:
图片水印文字换行,置于右下角_第2张图片

你可能感兴趣的:(水印)