完整代码
package com.yuanjia.util;
import java.awt.Color;
import java.awt.Font;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Objects;
public class ImgWaterUtils {
private final static String IMG_TYPE = "jpg";
public static void markImageByTextOne(WaterConfig waterConfig, byte[] bytes, String srcImgPath, String targetPath) {
OutputStream os = null;
try {
Image srcImg = getImage(bytes, srcImgPath);
int width = srcImg.getWidth(null);
int height = srcImg.getHeight(null);
BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = assembleGraphics2D(waterConfig, srcImg, width, height, buffImg);
int size = waterConfig.getFont().getSize();
int waterWidth = size * getTextLength(waterConfig.getLogText());
int x = (width - waterWidth) / 2;
int y = (height - size) / 2;
g.drawString(waterConfig.getLogText(), x, y);
g.dispose();
os = new FileOutputStream(targetPath);
ImageIO.write(buffImg, IMG_TYPE, os);
System.out.println("图片完成添加单个水印文字");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != os)
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void markImageByTextMore(WaterConfig waterConfig, byte[] bytes, String srcImgPath, String targetPath) {
OutputStream os = null;
try {
Image srcImg;
srcImg = getImage(bytes, srcImgPath);
int width = srcImg.getWidth(null);
int height = srcImg.getHeight(null);
BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = assembleGraphics2D(waterConfig, srcImg, width, height, buffImg);
int x = -width * 3;
int y;
String logText = waterConfig.getLogText();
int size = waterConfig.getFont().getSize();
int waterWidth = size * getTextLength(logText);
int positionWidth = waterConfig.getPositionWidth();
int positionHeight = waterConfig.getPositionHeight();
while (x < width * 3) {
y = -height * 3;
while (y < height * 3) {
g.drawString(logText, x, y);
y += size + positionWidth;
}
x += waterWidth + positionHeight;
}
g.dispose();
os = new FileOutputStream(targetPath);
ImageIO.write(buffImg, IMG_TYPE, os);
System.out.println("图片完成添加多个水印文字");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != os) {
os.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
private static Image getImage(byte[] bytes, String srcImgPath) throws IOException {
Image srcImg;
if (Objects.nonNull(bytes)) {
srcImg = ImageIO.read(new ByteArrayInputStream(bytes));
} else {
srcImg = ImageIO.read(new File(srcImgPath));
}
return srcImg;
}
private static Graphics2D assembleGraphics2D(WaterConfig waterConfig, Image srcImg, int width, int height, BufferedImage buffImg) {
Graphics2D g = buffImg.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(srcImg, 0, 0, width, height, null);
if (null != waterConfig.getDegree()) {
g.rotate(Math.toRadians(waterConfig.getDegree()), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2);
}
g.setColor(waterConfig.getColor());
g.setFont(waterConfig.getFont());
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, waterConfig.getAlpha()));
return g;
}
public static int getTextLength(String text) {
int length = text.length();
for (int i = 0; i < text.length(); i++) {
String s = String.valueOf(text.charAt(i));
if (s.getBytes().length > 1) {
length++;
}
}
length = length % 2 == 0 ? length / 2 : length / 2 + 1;
return length;
}
}
@Data
@NoArgsConstructor
@AllArgsConstructor
class WaterConfig {
private float alpha = 0.5f;
private int positionWidth = 100;
private int positionHeight = 100;
private Font font = new Font("宋体", Font.BOLD, 32);
private Color color = Color.red;
private Integer degree = -45;
private String logText;
}
效果
原图
单个水印
多个水印