import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
/**
-
@ProjectName: test
-
@Package: com.test.draw
-
@ClassName: AddWatermarkUtil
-
@Author: ***
-
@Description:
-
@Date: 2020/11/2 11:24
-
@Version: 1.0
/
public class AddWatermarkUtil {
/*- 水印之间的横向间隔
*/
private static final int XMOVE = 80;
/**
- 水印之间的纵向间隔
*/
private static final int YMOVE = 80;
/**
-
图片添加水印
-
@param outImgPath 添加水印后图片输出路径
-
@param markContentColor 水印文字的颜色
-
@param fontSize 文字大小
-
@param waterMarkContent 水印的文字,多排水印请使用"||"分割
-
@param srcImgPath 需要添加水印的图片的路径
*/
public static 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(“Courier New”, Font.PLAIN, 12);
// 字体
Font font = new Font(“宋体”, Font.PLAIN, fontSize);
// 根据图片的背景设置水印颜色
g.setColor(markContentColor);
// 设置水印文字字体
g.setFont(font);
// 设置水印旋转
g.rotate(Math.toRadians(-45), (double) bufImg.getWidth() / 2, (double) bufImg.getHeight() / 2);// 获取其中最长的文字水印的大小 int maxLen = 0; int maxHigh = 0; for (int i = 0; i < waterMarkContents.length; i++) { waterMarkContent = waterMarkContents[i]; int fontlen = getWatermarkLength(waterMarkContent, g); if (fontlen >= maxLen) { maxLen = fontlen; } maxHigh = maxHigh + (i + 1) * fontSize + 10; } // 文字长度相对于图片宽度应该有多少行 int line = srcImgWidth * 2 / maxLen; int co = srcImgHeight * 2 / maxHigh; int yz = 0; // 填充Y轴方向 for (int a = 0; a < co; a++) { int t = 0; for (int j = 0; j < waterMarkContents.length; j++) { waterMarkContent = waterMarkContents[j]; int y = (j + 1) * fontSize + 10 + t; // 文字叠加,自动换行叠加,注意符号 int tempX = -srcImgWidth / 2; int tempY = -srcImgHeight / 2 + y + yz; // 单字符长度 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); t = t + fontSize; // 清空内容,重新追加 sb.delete(0, sb.length()); tempY += fontSize; tempLineLen = 0; } // 追加字符 sb.append(tempChar); } // 填充X轴 for (int z = 0; z < line; z++) { // 最后叠加余下的文字 g.drawString(sb.toString(), tempX, tempY); tempX = tempX + maxLen + XMOVE; } } yz = yz + maxHigh + YMOVE; } g.dispose(); // 输出图片 FileOutputStream outImgStream = new FileOutputStream(outImgPath); ImageIO.write(bufImg, "jpg", outImgStream); outImgStream.flush(); outImgStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static int getCharLen(char c, Graphics2D g) {
return g.getFontMetrics(g.getFont()).charWidth©;
}/**
- 获取水印文字总长度
- @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 main(String[] args) {
// 原图位置, 输出图片位置, 水印文字颜色,水印文字大小 水印文字
String font = “嘻嘻”;
String inputAddress =“F:\UpupooWallpaper\zta.jpg”;
String outputAddress=“F:\UpupooWallpaper\ztass.jpg”;
Color color =Color.red;
waterPress(inputAddress, outputAddress, color,30, font);
}
} - 水印之间的横向间隔