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

Java基于裝飾者模式實現(xiàn)的圖片工具類實例【附demo源碼下載】

 更新時間:2017年09月06日 09:48:11   作者:qq_35433926  
這篇文章主要介紹了Java基于裝飾者模式實現(xiàn)的圖片工具類,結(jié)合完整實例形式分析了裝飾者模式實現(xiàn)圖片的判斷、水印、縮放、復(fù)制等功能,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下

本文實例講述了Java基于裝飾者模式實現(xiàn)的圖片工具類。分享給大家供大家參考,具體如下:

ImgUtil.java:

/*
 * 裝飾者模式實現(xiàn)圖片處理工具類
 * 類似java的io流 - 
 * Img類似低級流可以獨立使用
 * Press和Resize類似高級流
 * 需要依賴于低級流
 */
package util;
import java.io.File;
import java.util.List;
/**
 * 圖片工具類(裝飾者)和圖片(被裝飾者)的公共接口
 * @author xlk
 */
public interface ImgUtil {
  /** 裝飾方法 - 處理圖片 */
  List<File> dispose();
}

AbstractImgUtil.java:

package util;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
/**
 * 抽象圖片工具類 - 抽象裝飾者
 * @author xlk
 */
public abstract class AbstractImgUtil implements ImgUtil {
  private ImgUtil imgUtil;
  @Override
  public List<File> dispose() {
    return imgUtil.dispose();
  }
  public AbstractImgUtil(){}
  public AbstractImgUtil(ImgUtil imgUtil) {
    this.imgUtil = imgUtil;
  }
  /**
   * 判斷文件是不是圖片
   * @param file 被判斷的文件
   * @return 圖片返回true 非圖片返回false
   * @throws IOException 
   */
  public static boolean isImg(File file) {
    if (file.isDirectory()) {
      return false;
    }
    try {
      ImageInputStream iis = ImageIO.createImageInputStream(file);
      Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);
      if (!iter.hasNext()) {//文件不是圖片
        return false;
      }
      return true;
    } catch (IOException e) {
      return false;
    }
  }
}

Press.java:

package util;
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.imageio.ImageIO;
/**
 * 加水印 - 裝飾者
 * @author xlk
 */
public class Press extends AbstractImgUtil {
  private  List<File>  src;   //圖片路徑集合
  private  String    waterImg;//水印圖片路徑
  private  Integer    x;     //水印圖片距離目標圖片左側(cè)的偏移量, 如果x<0, 則在正中間
  private  Integer    y;     //水印圖片距離目標圖片上側(cè)的偏移量, 如果y<0, 則在正中間
  private  float    alpha;   //水印透明度(0.0 -- 1.0, 0.0為完全透明,1.0為完全不透明)
  @Override
  public List<File> dispose() {
    src = super.dispose();
    return press();
  }
  /** 加水印 - 具體裝飾方法 */
  private List<File> press() {
    if (waterImg==null || "".equals(waterImg)) {
      throw new RuntimeException("水印路徑不能為空");
    }
    if (!isImg(new File(waterImg))) {
      throw new RuntimeException("水印路徑所指向的文件不是圖片");
    }
    if (src.size()<=0) {
      return src;
    }
    if (x!=null && y!=null) {
      for (File f: src) {
        press(f.getPath(), waterImg, f.getParent(), x, y, alpha);
      }
    } else {
      for (File f: src) {
        press(f.getPath(), waterImg, f.getParent(), alpha);
      }
    }
    return src;
  }
  public Press() {}
  public Press(ImgUtil imgUtil, String waterImg, float alpha) {
    super(imgUtil);
    this.waterImg = waterImg;
    this.alpha = alpha;
  }
  public Press(ImgUtil imgUtil, String waterImg, Integer x, Integer y, float alpha) {
    super(imgUtil);
    this.waterImg = waterImg;
    this.x = x;
    this.y = y;
    this.alpha = alpha;
  }
  public String getWaterImg() {
    return waterImg;
  }
  public void setWaterImg(String waterImg) {
    this.waterImg = waterImg;
  }
  public Integer getX() {
    return x;
  }
  public void setX(Integer x) {
    this.x = x;
  }
  public Integer getY() {
    return y;
  }
  public void setY(Integer y) {
    this.y = y;
  }
  public float getAlpha() {
    return alpha;
  }
  public void setAlpha(float alpha) {
    this.alpha = alpha;
  }
  /** 添加圖片水印 */
  private static void press(String src, String waterImg, String target, int x, int y, float alpha) {
    File newFile = null;
    try {
      File file = new File(src);
      Image image = ImageIO.read(file);
      int width = image.getWidth(null);
      int height = image.getHeight(null);
      BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
      Graphics2D g = bufferedImage.createGraphics();
      g.drawImage(image, 0, 0, width, height, null);
      
      Image waterImage = ImageIO.read(new File(waterImg)); // 水印文件
      int width_1 = waterImage.getWidth(null);
      int height_1 = waterImage.getHeight(null);
      g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
      
      int widthDiff = width - width_1;
      int heightDiff = height - height_1;
      if (x < 0) {
        x = widthDiff / 2;
      } else if (x > widthDiff) {
        x = widthDiff;
      }
      if (y < 0) {
        y = heightDiff / 2;
      } else if (y > heightDiff) {
        y = heightDiff;
      }
      g.drawImage(waterImage, x, y, width_1, height_1, null); // 水印文件結(jié)束
      g.dispose();
      
      File dir = new File(target);
      if (!dir.exists()) {
        dir.mkdirs();
      }
      newFile = new File(target + File.separator + file.getName());
      ImageIO.write(bufferedImage, "jpg", newFile);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  /** 平鋪添加圖片水印 */
  private static void press(String src, String waterImg, String target, float alpha) {
    File newFile = null;
    try {
      File file = new File(src);
      Image image = ImageIO.read(file);
      int width = image.getWidth(null);
      int height = image.getHeight(null);
      BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
      Graphics2D g = bufferedImage.createGraphics();
      g.drawImage(image, 0, 0, width, height, null);
      
      Image waterImage = ImageIO.read(new File(waterImg)); // 水印文件
      int width_1 = waterImage.getWidth(null);
      int height_1 = waterImage.getHeight(null);
      g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
      
      int rpt_x = width>width_1?(int)Math.ceil(Double.valueOf(width)/width_1):1;//x方向重復(fù)次數(shù)
      int rpt_y = height>height_1?(int)Math.ceil(Double.valueOf(height)/height_1):1;//y方向重復(fù)次數(shù)
      for (int i=0; i<rpt_x; i++) {
        for (int j=0; j<rpt_y; j++) {
          g.drawImage(waterImage, i*width_1, j*height_1, width_1, height_1, null);
        }
      }// 水印文件結(jié)束
      g.dispose();
      
      File dir = new File(target);
      if (!dir.exists()) {
        dir.mkdirs();
      }
      newFile = new File(target + File.separator + file.getName());
      ImageIO.write(bufferedImage, "jpg", newFile);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Resize.java:

package util;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.imageio.ImageIO;
/**
 * 縮放 - 裝飾者
 * @author xlk
 */
public class Resize extends AbstractImgUtil {
  /** 比例不同邊界顏色 */
  private static final Color color = new Color(230,230,230);
  private  List<File>  src;  //圖片路徑集合
  private  int      height;  //處理后高度
  private int      width;  //處理后寬度
  private double    ratio;  //處理后高寬比
  private boolean    bb;    //比例不對時是否補白
  @Override
  public List<File> dispose() {
    src = super.dispose();
    return resize();
  }
  /** 縮放 - 具體裝飾方法 */
  private List<File> resize() {
    if (src.size()<=0) {
      return src;
    }
    if (ratio>0) {
      for (File f: src) {
        resize(f.getPath(), f.getParent(), ratio, bb);
      }
    } else if (height>0 && width>0) {
      for (File f: src) {
        resize(f.getPath(), f.getParent(), height, width, bb);
      }
    }
    return src;
  }
  public Resize() {}
  public Resize(ImgUtil imgUtil, int height, int width, boolean bb) {
    super(imgUtil);
    this.height = height;
    this.width = width;
    this.bb = bb;
  }
  public Resize(ImgUtil imgUtil, double ratio, boolean bb) {
    super(imgUtil);
    this.ratio = ratio;
    this.bb = bb;
  }
  public int getHeight() {
    return height;
  }
  public void setHeight(int height) {
    this.height = height;
  }
  public int getWidth() {
    return width;
  }
  public void setWidth(int width) {
    this.width = width;
  }
  public double getRatio() {
    return ratio;
  }
  public void setRatio(double ratio) {
    this.ratio = ratio;
  }
  public boolean isBb() {
    return bb;
  }
  public void setBb(boolean bb) {
    this.bb = bb;
  }
  /** 圖片縮放-按照尺寸 */
  private static void resize(String src, String target, int height, int width, boolean bb) {
    File newFile = null;
    try {
      double ratio = 0; //縮放比例
      File f = new File(src);
      BufferedImage bi = ImageIO.read(f);
      Image itemp = bi.getScaledInstance(width, height, BufferedImage.SCALE_SMOOTH);
      //計算比例
      if (Double.valueOf(bi.getHeight())/bi.getWidth() > Double.valueOf(height)/width) {
        ratio = Double.valueOf(height) / bi.getHeight();
      } else {
        ratio = Double.valueOf(width) / bi.getWidth();
      }
      AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);
      itemp = op.filter(bi, null);
      if (bb) {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();
        g.setColor(color);
        g.fillRect(0, 0, width, height);
        if (width == itemp.getWidth(null))
          g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2, itemp.getWidth(null), itemp.getHeight(null), color, null);
        else
          g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0, itemp.getWidth(null), itemp.getHeight(null), color, null);
        g.dispose();
        itemp = image;
      }
      
      File dir = new File(target);
      if (!dir.exists()) {
        dir.mkdirs();
      }
      newFile = new File(target + File.separator + f.getName());
      ImageIO.write((BufferedImage) itemp, "jpg", newFile);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  /** 圖片縮放-按照高寬比 */
  private static void resize(String src, String target, double ratio, boolean bb) {
    File newFile = null;
    try {
      File f = new File(src);
      BufferedImage bi = ImageIO.read(f);
      //計算尺寸
      int width = bi.getWidth();
      int height = bi.getHeight();
      if (Double.valueOf(height)/width<ratio) {
        height = (int)(width*ratio);
      } else {
        width = (int)(Double.valueOf(height)/ratio);
      }
      Image itemp = bi.getScaledInstance(width, height, BufferedImage.SCALE_SMOOTH);
      AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(1, 1), null);
      itemp = op.filter(bi, null);
      if (bb) {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();
        g.setColor(color);
        g.fillRect(0, 0, width, height);
        if (width == itemp.getWidth(null))
          g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2, itemp.getWidth(null), itemp.getHeight(null), color, null);
        else
          g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0, itemp.getWidth(null), itemp.getHeight(null), color, null);
        g.dispose();
        itemp = image;
      }
      
      File dir = new File(target);
      if (!dir.exists()) {
        dir.mkdirs();
      }
      newFile = new File(target + File.separator + f.getName());
      ImageIO.write((BufferedImage) itemp, "jpg", newFile);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Img.java:

package util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
 * 圖片 - 原組件
 * @author xlk
 */
public class Img implements ImgUtil {
  private  String  src;  //源圖片或圖片文件夾路徑
  private  String  target;  //目標文件夾路徑
  private  boolean  inner;  //true-包含子文件夾, false-僅當(dāng)前文件夾
  @Override
  public List<File> dispose() {
    return copy();
  }
  /** 復(fù)制 - 被裝飾者初始狀態(tài) */
  private List<File> copy() {
    if (src==null || "".equals(src) || target==null || "".equals(target)) {
      throw new RuntimeException("源路徑或目標路徑不能為空");
    }
    File srcFile = new File(src);
    List<File> list = new ArrayList<File>();
    
    File targetDir = new File(target);
    if (!targetDir.exists()) {
      targetDir.mkdirs();
    }
    a:
    if (srcFile.isDirectory()) {//源路徑是文件夾
      File[] subs = srcFile.listFiles();
      if (subs.length<=0) {
        break a;
      }
      for (File sub: subs) {
        if (sub.isDirectory() && inner) {
          list.addAll(new Img(sub.getPath(), target+File.separator+sub.getName(), true).copy());
        } else if (AbstractImgUtil.isImg(sub)) {
          list.add(copy(sub, target));
        }
      }
    } else if (AbstractImgUtil.isImg(srcFile)) {//源路徑是圖片
      list.add(copy(srcFile, target));
    }
    return list;
  }
  private File copy(File file, String target) {
    FileInputStream fis = null;
    FileOutputStream fos = null;
    File newFile = null;
    try {
      fis = new FileInputStream(file);
      newFile = new File(target + File.separator + file.getName());
      fos = new FileOutputStream(newFile);
      byte[] bs = new byte[1024*10];
      int len = -1;
      while ((len=fis.read(bs))!=-1) {
        fos.write(bs, 0, len);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        if (fis!=null) {
          fis.close();
        }
        if (fos!=null) {
          fos.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return newFile;
  }
  public Img() {}
  public Img(String src, String target) {
    this.src = src;
    this.target = target;
  }
  public Img(String src, String target, boolean inner) {
    this.src = src;
    this.target = target;
    this.inner = inner;
  }
  public String getSrc() {
    return src;
  }
  public void setSrc(String src) {
    this.src = src;
  }
  public String getTarget() {
    return target;
  }
  public void setTarget(String target) {
    this.target = target;
  }
  public boolean isInner() {
    return inner;
  }
  public void setInner(boolean inner) {
    this.inner = inner;
  }
}

附:完整實例代碼點擊此處本站下載。

更多java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java面向?qū)ο蟪绦蛟O(shè)計入門與進階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總

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

相關(guān)文章

  • java實現(xiàn)上傳網(wǎng)絡(luò)圖片到微信臨時素材

    java實現(xiàn)上傳網(wǎng)絡(luò)圖片到微信臨時素材

    這篇文章主要為大家詳細介紹了java實現(xiàn)上傳網(wǎng)絡(luò)圖片到微信臨時素材,網(wǎng)絡(luò)圖片上傳到微信服務(wù)器,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • 使用java + selenium + OpenCV破解騰訊防水墻滑動驗證碼功能

    使用java + selenium + OpenCV破解騰訊防水墻滑動驗證碼功能

    這篇文章主要介紹了使用java + selenium + OpenCV破解騰訊防水墻滑動驗證碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • Spring?Data?JPA映射自定義實體類操作

    Spring?Data?JPA映射自定義實體類操作

    這篇文章主要介紹了Spring?Data?JPA映射自定義實體類操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • 手寫一個@Valid字段校驗器的示例代碼

    手寫一個@Valid字段校驗器的示例代碼

    這篇文章主要為大家詳細介紹了如何手寫一個@Valid字段校驗器,文中的示例代碼講解詳細,對我們學(xué)習(xí)有一定幫助,需要的可以參考一下
    2022-07-07
  • 解決SpringBoot使用yaml作為配置文件遇到的坑

    解決SpringBoot使用yaml作為配置文件遇到的坑

    這篇文章主要介紹了解決SpringBoot使用yaml作為配置文件遇到的坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 詳解java8在Collection中新增加的方法removeIf

    詳解java8在Collection中新增加的方法removeIf

    這篇文章主要介紹了詳解java8在Collection中新增加的方法removeIf的相關(guān)資料,需要的朋友可以參考下
    2018-01-01
  • springboot?如何添加webapp文件夾

    springboot?如何添加webapp文件夾

    這篇文章主要介紹了springboot?如何添加webapp文件夾,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • 聊聊為什么要使用BufferedReader讀取File

    聊聊為什么要使用BufferedReader讀取File

    這篇文章主要介紹了為什么要使用BufferedReader讀取File,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Spring Boot實現(xiàn)qq郵箱驗證碼注冊和登錄驗證功能

    Spring Boot實現(xiàn)qq郵箱驗證碼注冊和登錄驗證功能

    這篇文章主要給大家介紹了關(guān)于Spring Boot實現(xiàn)qq郵箱驗證碼注冊和登錄驗證功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • Spring的@CrossOrigin注解處理請求源碼解析

    Spring的@CrossOrigin注解處理請求源碼解析

    這篇文章主要介紹了Spring的@CrossOrigin注解處理請求源碼解析,@CrossOrigin源碼解析主要分為兩個階段@CrossOrigin注釋的方法掃描注冊,請求匹配@CrossOrigin注釋的方法,本文從源碼角度進行解析,需要的朋友可以參考下
    2023-12-12

最新評論