先上效果图
这种普通水印应该符合大部分要求,废话不说直接上代码;
package com.util;
import sun.font.FontDesignMetrics;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
public class WaterMarkUtil1 {
// 水印之间的间隔
private static final int XMOVE = 60;
// 水印之间的间隔
private static final int YMOVE = 60;
public static void main(String[] args) {
Color color = string2Color("030303");
String logoText="我的水印";
String srcPaht="C:\\Users\\Administrator\\Desktop\\11.png";
String newPath="C:\\Users\\Administrator\\Desktop\\2.png";
Integer degree=45;
markImageByText(logoText,srcPaht,newPath,degree,color,"png");
}
/**
* 给图片添加水印文字、可设置水印文字的旋转角度
* @param srcImgPath 源图片路径
* @param newImagePath 新图片路径
* @param degree 旋转角度
* @param color 字体颜色
* @param formaName 图片后缀
* */
public static void markImageByText(String logoText, String srcImgPath, String newImagePath, Integer degree, Color color, String formaName){
InputStream is=null;
OutputStream os=null;
try {
//1根据原图片获取笔画对象
Image srcImg = ImageIO.read(new File(srcImgPath));
int srcImgHeight = srcImg.getHeight(null);
int srcImgWidth = srcImg.getWidth(null);
System.out.println("=====Srcwidth:"+srcImgWidth+"=====Srcheight:"+srcImgHeight+"=======");
BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
int width = buffImg.getWidth();
int height = buffImg.getHeight();
System.out.println("=====width:"+width+"=====height:"+height+"=======");
//2获取笔画对象
Graphics2D g = buffImg.createGraphics();
//3设置对线段的锯齿状边缘处理
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), java.awt.Image.SCALE_SMOOTH), 0, 0, null);
//4设置水印旋转
if(null!=degree){
g.rotate(Math.toRadians(degree), buffImg.getWidth()/2,buffImg.getHeight() /2);
}
//5 设置水印文字颜色
g.setColor(color);
//6设置水印文字Font
Font font = new Font("宋体", java.awt.Font.BOLD, buffImg.getHeight() /10);
g.setFont(font);
FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);
//7设置水印透明度
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.7f));
// 8、第一参数->设置的内容,后面两个参数->文字在图片上的坐标位置(x,y)
int x = -srcImgWidth / 2;
int y = -srcImgHeight / 2;
int markWidth = font.getSize() * getTextLength(logoText);// 字体长度
int markHeight = font.getSize();
while (x < srcImgWidth * 1.5) {
y = -srcImgHeight / 2;
while (y < srcImgHeight * 1.5) {
g.drawString(logoText, x, y);
y += markHeight + YMOVE;
}
x += markWidth + XMOVE;
}
//9释放资源
g.dispose();
os = new FileOutputStream(newImagePath);
ImageIO.write(buffImg, formaName, os);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (null != is) {
is.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (null != os) {
os.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
//获取字符串长度
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 src="FFFF00"格式
* */
public static Color string2Color(String src){
String substring = src.substring(0, 2);
String substring1 = src.substring(2, 4);
String substring2 = src.substring(4, 6);
int r = Integer.parseInt(substring, 16);
int g = Integer.parseInt(substring1, 16);
int b = Integer.parseInt(substring2, 16);
return new Color(r,g,b);
}
}
代码没有什么好解释的,里面都有注释,api的意思可以自行搜索