8.9
luodangjia
2024-09-03 2855ae89d2ab6268e1edc42c3e9d7e73c8e7259e
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package com.ruoyi.worker.utils;
 
 
import com.ruoyi.worker.entity.MultipartFileDto;
import org.springframework.web.multipart.MultipartFile;
 
import javax.imageio.ImageIO;
 
import java.awt.AlphaComposite;
 
import java.awt.Color;
 
import java.awt.Font;
 
import java.awt.Graphics2D;
 
import java.awt.RenderingHints;
 
import java.awt.image.BufferedImage;
 
import java.io.*;
 
/**
 
 * @author inspur
 
 * 定义文字加水印
 
 */
 
public class FileImageWatermarkUtils {
 
    /**
 
     * 水印字体
 
     */
 
    private static final Font FONT = new Font("宋体", Font.PLAIN, 40);
 
    /**
 
     * 透明度
 
     */
 
    private static final AlphaComposite COMPOSITE = AlphaComposite
 
            .getInstance(AlphaComposite.SRC_OVER, 0.9f);
 
    /**
 
     * 水印之间的间隔
 
     */
 
    private static final int X_MOVE = 150;
 
    /**
 
     * 水印之间的间隔
 
     */
 
    private static final int Y_MOVE = 200;
 
    /**
 
     * 打水印(文字)
 
     *
 
     * @param file       MultipartFile
 
     * @return MultipartFile
 
     */
 
    public static MultipartFile markWithContent(MultipartFile file,String keyword,String address) {
 
        FileOutputStream fos = null;
 
        try {
 
            BufferedImage srcImg = ImageIO.read(file.getInputStream());
 
            // 图片宽、高
 
            int imgWidth = srcImg.getWidth();
 
            int imgHeight = srcImg.getHeight();
 
            // 图片缓存
 
            BufferedImage bufImg = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
 
            // 创建绘图工具
 
            Graphics2D graphics = bufImg.createGraphics();
 
            // 画入原始图像
 
            graphics.drawImage(srcImg, 0, 0, imgWidth, imgHeight, null);
 
            // 设置水印颜色
 
            graphics.setColor(Color.RED);
 
            // 设置水印透明度
 
            graphics.setComposite(COMPOSITE);
 
            // 设置倾斜角度
 
/*            graphics.rotate(Math.toRadians(-35), (double) bufImg.getWidth() / 2,
 
                    (double) bufImg.getHeight() / 2);*/
 
            // 设置水印字体
 
            graphics.setFont(FONT);
 
            // 消除java.awt.Font字体的锯齿
 
//            graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
 
            int xCoordinate , yCoordinate;
            int xCoordinate1 , yCoordinate1;
            int xCoordinate2 , yCoordinate2;
 
            String mark = "ls65535";
 
            // 字体长度
 
            int markWidth = FONT.getSize() * getTextLength(keyword);
 
            // 字体高度
 
            int markHeight = FONT.getSize();
 
            //位置控制 水印位于图片 下方右下角
            if (address.length()<=23){
 
 
                xCoordinate = (int) ((imgWidth - getWatermarkLength(keyword, graphics)) * 0.05);
 
                yCoordinate = (int) (imgHeight * 0.91);
 
                xCoordinate1 = (int) ((imgWidth - getWatermarkLength(keyword, graphics)) * 0.05);
 
                yCoordinate1 = (int) (imgHeight * 0.96);
 
 
                graphics.drawString("地址:" + address, xCoordinate, yCoordinate);
                graphics.drawString("时间:" + keyword, xCoordinate1, yCoordinate1);
 
                InputStream inputStream = buffToInputStream(bufImg);
 
 
                // 释放画图工具
 
                graphics.dispose();
 
                return new MultipartFileDto(file.getName(),file.getOriginalFilename(),file.getContentType(),inputStream);
 
            }else {
                xCoordinate2 = (int) ((imgWidth - getWatermarkLength(keyword, graphics)) * 0.05);
 
                yCoordinate2 = (int) (imgHeight * 0.91);
 
                xCoordinate = (int) ((imgWidth - getWatermarkLength(keyword, graphics)) * 0.05);
 
                yCoordinate = (int) (imgHeight * 0.88);
 
                xCoordinate1 = (int) ((imgWidth - getWatermarkLength(keyword, graphics)) * 0.05);
 
                yCoordinate1 = (int) (imgHeight * 0.96);
 
 
                graphics.drawString("地址:" + address.substring(0,20), xCoordinate, yCoordinate);
                graphics.drawString(address.substring(20), xCoordinate2, yCoordinate2);
 
                graphics.drawString("时间:" + keyword, xCoordinate1, yCoordinate1);
 
                InputStream inputStream = buffToInputStream(bufImg);
 
 
            // 释放画图工具
 
            graphics.dispose();
 
            return new MultipartFileDto(file.getName(),file.getOriginalFilename(),file.getContentType(),inputStream);
            }
 
        } catch (Exception e) {
 
            e.printStackTrace();
 
        } finally {
 
            if (fos != null) {
 
                try {
 
                    fos.flush();
 
                    fos.close();
 
                } catch (Exception e) {
 
                    e.printStackTrace();
 
                }
 
            }
 
        }
 
        return null;
 
    }
 
    /**
 
     * 计算水印文本长度
 
     * 1、中文长度即文本长度 2、英文长度为文本长度二分之一
 
     * @param text
 
     * @return
 
     */
 
    public 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;
 
    }
 
    public static InputStream buffToInputStream(BufferedImage buffer) throws IOException {
 
        ByteArrayOutputStream os = new ByteArrayOutputStream();
 
        ImageIO.write(buffer, "png", os);
 
        return new ByteArrayInputStream(os.toByteArray());
 
    }
 
    public static int getWatermarkLength(String waterMarkContent, Graphics2D g) {
 
        return g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length());
 
    }
 
}