效果:
原圖
加水印后的圖片
廢話不多說,直接上代碼
代碼:
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
package com.example.demo; import java.awt.alphacomposite; import java.awt.color; import java.awt.font; import java.awt.graphics2d; import java.awt.image; import java.awt.renderinghints; import java.awt.image.bufferedimage; import java.io.file; import java.io.fileoutputstream; import java.io.inputstream; import java.io.outputstream; import javax.imageio.imageio; /** * @author xuyangwei * @date 2021/1/28 09:10 */ public class watermarkutils { // 水印透明度 private static float alpha = 0 .5f; // 水印文字大小 public static final int font_size = 18 ; // 水印文字字體 private static font font = new font( "宋體" , font.plain, font_size); // 水印文字顏色 private static color color = color.gray; // 水印之間的間隔 private static final int xmove = 80 ; // 水印之間的間隔 private static final int ymove = 80 ; /** * 給圖片添加水印文字 * * @param logotext 水印文字 * @param srcimgpath 源圖片路徑 * @param targerpath 目標圖片路徑 */ public static void imagebytext(string logotext, string srcimgpath, string targerpath) { imagebytext(logotext, srcimgpath, targerpath, null ); } /** * 獲取文本長度。漢字為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 targerpath * @param degree */ public static void imagebytext(string logotext, string srcimgpath, string targerpath, integer degree) { inputstream is = null ; outputstream os = null ; try { // 源圖片 image srcimg = imageio.read( new file(srcimgpath)); int width = srcimg.getwidth( null ); // 原圖寬度 int height = srcimg.getheight( null ); // 原圖高度 bufferedimage buffimg = new bufferedimage(srcimg.getwidth( null ), srcimg.getheight( null ), bufferedimage.type_int_rgb); // 得到畫筆對象 graphics2d g = buffimg.creategraphics(); // 設置對線段的鋸齒狀邊緣處理 g.setrenderinghint(renderinghints.key_interpolation, renderinghints.value_interpolation_bilinear); g.drawimage(srcimg.getscaledinstance(srcimg.getwidth( null ), srcimg.getheight( null ), image.scale_smooth), 0 , 0 , null ); // 設置水印旋轉 if ( null != degree) { 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 = -width / 2 ; int y = -height / 2 ; int markwidth = font_size * gettextlength(logotext); // 字體長度 int markheight = font_size; // 字體高度 // 循環添加水印 while (x < width * 1.5 ) { y = -height / 2 ; while (y < height * 1.5 ) { g.drawstring(logotext, x, y); y += markheight + ymove; } x += markwidth + xmove; } // 釋放資源 g.dispose(); // 生成圖片 os = new fileoutputstream(targerpath); imageio.write(buffimg, "jpg" , os); system.out.println( "添加水印文字成功!" ); } catch (exception 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(); } } } public static void main(string[] args) { string srcimgpath = "d:/1.jpg" ; // 水印文字 string logotext = "打印無效" ; string targertextpath2 = "d:/2.jpg" ; system.out.println( "給圖片添加水印文字開始..." ); // 給圖片添加斜水印文字 watermarkutils.imagebytext(logotext, srcimgpath, targertextpath2, - 40 ); system.out.println( "給圖片添加水印文字結束..." ); } } |
總結
到此這篇關于java實現給圖片加鋪滿的網格式文字水印的文章就介紹到這了,更多相關java圖片加鋪網格式文字水印內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/q15102780705/article/details/113307631