欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實(shí)現(xiàn)給圖片添加圖片水印,文字水印及馬賽克的方法示例

 更新時(shí)間:2018年01月20日 12:11:46   作者:csdn_liuliu  
這篇文章主要介紹了Java實(shí)現(xiàn)給圖片添加圖片水印,文字水印及馬賽克的方法,涉及java針對(duì)圖片的讀取、水印添加、馬賽克設(shè)置等相關(guān)操作技巧,需要的朋友可以參考下

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

可以在eclipse中新建個(gè)Utils類,把以下代碼復(fù)制進(jìn)去直接使用,以下方法實(shí)現(xiàn)單個(gè)或多個(gè)水印的添加

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; //水印旋轉(zhuǎn)角度-45,null表示不旋轉(zhuǎn)
    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);
  }
/**
   * 給圖片不同位置添加多個(gè)圖片水印、可設(shè)置水印圖片旋轉(zhuǎn)角度
   * @param icon 水印圖片路徑(如:F:/images/icon.png)
   * @param source 沒有加水印的圖片路徑(如:F:/images/6.jpg)
   * @param output 加水印后的圖片路徑(如:F:/images/)
   * @param imageName 圖片名稱(如:11111)
   * @param imageType 圖片類型(如:jpg)
   * @param degree 水印圖片旋轉(zhuǎn)角度,為null表示不旋轉(zhuǎn)
   */
  public static String markImageByMoreIcon(String icon,String source,String output,String imageName,String imageType,Integer degree) {
    String result = "添加圖片水印出錯(cuò)";
    try {
    File file = new File(source);
    File ficon = new File(icon);
    if (!file.isFile()) {
      return source + " 不是一個(gè)圖片文件!";
    }
      //將icon加載到內(nèi)存中
      Image ic = ImageIO.read(ficon);
      //icon高度
      int icheight = ic.getHeight(null);
      //將源圖片讀到內(nèi)存中
      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);
      //創(chuàng)建一個(gè)指定 BufferedImage 的 Graphics2D 對(duì)象
      Graphics2D g = bi.createGraphics();
      //x,y軸默認(rèn)是從0坐標(biāo)開始
      int x = 0;
      int y = 0;
      //默認(rèn)兩張水印圖片的間隔高度是水印圖片的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;
      }
      //設(shè)置對(duì)線段的鋸齒狀邊緣處理
      g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
      //呈現(xiàn)一個(gè)圖像,在繪制前進(jìn)行從圖像空間到用戶空間的轉(zhuǎn)換
      g.drawImage(img.getScaledInstance(width,height,Image.SCALE_SMOOTH),0,0,null);
      for(int i=0;i<space;i++){
      if (null != degree) {
        //設(shè)置水印旋轉(zhuǎn)
        g.rotate(Math.toRadians(degree),(double) bi.getWidth() / 2, (double) bi.getHeight() / 2);
      }
      //水印圖象的路徑 水印一般為gif或者png的,這樣可設(shè)置透明度
      ImageIcon imgIcon = new ImageIcon(icon);
      //得到Image對(duì)象。
      Image con = imgIcon.getImage();
      //透明度,最小值為0,最大值為1
      float clarity = 0.6f;
      g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,clarity));
      //表示水印圖片的坐標(biāo)位置(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;
  }
/**
   * 給圖片添加單個(gè)圖片水印、可設(shè)置水印圖片旋轉(zhuǎn)角度
   * @param icon 水印圖片路徑(如:F:/images/icon.png)
   * @param source 沒有加水印的圖片路徑(如:F:/images/6.jpg)
   * @param output 加水印后的圖片路徑(如:F:/images/)
   * @param imageName 圖片名稱(如:11111)
   * @param imageType 圖片類型(如:jpg)
   * @param degree 水印圖片旋轉(zhuǎn)角度,為null表示不旋轉(zhuǎn)
   */
  public static String markImageBySingleIcon(String icon,String source,String output,String imageName,String imageType,Integer degree) {
    String result = "添加圖片水印出錯(cuò)";
    try {
    File file = new File(source);
    File ficon = new File(icon);
    if (!file.isFile()) {
      return source + " 不是一個(gè)圖片文件!";
    }
      //將icon加載到內(nèi)存中
      Image ic = ImageIO.read(ficon);
      //icon高度
      int icheight = ic.getHeight(null);
      //將源圖片讀到內(nèi)存中
      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);
      //創(chuàng)建一個(gè)指定 BufferedImage 的 Graphics2D 對(duì)象
      Graphics2D g = bi.createGraphics();
      //x,y軸默認(rèn)是從0坐標(biāo)開始
      int x = 0;
      int y = (height/2)-(icheight/2);
      //設(shè)置對(duì)線段的鋸齒狀邊緣處理
      g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
      //呈現(xiàn)一個(gè)圖像,在繪制前進(jìn)行從圖像空間到用戶空間的轉(zhuǎn)換
      g.drawImage(img.getScaledInstance(width,height,Image.SCALE_SMOOTH),0,0,null);
      if (null != degree) {
        //設(shè)置水印旋轉(zhuǎn)
        g.rotate(Math.toRadians(degree),(double) bi.getWidth() / 2, (double) bi.getHeight() / 2);
      }
      //水印圖象的路徑 水印一般為gif或者png的,這樣可設(shè)置透明度
      ImageIcon imgIcon = new ImageIcon(icon);
      //得到Image對(duì)象。
      Image con = imgIcon.getImage();
      //透明度,最小值為0,最大值為1
      float clarity = 0.6f;
      g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,clarity));
      //表示水印圖片的坐標(biāo)位置(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;
  }
  /**
   * 給圖片添加多個(gè)文字水印、可設(shè)置水印文字旋轉(zhuǎn)角度
   * @param source 需要添加水印的圖片路徑(如:F:/images/6.jpg)
   * @param outPut 添加水印后圖片輸出路徑(如:F:/images/)
   * @param imageName 圖片名稱(如:11111)
   * @param imageType 圖片類型(如:jpg)
   * @param color 水印文字的顏色
   * @param word 水印文字
   * @param degree 水印文字旋轉(zhuǎn)角度,為null表示不旋轉(zhuǎn)
   */
  public static String markImageByMoreText(String source,String output,String imageName,String imageType,Color color,String word,Integer degree) {
    String result = "添加文字水印出錯(cuò)";
  try {
      //讀取原圖片信息
      File file = new File(source);
      if (!file.isFile()) {
      return file + " 不是一個(gè)圖片文件!";
    }
      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);
      //設(shè)置水印字體樣式
      Font font = new Font("宋體", Font.PLAIN, size);
      //根據(jù)圖片的背景設(shè)置水印顏色
      g.setColor(color);
      int x = width/3;
      int y = size;
      int space = height/size;
      for(int i=0;i<space;i++){
      //如果最后一個(gè)坐標(biāo)的y軸比height高,直接退出
      if((y+size)>height){
      break;
      }
      if (null != degree) {
        //設(shè)置水印旋轉(zhuǎn)
        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;
  }
  /**
   * 給圖片添加單個(gè)文字水印、可設(shè)置水印文字旋轉(zhuǎn)角度
   * @param source 需要添加水印的圖片路徑(如:F:/images/6.jpg)
   * @param outPut 添加水印后圖片輸出路徑(如:F:/images/)
   * @param imageName 圖片名稱(如:11111)
   * @param imageType 圖片類型(如:jpg)
   * @param color 水印文字的顏色
   * @param word 水印文字
   * @param degree 水印文字旋轉(zhuǎn)角度,為null表示不旋轉(zhuǎn)
   */
  public static String markImageBySingleText(String source,String output,String imageName,String imageType,Color color,String word,Integer degree) {
    String result = "添加文字水印出錯(cuò)";
  try {
      //讀取原圖片信息
      File file = new File(source);
      if (!file.isFile()) {
      return file + " 不是一個(gè)圖片文件!";
    }
      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);
      //設(shè)置水印字體樣式
      Font font = new Font("宋體", Font.PLAIN, 50);
      //根據(jù)圖片的背景設(shè)置水印顏色
      g.setColor(color);
      if (null != degree) {
        //設(shè)置水印旋轉(zhuǎn)
        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 馬賽克尺寸,即每個(gè)矩形的寬高
   * @return
   */
  public static String markImageByMosaic(String source,String output,String imageName,String imageType,int size){
  String result = "圖片打馬賽克出錯(cuò)";
  try{
    File file = new File(source);
    if (!file.isFile()) {
      return file + " 不是一個(gè)圖片文件!";
    }
    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方向繪制個(gè)數(shù)
    int ycount = 0; //y方向繪制個(gè)數(shù)
    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坐標(biāo)
    int y = 0;
//y坐標(biāo)
    //繪制馬賽克(繪制矩形并填充顏色)
    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){  //橫向最后一個(gè)不夠一個(gè)size
          mwidth = width-x;
        }
        if(j == ycount-1){ //縱向最后一個(gè)不夠一個(gè)size
          mheight = height-y;
        }
        //矩形顏色取中心像素點(diǎn)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;// 計(jì)算下一個(gè)矩形的y坐標(biāo)
      }
      y = 0;// 還原y坐標(biāo)
      x = x + size;// 計(jì)算x坐標(biāo)
    }
    g.dispose();
    File sf = new File(output, imageName+"."+imageType);
    ImageIO.write(bi, imageType, sf); // 保存圖片
    result = "打馬賽克成功";
  }catch(Exception e){
  e.printStackTrace();
  }
    return result;
  }
}

更多java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java圖片操作技巧匯總》、《java日期與時(shí)間操作技巧匯總》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》及《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》。

希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • MyBatis實(shí)現(xiàn)插入大量數(shù)據(jù)方法詳解

    MyBatis實(shí)現(xiàn)插入大量數(shù)據(jù)方法詳解

    最近在公司項(xiàng)目開發(fā)中遇到批量數(shù)據(jù)插入或者更新,下面這篇文章主要給大家介紹了關(guān)于MyBatis實(shí)現(xiàn)批量插入的相關(guān)資料,需要的朋友可以參考下
    2022-11-11
  • 一文詳解Java項(xiàng)目中如何優(yōu)雅的使用枚舉類型

    一文詳解Java項(xiàng)目中如何優(yōu)雅的使用枚舉類型

    枚舉類型在開發(fā)中是很常見的,有非常多的應(yīng)用場(chǎng)景,這篇文章我們就來學(xué)習(xí)一下項(xiàng)目中如何優(yōu)雅的使用枚舉類型,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-03-03
  • java如何使用fastjson修改多層嵌套的Objectjson數(shù)據(jù)

    java如何使用fastjson修改多層嵌套的Objectjson數(shù)據(jù)

    這篇文章主要介紹了java如何使用fastjson修改多層嵌套的Objectjson數(shù)據(jù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • SpringMvc請(qǐng)求處理參數(shù)?和?響應(yīng)數(shù)據(jù)處理的示例詳解

    SpringMvc請(qǐng)求處理參數(shù)?和?響應(yīng)數(shù)據(jù)處理的示例詳解

    這篇文章主要介紹了SpringMvc請(qǐng)求處理參數(shù)和響應(yīng)數(shù)據(jù)處理,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-09-09
  • 解決springboot bean中大寫的字段返回變成小寫的問題

    解決springboot bean中大寫的字段返回變成小寫的問題

    這篇文章主要介紹了解決springboot bean中大寫的字段返回變成小寫的問題,具有很好的參考價(jià)值希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • 云IDE:Eclipse Che:Eclipse下一代IDE(推薦)

    云IDE:Eclipse Che:Eclipse下一代IDE(推薦)

    這篇文章主要介紹了云IDE:Eclipse Che:Eclipse下一代IDE,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • SpringBoot 集成 Memcached的方法示例

    SpringBoot 集成 Memcached的方法示例

    這篇文章主要介紹了SpringBoot 集成 Memcached的方法示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • ReentrantLock條件變量使多個(gè)線程順序執(zhí)行

    ReentrantLock條件變量使多個(gè)線程順序執(zhí)行

    這篇文章主要為大家介紹了ReentrantLock條件變量使多個(gè)線程順序執(zhí)行,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • MyBatis中#{}和${}的區(qū)別詳解

    MyBatis中#{}和${}的區(qū)別詳解

    mybatis和ibatis總體來講都差不多的。下面小編給大家探討下mybatis中#{}和${}的區(qū)別,感興趣的朋友一起學(xué)習(xí)吧
    2016-08-08
  • java結(jié)合email實(shí)現(xiàn)自動(dòng)推送功能

    java結(jié)合email實(shí)現(xiàn)自動(dòng)推送功能

    這篇文章主要介紹了java結(jié)合email實(shí)現(xiàn)自動(dòng)推送功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03

最新評(píng)論