先来个样式,效果图片,文件大小,不失真,都比较好
直接贴代码
public static void main(String[] args) throws IOException {
markImageByText("水印文字2","C:\\Users\\Desktop\\fsdownload\\aacc.jpg","C:\\Users\\Desktop\\fsdownload\\aabbcc.jpg");
}
/**
* 给图片添加水印文字、可设置水印文字的大小、旋转角度、透明度
*
* @param logoText 水印内容
* @param srcImgPath 原图片路径
* @param targerPath 目标图片路径
*/
public static void markImageByText(String logoText, String srcImgPath,
String targerPath) throws IOException {
System.out.println(("图片开始添加水印文字"));
// 1、读取源图片,Image获取图片宽度、高度
Image scrImg = ImageIO.read(new File(srcImgPath));
BufferedImage buffImg = new BufferedImage(scrImg.getWidth(null), scrImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
// 2、得到画笔对象
Graphics2D graphics = buffImg.createGraphics();
// 3、设置对线段的锯齿状边缘处理
graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics.drawImage(scrImg.getScaledInstance(scrImg.getWidth(null), scrImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);
// 4、设置水印倾斜度,这里是在图片的对角线上
// 对角线长度lengthOfDiagonal
double lengthOfDiagonal = Math.sqrt(Math.pow(buffImg.getWidth(), 2) + Math.pow(buffImg.getHeight(), 2));
double v = (Math.pow(buffImg.getWidth(), 2) + Math.pow(lengthOfDiagonal, 2) - Math.pow(buffImg.getHeight(), 2)) / (2 * buffImg.getWidth() * lengthOfDiagonal);
//get到了一个弧度数
double acos = Math.acos(v);
double myDegree = Math.toDegrees(acos);
//这里的负号决定对角线-Math.toRadians(myDegree)
graphics.rotate(-Math.toRadians(myDegree),
(double) buffImg.getWidth() / 2,
(double) buffImg.getHeight() / 2);
// 5、设置水印文字颜色
graphics.setColor(Color.DARK_GRAY);
// 6、获取源图片的宽度和高度
int width = scrImg.getWidth(null);
int heigth = scrImg.getHeight(null);
ApplicationHome ah = new ApplicationHome(Test.class);
File parentPathStringLinux = ah.getSource();
String dirPath = parentPathStringLinux.getParentFile().toString().replace("\\", "/");
//设置字体,大小
String filename = "/fonts/msyhbd.ttf";
File fontFile = new File(dirPath, filename);
Font font = new Font(fontFile.getAbsolutePath(), Font.PLAIN, 150);
graphics.setFont(font);
//8、设置透明度
graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
0.2f));
//9、设置文字位置
FontDesignMetrics metrics = FontDesignMetrics.getMetrics(graphics.getFont());
//获取文字宽度
int strWidth = metrics.stringWidth(logoText);
int xNum = width / strWidth + 1;
int yNum = heigth / 50 + 1;
int split = 100;
for (int i = 1; i <= 2 * yNum; i++) {
int y = -heigth + 50 * i + 5 * split * i;
for (int j = 0; j < xNum; j++) {
int x = strWidth * j + 3 * split * j;
graphics.drawString(logoText, x, y);
}
}
//11、释放资源
graphics.dispose();
//12、生成图片
try {
OutputStream os = null;
os = new FileOutputStream(targerPath);
ImageIO.write(buffImg, "jpg", os);
} catch (
FileNotFoundException e) {
e.printStackTrace();
}
System.out.println("图片完成添加水印文字");
}
如果linux没有安装文件中文字体,又懒得弄可以使用java,讲windows的文字放到java resouce下面,记得改文件名 讲后缀 ttc 改为 ttf 特别记住 ,因为打的是jar包所以访问不到 resouce里面的文件只有使用
ApplicationHome ah = new ApplicationHome(Test.class);
File parentPathStringLinux = ah.getSource();
String dirPath = parentPathStringLinux.getParentFile().toString().replace("\\", "/");
//设置字体,大小
String filename = "/fonts/msyhbd.ttf";
File fontFile = new File(dirPath, filename);
Font font = new Font(fontFile.getAbsolutePath(), Font.PLAIN, 150);
然后就是最后转 ImageIO.write(buffImg, "jpg", os); 一定要弄 jpg不然png 文件能从2M放到40M,有些人写的图片会失真