亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 開發 > Java > 正文

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

2024-07-13 10:17:01
字體:
來源:轉載
供稿:網友

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

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

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程序設計有所幫助。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
国内精品小视频| 亚洲人成77777在线观看网| 欧美大片网站在线观看| 精品国产福利视频| 欧美日韩一区二区免费视频| 久久久久久这里只有精品| 91高清视频在线免费观看| 91免费在线视频| 中文字幕日韩欧美在线| 国产专区精品视频| 国产噜噜噜噜久久久久久久久| 91免费精品国偷自产在线| 国产精品v片在线观看不卡| 日韩电影免费在线观看中文字幕| 国产精品无码专区在线观看| 最近中文字幕mv在线一区二区三区四区| 国产精品夜色7777狼人| 久久不射电影网| 国产精品成人一区二区三区吃奶| 亚洲精品欧美日韩专区| 日韩av电影免费观看高清| 日韩中文理论片| 亚洲直播在线一区| 日韩美女免费观看| 国产91网红主播在线观看| 欧美视频在线免费| 国产91对白在线播放| 日韩成人高清在线| 国产亚洲精品va在线观看| 免费91麻豆精品国产自产在线观看| 亚洲国产成人在线视频| 一本色道久久88精品综合| 亚洲欧洲黄色网| 国产精品一区电影| 国产精品夫妻激情| 在线播放日韩专区| 伊人伊人伊人久久| 日本中文字幕不卡免费| 国产亚洲视频在线| 欧美日韩一区二区免费在线观看| 日韩精品在线观| 日韩电影大全免费观看2023年上| 国产一区二区日韩精品欧美精品| 国产精品igao视频| 国产成人精品电影| 亚洲天堂网在线观看| 国产日韩综合一区二区性色av| 久久伊人精品一区二区三区| 一本大道香蕉久在线播放29| 欧美成人四级hd版| 国产精品自产拍在线观| www.xxxx欧美| 国产精品国产自产拍高清av水多| 日韩美女免费线视频| 日韩欧美精品免费在线| 91社影院在线观看| 亚洲精品国产综合久久| 亚洲欧美综合区自拍另类| 午夜精品久久久久久久男人的天堂| 久久久久久久久久久久久久久久久久av| 97视频在线观看亚洲| 欧美性猛交xxxx免费看漫画| 色综合天天综合网国产成人网| 精品少妇一区二区30p| 91亚洲永久免费精品| 成人欧美在线视频| 亚洲视频在线免费观看| 成人免费大片黄在线播放| 久久91亚洲人成电影网站| 国产精品高精视频免费| 日韩欧美高清在线视频| 久久久免费av| 国产性色av一区二区| 日韩有码在线观看| 国产欧美亚洲视频| 亚洲福利在线视频| 欧美日韩亚洲视频| 欧美www视频在线观看| 欧美激情a∨在线视频播放| 国产精品久久一区| 欧美日韩一二三四五区| 成人av番号网| 亚洲图中文字幕| 亚洲黄页视频免费观看| 国产视频精品xxxx| 日韩视频免费看| 久久国产精品久久精品| 亚洲高清色综合| 欧美性猛交xxxx乱大交3| 精品网站999www| 97视频在线观看播放| 国产精品久久久久久一区二区| 欧美精品久久久久a| 欧美另类在线观看| 欧美高清理论片| 国产成人精品网站| 久久久国产精品一区| 色多多国产成人永久免费网站| 国产亚洲视频在线观看| 欧美激情精品久久久久久免费印度| 最好看的2019的中文字幕视频| 亚洲国产毛片完整版| 久久亚洲成人精品| 亚洲老头老太hd| 欧美在线性视频| 国产精品一区av| 岛国av一区二区三区| 一区二区三区亚洲| 日韩麻豆第一页| 欧美成人精品h版在线观看| 欧美激情奇米色| 久久综合五月天| 亚洲最大av网站| 日韩国产精品视频| 九九久久久久99精品| 亚洲黄色有码视频| 国产丝袜一区二区| 国产精品久久久久影院日本| 欧美激情区在线播放| 欧美成人亚洲成人| 九色精品免费永久在线| 亚洲精品美女视频| 午夜精品一区二区三区在线视| 久久久久久久久国产| 久久久www成人免费精品张筱雨| 国产一区二区三区在线播放免费观看| 欧美日韩另类字幕中文| 中文字幕免费精品一区| 91热精品视频| 狠狠躁夜夜躁人人爽天天天天97| 色综合天天综合网国产成人网| 欧美丝袜一区二区三区| 中文字幕综合在线| 91精品美女在线| 国产精品视频免费观看www| 色www亚洲国产张柏芝| 精品国产一区二区三区四区在线观看| 日韩高清av一区二区三区| 亚洲欧美日韩网| 国产精品专区第二| 亚洲人成五月天| 日本三级韩国三级久久| 亚洲欧美日韩精品久久奇米色影视| 亚洲视频在线免费看| 日韩在线免费视频| 欧美国产高跟鞋裸体秀xxxhd| 欧美成人中文字幕在线| 亚洲精品日韩丝袜精品| 国产精品美女午夜av| 91久久夜色精品国产网站| 欧美激情网友自拍| 亚洲精品国产电影| 亚洲欧美中文在线视频| 亚洲国产女人aaa毛片在线| 午夜精品一区二区三区在线视| 97精品一区二区视频在线观看| 77777亚洲午夜久久多人| 久久久综合av| 久久夜精品va视频免费观看| 亚洲缚视频在线观看| 亚洲成年网站在线观看| 亚洲精品久久久久中文字幕二区| 正在播放欧美一区| 91网站免费看|