1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
// 文字水印
// Graphics2D实现
public void addTextWaterMark(BufferedImage targetImg, Color textColor, int fontSize, String text, String outPath) {
try {
int width = targetImg.getWidth(); //图片宽
int height = targetImg.getHeight(); //图片高
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
Graphics2D g = bufferedImage.createGraphics();
g.drawImage(targetImg, 0, 0, width, height, null);
g.setColor(textColor); //水印颜色
g.setFont(new Font("微软雅黑", Font.ITALIC, fontSize));
// 水印内容放置在右下角
int x = width - (text.length() + 1) * fontSize;
int y = height - fontSize * 2;
g.drawString(text, x, y);
FileOutputStream outImgStream = new FileOutputStream(outPath);
ImageIO.write(bufferedImage, "jpg", outImgStream);
outImgStream.flush();
outImgStream.close();
g.dispose();
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void addTextTest() throws IOException {
File file = new File("D:\\Desktop\\test\\2.jpg");
BufferedImage image = ImageIO.read(file);
addTextWaterMark(image, Color.RED, 8, "测试文本水印", "D:\\Desktop\\test\\444.jpg");// 80 or 8
}
|