Java實現給圖片添加圖片浮水印,文字浮水印及馬賽克的方法樣本,java浮水印

來源:互聯網
上載者:User

Java實現給圖片添加圖片浮水印,文字浮水印及馬賽克的方法樣本,java浮水印

本文執行個體講述了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圖片操作技巧匯總》、《java日期與時間操作技巧匯總》、《Java操作DOM節點技巧總結》、《Java檔案與目錄操作技巧匯總》及《Java資料結構與演算法教程》。

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.