图片下传压缩处理

图片上传压缩处理

    解决用户上传图片后,按照用户规定的尺寸大小或者按照图片比例,对图片进行压缩。

    自己试写的工具类,写的时候考虑了几个关键点:

    1、图片格式

    JAVA的API很好,com.sun.image.codec.jpeg.JPEGCodec和com.sun.image.codec.jpeg.JPEGImageEncoder 这两个类基本上自动解决了类型转换的问题,可以正常实现bmp转jpg、png转jpg、gif转jpg但是暂时还没有解决gif转gif的功能。

    2、画面质量的问题

    BufferedImage tag = new BufferedImage((int)newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB);

    // Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢

    tag.getGraphics().drawImage(img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0, null);

    3、压缩速度

    测试36MB的bmp图片(8192*6144)压缩成(160*120)的jpg的5KB图片,只需要2-3秒的时间。批量处理100张(1027*768)的bmp图像,转换成(120*80)的jpg图片总共只需要17秒。

    4、根据用户喜好选择压缩模式

    按比例或者按规定尺寸

    //compressPic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度,是否等比缩放(默认为true))

 

    以下是源码,如觉得可以更加完善的话,希望大家可以提供点意见!

Java代码
  1. /**  
  2.  *  缩略图实现,将图片(jpg、bmp、png、gif等等)真实的变成想要的大小  
  3.  */  
  4. package com.joewalker.test;   
  5.   
  6. import java.awt.Image;   
  7. import java.awt.image.BufferedImage;   
  8. import java.io.File;   
  9. import java.io.FileOutputStream;   
  10. import java.io.IOException;   
  11. import javax.imageio.ImageIO;   
  12. import com.sun.image.codec.jpeg.JPEGCodec;   
  13. import com.sun.image.codec.jpeg.JPEGImageEncoder;   
  14.   
  15. /*******************************************************************************  
  16.  * 缩略图类(通用) 本java类能将jpg、bmp、png、gif图片文件,进行等比或非等比的大小转换。 具体使用方法  
  17.  * compressPic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度,是否等比缩放(默认为true))  
  18.  */  
  19.  public class CompressPicDemo {    
  20.      private File file = null// 文件对象    
  21.      private String inputDir; // 输入图路径   
  22.      private String outputDir; // 输出图路径   
  23.      private String inputFileName; // 输入图文件名   
  24.      private String outputFileName; // 输出图文件名   
  25.      private int outputWidth = 100// 默认输出图片宽   
  26.      private int outputHeight = 100// 默认输出图片高   
  27.      private boolean proportion = true// 是否等比缩放标记(默认为等比缩放)   
  28.      public CompressPicDemo() { // 初始化变量   
  29.          inputDir = "";    
  30.          outputDir = "";    
  31.          inputFileName = "";    
  32.          outputFileName = "";    
  33.          outputWidth = 100;    
  34.          outputHeight = 100;    
  35.      }    
  36.      public void setInputDir(String inputDir) {    
  37.          this.inputDir = inputDir;    
  38.      }    
  39.      public void setOutputDir(String outputDir) {    
  40.          this.outputDir = outputDir;    
  41.      }    
  42.      public void setInputFileName(String inputFileName) {    
  43.          this.inputFileName = inputFileName;   
  44.      }    
  45.      public void setOutputFileName(String outputFileName) {    
  46.          this.outputFileName = outputFileName;    
  47.      }    
  48.      public void setOutputWidth(int outputWidth) {   
  49.          this.outputWidth = outputWidth;    
  50.      }    
  51.      public void setOutputHeight(int outputHeight) {    
  52.          this.outputHeight = outputHeight;    
  53.      }    
  54.      public void setWidthAndHeight(int width, int height) {    
  55.          this.outputWidth = width;   
  56.          this.outputHeight = height;    
  57.      }    
  58.         
  59.      /*   
  60.       * 获得图片大小   
  61.       * 传入参数 String path :图片路径   
  62.       */    
  63.      public long getPicSize(String path) {    
  64.          file = new File(path);    
  65.          return file.length();    
  66.      }   
  67.         
  68.      // 图片处理    
  69.      public String compressPic() {    
  70.          try {    
  71.              //获得源文件    
  72.              file = new File(inputDir + inputFileName);    
  73.              if (!file.exists()) {    
  74.                  return "";    
  75.              }    
  76.              Image img = ImageIO.read(file);    
  77.              // 判断图片格式是否正确    
  78.              if (img.getWidth(null) == -1) {   
  79.                  System.out.println(" can't read,retry!" + "<BR>");    
  80.                  return "no";    
  81.              } else {    
  82.                  int newWidth; int newHeight;    
  83.                  // 判断是否是等比缩放    
  84.                  if (this.proportion == true) {    
  85.                      // 为等比缩放计算输出的图片宽度及高度    
  86.                      double rate1 = ((double) img.getWidth(null)) / (double) outputWidth + 0.1;    
  87.                      double rate2 = ((double) img.getHeight(null)) / (double) outputHeight + 0.1;    
  88.                      // 根据缩放比率大的进行缩放控制    
  89.                      double rate = rate1 > rate2 ? rate1 : rate2;    
  90.                      newWidth = (int) (((double) img.getWidth(null)) / rate);    
  91.                      newHeight = (int) (((double) img.getHeight(null)) / rate);    
  92.                  } else {    
  93.                      newWidth = outputWidth; // 输出的图片宽度    
  94.                      newHeight = outputHeight; // 输出的图片高度    
  95.                  }    
  96.                 BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB);    
  97.                    
  98.                 /*  
  99.                  * Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的  
  100.                  * 优先级比速度高 生成的图片质量比较好 但速度慢  
  101.                  */    
  102.                 tag.getGraphics().drawImage(img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 00null);   
  103.                 FileOutputStream out = new FileOutputStream(outputDir + outputFileName);   
  104.                 // JPEGImageEncoder可适用于其他图片类型的转换    
  105.                 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);    
  106.                 encoder.encode(tag);    
  107.                 out.close();    
  108.              }    
  109.          } catch (IOException ex) {    
  110.              ex.printStackTrace();    
  111.          }    
  112.          return "ok";    
  113.     }    
  114.     public String compressPic (String inputDir, String outputDir, String inputFileName, String outputFileName) {    
  115.         // 输入图路径    
  116.         this.inputDir = inputDir;    
  117.         // 输出图路径    
  118.         this.outputDir = outputDir;    
  119.         // 输入图文件名    
  120.         this.inputFileName = inputFileName;    
  121.         // 输出图文件名   
  122.         this.outputFileName = outputFileName;    
  123.         return compressPic();    
  124.     }    
  125.     public String compressPic(String inputDir, String outputDir, String inputFileName, String outputFileName, int width, int height, boolean gp) {    
  126.         // 输入图路径    
  127.         this.inputDir = inputDir;    
  128.         // 输出图路径    
  129.         this.outputDir = outputDir;    
  130.         // 输入图文件名    
  131.         this.inputFileName = inputFileName;    
  132.         // 输出图文件名    
  133.         this.outputFileName = outputFileName;    
  134.         // 设置图片长宽   
  135.         setWidthAndHeight(width, height);    
  136.         // 是否是等比缩放 标记    
  137.         this.proportion = gp;    
  138.         return compressPic();    
  139.     }    
  140.        
  141.     // main测试    
  142.     // compressPic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度,是否等比缩放(默认为true))   
  143.     public static void main(String[] arg) {    
  144.         CompressPicDemo mypic = new CompressPicDemo();    
  145.         System.out.println("输入的图片大小:" + mypic.getPicSize("e:\\1.jpg")/1024 + "KB");    
  146.         int count = 0// 记录全部图片压缩所用时间   
  147.         for (int i = 0; i < 100; i++) {    
  148.             int start = (int) System.currentTimeMillis();   // 开始时间    
  149.             mypic.compressPic("e:\\", "e:\\test\\", "1.jpg", "r1"+i+".jpg", 120120true);    
  150.             int end = (int) System.currentTimeMillis(); // 结束时间    
  151.             int re = end-start; // 但图片生成处理时间    
  152.             count += re; System.out.println("第" + (i+1) + "张图片压缩处理使用了: " + re + "毫秒");    
  153.             System.out.println("输出的图片大小:" + mypic.getPicSize("e:\\test\\r1"+i+".jpg")/1024 + "KB");    
  154.         }   
  155.         System.out.println("总共用了:" + count + "毫秒");    
  156.     }    
  157.  }