java中使用ImageMagick工具在图片中进行文字水印输出

第一步:需要准备ImageMagick水印工具
第二步:配置水印工具位置、字体等基础设置
public static void main(String[] args) throws InterruptedException, IOException, IM4JavaException {
String imageMagickPath = "D:\\ImageMagick-7.0.8-Q16";  //软件路径
String fontPath = "C:\\Windows\\Fonts\\simhei.ttf";   //字体路径
int coordinateX = 300;// 在图片的坐标X轴300处开始水印
int coordinateY = 400;// 在图片的坐标Y轴400处开始水印
String filePath = "D:\\360Downloads\\水印图片.png";//目标图片路径
IMOperation op = new IMOperation();
int totalheight;
int textsize = 20;//字体大小
int colsize = 30;//每行存放多少字29
String content = "输出内容kkk内容1;输出内容内容2;输出内容内容3";//要输出的内容
// 意见处理
if (content.contains(";")) {
    List<String> ss = Arrays.asList(content.split(";"));
    totalheight = coordinateY;
    for (String value : ss) {
        content = value;
        if (content.contains("kkk")) {
            StringBuilder data;
            content = content.replaceAll("kkk", ";");
            List<String> commonSS = Arrays.asList(content.split(";"));
            for (int j = 0; j < commonSS.size(); j++) {
                data = new StringBuilder(commonSS.get(j));
                if (j == (commonSS.size() - 1)) {
                    data = new StringBuilder();
                    for (int k = 0; k < 40; k++) {//给内容前面增加40个空格在水印到图片上面
                        data.append(" ");
                    }
                    data.append(commonSS.get(j));
                }
                totalheight = addImageTextWithBr(op, data.toString(), totalheight, coordinateX, colsize, textsize, fontPath);
            }
        } else {
            totalheight = addImageTextWithBr(op, content, totalheight, coordinateX, colsize, textsize, fontPath);
        }
    }
} else {
    addImageTextWithBr(op, content, coordinateY, coordinateX, colsize, textsize, fontPath);
}
op.addImage(filePath);
op.addImage(filePath);
ConvertCmd convert = new ConvertCmd();
convert.setSearchPath(imageMagickPath);
convert.run(op);

}

/**
 * 文字水印功能
 * subject  插入的内容
 * height   当前要插入的高度
 * width    当前要插入的宽度
 * colsize  一行存放文字的多少
 * textsize 字体大小
 */
public static int addImageTextWithBr(IMOperation op, String subject, int height, int width, int colsize, int textsize, String fontPath) {

    if (!StringUtils.hasText(subject)) {
        return height;
    }
    //中文和非中文字符所占的宽度不一样,中文字符大约是非中文字符的2倍
    //所以换行规则是:达到colsize*2个非中文字符触发换行(中文字符当2个算)
    char[] test = subject.toCharArray(); //把字符串转成字符数组
    int num = 0; //非中文字符数量
    int i = 0; //当前行开始位置所在字符串的index
    int j = 1; //当前行结束位置所在字符串的index
    for (char c : test) {
        if (checkCharCN(c)) {//判断是否是中文或者相关中文字符
            num = num + 2;
        } else {
            num = num + 1;
        }
        if (num >= colsize * 2) {
            String str = subject.substring(i, j);
            op.font(fontPath).gravity("northwest").pointsize(textsize).fill("black").draw("text " + width + "," + height + " '" + str + "'");
            height = height + textsize;
            i = j;
            num = 0;
        }
        j++;
    }

    if (num > 0) {
        String str = subject.substring(i);
        String[] split = subject.split("\n");
        if (split.length > 0) {
            height = height + (textsize * (split.length - 1));
        }
        op.font(fontPath).gravity("northwest").pointsize(textsize).fill("black").draw("text " + width + "," + height + " '" + str + "'");
        height = height + textsize;
    }

    return height;
}

//判断是否为中文字符,以及包括部分中文标点都需要进行+2
private static boolean checkCharCN(char c) {
    String s = String.valueOf(c);
    String regex = "^[\u4E00-\u9FA5|\\、|\\!|\\,|\\。|\\(|\\)|\\《|\\》|\\“|\\”|\\?|\\:|\\;|\\【|\\】]$";
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(s);
    return m.matches();
}

输出效果图:

  • 38
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Q_L_D_X_K

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

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

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

打赏作者

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

抵扣说明:

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

余额充值