一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java實現給圖片添加圖片水印,文字水印及馬賽克的方法示例

Java實現給圖片添加圖片水印,文字水印及馬賽克的方法示例

2021-03-24 15:30csdn_liuliu Java教程

這篇文章主要介紹了Java實現給圖片添加圖片水印,文字水印及馬賽克的方法,涉及java針對圖片的讀取、水印添加、馬賽克設置等相關操作技巧,需要的朋友可以參考下

本文實例講述了Java實現給圖片添加圖片水印,文字水印及馬賽克的方法。分享給大家供大家參考,具體如下:

可以在eclipse中新建個Utils類,把以下代碼復制進去直接使用,以下方法實現單個或多個水印的添加

?
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package com.rzxt.fyx.common.util;
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 javax.imageio.ImageIO;
import javax.swing.ImageIcon;
/**
 * 給圖片添加水印
 * @author tgy
 *
 */
public class MarkImageUtils {
  /**
   * @param args
   */
  public static void main(String[] args) {
  String output = "F:/images/";
    String source = "F:/images/6.jpg"//源圖片路徑
    String icon = "F:/images/icon2.png"; //覆蓋圖片路徑
    String imageName = "mark_image"; //圖片名稱
    String imageType = "jpg"; //圖片類型jpg,jpeg,png,gif
    String text = "加水印了";
    int size = 4//馬賽克大小
    Integer degree = null; //水印旋轉角度-45,null表示不旋轉
    String result = null;
    //給圖片添加圖片水印
    result = MarkImageUtils.markImageByMoreIcon(icon,source,output,imageName,imageType,degree);
//    result = MarkImageUtils.markImageBySingleIcon(icon, source, output, imageName, imageType, degree);
//    //給圖片添加文字水印
//    result = MarkImageUtils.markImageByMoreText(source,output,imageName,imageType,Color.red,text,degree);
//    result = MarkImageUtils.markImageBySingleText(source,output,imageName,imageType,Color.red,text,degree);
//    //給圖片打馬賽克
//    result = MarkImageUtils.markImageByMosaic(source,output,imageName,imageType,size);
    System.out.println(result);
  }
/**
   * 給圖片不同位置添加多個圖片水印、可設置水印圖片旋轉角度
   * @param icon 水印圖片路徑(如:F:/images/icon.png)
   * @param source 沒有加水印的圖片路徑(如:F:/images/6.jpg)
   * @param output 加水印后的圖片路徑(如:F:/images/)
   * @param imageName 圖片名稱(如:11111)
   * @param imageType 圖片類型(如:jpg)
   * @param degree 水印圖片旋轉角度,為null表示不旋轉
   */
  public static String markImageByMoreIcon(String icon,String source,String output,String imageName,String imageType,Integer degree) {
    String result = "添加圖片水印出錯";
    try {
    File file = new File(source);
    File ficon = new File(icon);
    if (!file.isFile()) {
      return source + " 不是一個圖片文件!";
    }
      //將icon加載到內存中
      Image ic = ImageIO.read(ficon);
      //icon高度
      int icheight = ic.getHeight(null);
      //將源圖片讀到內存中
      Image img = ImageIO.read(file);
      //圖片寬
      int width = img.getWidth(null);
      //圖片高
      int height = img.getHeight(null);
      BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
      //創建一個指定 BufferedImage 的 Graphics2D 對象
      Graphics2D g = bi.createGraphics();
      //x,y軸默認是從0坐標開始
      int x = 0;
      int y = 0;
      //默認兩張水印圖片的間隔高度是水印圖片的1/3
      int temp = icheight/3;
      int space = 1;
      if(height>=icheight){
      space = height/icheight;
      if(space>=2){
      temp = y = icheight/2;
      if(space==1||space==0){
      x = 0;
      y = 0;
      }
      }
      }else{
      x = 0;
      y = 0;
      }
      //設置對線段的鋸齒狀邊緣處理
      g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
      //呈現一個圖像,在繪制前進行從圖像空間到用戶空間的轉換
      g.drawImage(img.getScaledInstance(width,height,Image.SCALE_SMOOTH),0,0,null);
      for(int i=0;i<space;i++){
      if (null != degree) {
        //設置水印旋轉
        g.rotate(Math.toRadians(degree),(double) bi.getWidth() / 2, (double) bi.getHeight() / 2);
      }
      //水印圖象的路徑 水印一般為gif或者png的,這樣可設置透明度
      ImageIcon imgIcon = new ImageIcon(icon);
      //得到Image對象。
      Image con = imgIcon.getImage();
      //透明度,最小值為0,最大值為1
      float clarity = 0.6f;
      g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,clarity));
      //表示水印圖片的坐標位置(x,y)
      //g.drawImage(con, 300, 220, null);
      g.drawImage(con, x, y, null);
      g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
      y+=(icheight+temp);
      }
      g.dispose();
      File sf = new File(output, imageName+"."+imageType);
    ImageIO.write(bi, imageType, sf); // 保存圖片
      result = "圖片完成添加Icon水印";
    } catch (Exception e) {
      e.printStackTrace();
    }
    return result;
  }
/**
   * 給圖片添加單個圖片水印、可設置水印圖片旋轉角度
   * @param icon 水印圖片路徑(如:F:/images/icon.png)
   * @param source 沒有加水印的圖片路徑(如:F:/images/6.jpg)
   * @param output 加水印后的圖片路徑(如:F:/images/)
   * @param imageName 圖片名稱(如:11111)
   * @param imageType 圖片類型(如:jpg)
   * @param degree 水印圖片旋轉角度,為null表示不旋轉
   */
  public static String markImageBySingleIcon(String icon,String source,String output,String imageName,String imageType,Integer degree) {
    String result = "添加圖片水印出錯";
    try {
    File file = new File(source);
    File ficon = new File(icon);
    if (!file.isFile()) {
      return source + " 不是一個圖片文件!";
    }
      //將icon加載到內存中
      Image ic = ImageIO.read(ficon);
      //icon高度
      int icheight = ic.getHeight(null);
      //將源圖片讀到內存中
      Image img = ImageIO.read(file);
      //圖片寬
      int width = img.getWidth(null);
      //圖片高
      int height = img.getHeight(null);
      BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
      //創建一個指定 BufferedImage 的 Graphics2D 對象
      Graphics2D g = bi.createGraphics();
      //x,y軸默認是從0坐標開始
      int x = 0;
      int y = (height/2)-(icheight/2);
      //設置對線段的鋸齒狀邊緣處理
      g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
      //呈現一個圖像,在繪制前進行從圖像空間到用戶空間的轉換
      g.drawImage(img.getScaledInstance(width,height,Image.SCALE_SMOOTH),0,0,null);
      if (null != degree) {
        //設置水印旋轉
        g.rotate(Math.toRadians(degree),(double) bi.getWidth() / 2, (double) bi.getHeight() / 2);
      }
      //水印圖象的路徑 水印一般為gif或者png的,這樣可設置透明度
      ImageIcon imgIcon = new ImageIcon(icon);
      //得到Image對象。
      Image con = imgIcon.getImage();
      //透明度,最小值為0,最大值為1
      float clarity = 0.6f;
      g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,clarity));
      //表示水印圖片的坐標位置(x,y)
      //g.drawImage(con, 300, 220, null);
      g.drawImage(con, x, y, null);
      g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
      g.dispose();
      File sf = new File(output, imageName+"."+imageType);
    ImageIO.write(bi, imageType, sf); // 保存圖片
      result = "圖片完成添加Icon水印";
    } catch (Exception e) {
      e.printStackTrace();
    }
    return result;
  }
  /**
   * 給圖片添加多個文字水印、可設置水印文字旋轉角度
   * @param source 需要添加水印的圖片路徑(如:F:/images/6.jpg)
   * @param outPut 添加水印后圖片輸出路徑(如:F:/images/)
   * @param imageName 圖片名稱(如:11111)
   * @param imageType 圖片類型(如:jpg)
   * @param color 水印文字的顏色
   * @param word 水印文字
   * @param degree 水印文字旋轉角度,為null表示不旋轉
   */
  public static String markImageByMoreText(String source,String output,String imageName,String imageType,Color color,String word,Integer degree) {
    String result = "添加文字水印出錯";
  try {
      //讀取原圖片信息
      File file = new File(source);
      if (!file.isFile()) {
      return file + " 不是一個圖片文件!";
    }
      Image img = ImageIO.read(file);
      //圖片寬
      int width = img.getWidth(null);
      //圖片高
      int height = img.getHeight(null);
      //文字大小
      int size = 50;
      //加水印
      BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
      Graphics2D g = bi.createGraphics();
      g.drawImage(img, 0, 0, width, height, null);
      //設置水印字體樣式
      Font font = new Font("宋體", Font.PLAIN, size);
      //根據圖片的背景設置水印顏色
      g.setColor(color);
      int x = width/3;
      int y = size;
      int space = height/size;
      for(int i=0;i<space;i++){
      //如果最后一個坐標的y軸比height高,直接退出
      if((y+size)>height){
      break;
      }
      if (null != degree) {
        //設置水印旋轉
        g.rotate(Math.toRadians(degree),(double) bi.getWidth() / 2, (double) bi.getHeight() / 2);
      }
      g.setFont(font);
      //水印位置
      g.drawString(word, x, y);
      y+=(2*size);
      }
      g.dispose();
      //輸出圖片
      File sf = new File(output, imageName+"."+imageType);
    ImageIO.write(bi, imageType, sf); // 保存圖片
      result = "圖片完成添加Word水印";
    } catch (Exception e) {
      e.printStackTrace();
    }
  return result;
  }
  /**
   * 給圖片添加單個文字水印、可設置水印文字旋轉角度
   * @param source 需要添加水印的圖片路徑(如:F:/images/6.jpg)
   * @param outPut 添加水印后圖片輸出路徑(如:F:/images/)
   * @param imageName 圖片名稱(如:11111)
   * @param imageType 圖片類型(如:jpg)
   * @param color 水印文字的顏色
   * @param word 水印文字
   * @param degree 水印文字旋轉角度,為null表示不旋轉
   */
  public static String markImageBySingleText(String source,String output,String imageName,String imageType,Color color,String word,Integer degree) {
    String result = "添加文字水印出錯";
  try {
      //讀取原圖片信息
      File file = new File(source);
      if (!file.isFile()) {
      return file + " 不是一個圖片文件!";
    }
      Image img = ImageIO.read(file);
      int width = img.getWidth(null);
      int height = img.getHeight(null);
      //加水印
      BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
      Graphics2D g = bi.createGraphics();
      g.drawImage(img, 0, 0, width, height, null);
      //設置水印字體樣式
      Font font = new Font("宋體", Font.PLAIN, 50);
      //根據圖片的背景設置水印顏色
      g.setColor(color);
      if (null != degree) {
        //設置水印旋轉
        g.rotate(Math.toRadians(degree),(double) bi.getWidth() / 2, (double) bi.getHeight() / 2);
      }
      g.setFont(font);
      int x = width/3;
      int y = height/2;
      //水印位置
      g.drawString(word, x, y);
      g.dispose();
      //輸出圖片
      File sf = new File(output, imageName+"."+imageType);
    ImageIO.write(bi, imageType, sf); // 保存圖片
      result = "圖片完成添加Word水印";
    } catch (Exception e) {
      e.printStackTrace();
    }
  return result;
  }
  /**
   * 給圖片加馬賽克
   * @param source 原圖片路徑(如:F:/images/6.jpg)
   * @param output 打馬賽克后,圖片保存的路徑(如:F:/images/)
   * @param imageName 圖片名稱(如:11111)
   * @param imageType 圖片類型(如:jpg)
   * @param size 馬賽克尺寸,即每個矩形的寬高
   * @return
   */
  public static String markImageByMosaic(String source,String output,String imageName,String imageType,int size){
  String result = "圖片打馬賽克出錯";
  try{
    File file = new File(source);
    if (!file.isFile()) {
      return file + " 不是一個圖片文件!";
    }
    BufferedImage img = ImageIO.read(file); // 讀取該圖片
    int width = img.getWidth(null); //原圖片寬
      int height = img.getHeight(null); //原圖片高
    BufferedImage bi = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
    //馬賽克格尺寸太大或太小
    if (width < size || height < size) {
      return "馬賽克格尺寸太大";
    }
    if(size<=0){
     return "馬賽克格尺寸太小";
    }
    int xcount = 0; //x方向繪制個數
    int ycount = 0; //y方向繪制個數
    if (width % size == 0) {
      xcount = width / size;
    } else {
      xcount = width / size + 1;
    }
    if (height % size == 0) {
      ycount = height / size;
    } else {
      ycount = height / size + 1;
    }
    int x = 0; //x坐標
    int y = 0;
//y坐標
    //繪制馬賽克(繪制矩形并填充顏色)
    Graphics2D g = bi.createGraphics();
    for (int i = 0; i < xcount; i++) {
      for (int j = 0; j < ycount; j++) {
        //馬賽克矩形格大小
        int mwidth = size;
        int mheight = size;
        if(i==xcount-1){  //橫向最后一個不夠一個size
          mwidth = width-x;
        }
        if(j == ycount-1){ //縱向最后一個不夠一個size
          mheight = height-y;
        }
        //矩形顏色取中心像素點RGB值
        int centerX = x;
        int centerY = y;
        if (mwidth % 2 == 0) {
          centerX += mwidth / 2;
        } else {
          centerX += (mwidth - 1) / 2;
        }
        if (mheight % 2 == 0) {
          centerY += mheight / 2;
        } else {
          centerY += (mheight - 1) / 2;
        }
        Color color = new Color(img.getRGB(centerX, centerY));
        g.setColor(color);
        g.fillRect(x, y, mwidth, mheight);
        y = y + size;// 計算下一個矩形的y坐標
      }
      y = 0;// 還原y坐標
      x = x + size;// 計算x坐標
    }
    g.dispose();
    File sf = new File(output, imageName+"."+imageType);
    ImageIO.write(bi, imageType, sf); // 保存圖片
    result = "打馬賽克成功";
  }catch(Exception e){
  e.printStackTrace();
  }
    return result;
  }
}

希望本文所述對大家java程序設計有所幫助。

原文鏈接:http://blog.csdn.net/cnsd_liuliu/article/details/52450585

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 黄动漫车车好快的车车双女主 | 欧美性另类69xxxx | 国产精品第3页 | 精品videoss另类日本 | 91精品国产综合久久香蕉 | 国产黄频| 添逼逼视频 | 亚洲色图2 | 99久热只有精品视频免费看 | 亚洲国产成人综合 | 亚洲欧美日韩高清 | 久久热国产在线视频 | 星空无限传媒视频在线观看视频 | 欧美福利二区 | 99性视频| 波多野结衣中文字幕乱七八糟 | 欧美亚洲国产另类在线观看 | 被调教的校花 | 国产美女亚洲精品久久久综合91 | 性姿势女人嗷嗷叫图片 | 动漫美女胸被狂揉扒开吃奶动态图 | 搓光美女衣 | 91精品大神国产在线播放 | 成人小视频在线观看 | 国产日产国无高清码2020 | 精品久久久久久无码人妻国产馆 | 久久久高清国产999尤物 | 日日网| 青春娱乐国产分类精品二 | 色老板在线观看 | 国产黄色大片网站 | www.亚洲色图 | 不卡视频一区二区 | 羞羞私人影院可以直接免费观影吗 | 黑人巨大爆粗亚裔女人 | 免费精品99久久国产综合精品 | 国产成人性色视频 | 寡妇快点好大好爽视频 | 男人和女人日比 | 慢慢娇淫 | 福利视频一区二区牛牛 |