第一步:需要准备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(); }
输出效果图: