水印处理工具方法
效果
代码
import lombok.extern.slf4j.Slf4j;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;
/**
* 图片添加水印
*/
@Slf4j
public class ImgWaterMark {
// 水印透明度
private float alpha = 0.9f;
// 水印文字大小
private int fontSize = 28;
// 水印文字字体
private Font font = new Font(Font.DIALOG, Font.PLAIN, this.fontSize);;
// 水印文字颜色
private Color color = Color.black;
// 水印之间的间隔
private int xmove = 200;
// 水印之间的间隔
private int ymove = 200;
private int degree = -40;
private String logoText;
private ByteArrayOutputStream out;
//rgb
public static Color color(int v1,int v2,int v3){
return new Color(v1,v2,v3);
}
//文字加时间
public static String timeText(String logoText){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return logoText + sdf.format(new Date());
}
public ImgWaterMark(float alpha, int fontSize , Color color, int move, int degree, String logoText){
this(logoText);
this.alpha = alpha;
this.fontSize = fontSize;
this.font = new Font(Font.DIALOG, Font.PLAIN, this.fontSize);
this.color = color;
this.xmove = move;
this.ymove = move;
this.degree = degree;
}
public ImgWaterMark(String logoText){
this();
this.logoText = logoText;
}
public ImgWaterMark(){
log.info("水印对象实例化");
this.logoText = "水印测试";
out = new ByteArrayOutputStream();
}
/**
* 创建图片
* @return
*/
public ImgWaterMark create(InputStream in,String fileType) throws Exception {
log.info("开始创建水印");
try {
Image source = ImageIO.read(in);
int srcWidth = source.getWidth(null);
int srcHeight = source.getHeight(null);
BufferedImage buffImg = new BufferedImage(srcWidth,srcHeight, BufferedImage.TYPE_INT_RGB);
// 得到画笔对象
Graphics2D g = buffImg.createGraphics();
// 设置对线段的锯齿状边缘处理
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(source.getScaledInstance(srcWidth, srcHeight, Image.SCALE_SMOOTH),0, 0, null);
// 设置水印旋转
g.rotate(Math.toRadians(degree), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2);
// 设置水印文字颜色
g.setColor(color);
// 设置水印文字Font
g.setFont(font);
// 设置水印文字透明度
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
int x = -srcWidth / 2;
int y = -srcHeight / 2;
int markWidth = fontSize * getTextLength(logoText);// 字体长度
int markHeight = fontSize;// 字体高度
// 循环添加水印
while (x < srcWidth * 1.5) {
y = -srcHeight / 2;
while (y < srcHeight * 1.5) {
g.drawString(logoText, x, y);
y += markHeight + ymove;
}
x += markWidth + xmove;
}
// 释放资源
g.dispose();
ImageIO.write(buffImg,fileType,out);
log.info("创建水印结束");
} catch (IOException e) {
throw new IOException("文件读取失败");
}
return this;
}
/**
* 获取文本长度。汉字为1:1,英文和数字为2:1
*/
private 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;
}
public byte[] getData() throws IOException {
byte[] data = out.toByteArray();
out.flush();
out.close();
return data;
}
public String getDataBase64() throws IOException {
return Base64.getEncoder().encodeToString(getData());
}
}
测试代码
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("D:/imgtest/a.png");
ImgWaterMark imgWaterMark = new ImgWaterMark();
imgWaterMark.create(fis,"jpg");
byte[] data = imgWaterMark.getData();
System.out.println(data.length);
FileOutputStream out = new FileOutputStream("D:/imgtest/b.png");
out.write(data);
out.flush();
out.close();
}
可以找个图片试一下哈