当前位置: 移动技术网 > IT编程>开发语言>Java > Java基于装饰者模式实现的图片工具类实例【附demo源码下载】

Java基于装饰者模式实现的图片工具类实例【附demo源码下载】

2019年07月19日  | 移动技术网IT编程  | 我要评论

本文实例讲述了java基于装饰者模式实现的图片工具类。分享给大家供大家参考,具体如下:

imgutil.java:

/*
 * 装饰者模式实现图片处理工具类
 * 类似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;     //水印图片距离目标图片左侧的偏移量, 如果x<0, 则在正中间
  private  integer    y;     //水印图片距离目标图片上侧的偏移量, 如果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); // 水印文件结束
      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方向重复次数
      int rpt_y = height>height_1?(int)math.ceil(double.valueof(height)/height_1):1;//y方向重复次数
      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);
        }
      }// 水印文件结束
      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-仅当前文件夹
  @override
  public list<file> dispose() {
    return copy();
  }
  /** 复制 - 被装饰者初始状态 */
  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相关内容感兴趣的读者可查看本站专题:《java面向对象程序设计入门与进阶教程》、《java数据结构与算法教程》、《java操作dom节点技巧总结》、《java文件与目录操作技巧汇总》和《java缓存操作技巧汇总

希望本文所述对大家java程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网