前言: 为了防止网站的图片资源被盗用,需要给图片打上图文的水印,之前做了一版只是在图片的右下角打上图文水印,这次升级了,要求在整个屏幕上都打上水印。话不多说,直接上代码。
/**
* 水印图片的大小
*/
public static final double BASE_SIZE = 200d;
/**
* 图片的宽高倍数
*/
public static final float BASE_MULTIPLE = 1.5f;
/**
* 水印透明度
*/
private static float alpha = 0.2f;
/**
* 水印文字大小
*/
public static final int FONT_SIZE = 11;
/**
* 水印图片的大小
*/
public static final int LOGO_SIZE = 16;
/**
* 水印文字颜色
*/
private static Color color = Color.WHITE;
/**
* 水印之间的间隔
*/
private static final int XMOVE = 60;
/**
* 水印之间的间隔
*/
private static final int YMOVE = 60;
/**
* 获取文本长度。汉字为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;
}
/**
* 给图片添加水印文字、可设置水印文字的旋转角度
*
* @param logoText
* @param srcImgPath
* @param degree
*/
public static BufferedImage addWaterAndText(String srcImgPath, BufferedImage markImgPath, String logoText, Integer degree) {
try {
// 源图片
BufferedImage buffImg = ImageUtil.read(new File(srcImgPath));
int width = buffImg.getWidth(null);
int height = buffImg.getHeight(null);
int smallNum = Integer.MAX_VALUE;
smallNum = NumberUtils.min(smallNum, width, height);
// 得到画笔对象
Graphics2D g = buffImg.createGraphics();
// 设置对线段的锯齿状边缘处理
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(buffImg, 0, 0, null);
// 设置水印旋转
if (null != degree) {
g.rotate(Math.toRadians(degree));
}
// 设置水印文字颜色
g.setColor(color);
double multipleSize = smallNum / BASE_SIZE;
// 设置水印文字Font
Font font = new Font("微软雅黑", Font.BOLD, Double.valueOf(FONT_SIZE * multipleSize).intValue());
g.setFont(font);
// 设置水印文字透明度
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
int x = 0;
double widthDIvHeight = width / Double.valueOf(height);
double heightDivWidth = height / Double.valueOf(width);
if (widthDIvHeight < BASE_MULTIPLE) {
widthDIvHeight = BASE_MULTIPLE;
}
if (heightDivWidth < BASE_MULTIPLE) {
heightDivWidth = BASE_MULTIPLE;
}
int y = Double.valueOf(-height * widthDIvHeight).intValue();
int markWidth = Double.valueOf(FONT_SIZE * multipleSize).intValue() * getTextLength(logoText);
int markHeight = Double.valueOf(FONT_SIZE * multipleSize).intValue();
// 循环添加水印
while (y < height * widthDIvHeight) {
x = Double.valueOf(-width * heightDivWidth).intValue();
while (x < width * heightDivWidth) {
// 添加文字的水印
g.drawString(logoText, x, y);
x += markWidth + Double.valueOf(XMOVE * multipleSize).intValue();
}
y += markHeight + Double.valueOf(YMOVE * multipleSize).intValue();
}
// 释放资源
g.dispose();
BufferedImage destImage2 = new BufferedImage(width, height, 1);
Graphics2D g2 = destImage2.createGraphics();
g2.drawImage(buffImg, 0, 0, width, height, null);
// 设置水印旋转
if (null != degree) {
g2.rotate(Math.toRadians(degree));
}
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
// 循环添加水印
y = Double.valueOf(-height * widthDIvHeight).intValue();
int logoSize = Double.valueOf(LOGO_SIZE * multipleSize).intValue();
while (y < height * widthDIvHeight) {
x = Double.valueOf(-width * heightDivWidth).intValue();
while (x < width * heightDivWidth) {
// 画水印的
g2.drawImage(markImgPath, x - logoSize, y - Double.valueOf(logoSize * 0.8).intValue(), logoSize, logoSize, null);
x += markWidth + Double.valueOf(XMOVE * multipleSize).intValue();
}
y += markHeight + Double.valueOf(YMOVE * multipleSize).intValue();
}
g2.dispose();
// 生成图片
return destImage2;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
效果图: