壓縮圖片 java

1.定義壓縮圖片工具類算法

public class CompressPicUtil {
      private File file = null; // 文件對象 
      private String inputDir; // 輸入圖路徑
      private String outputDir; // 輸出圖路徑
      private String inputFileName; // 輸入圖文件名
      private String outputFileName; // 輸出圖文件名
      private int outputWidth = 0; // 默認輸出圖片寬
      private int outputHeight = 0; // 默認輸出圖片高
      private boolean proportion = true; // 是否等比縮放標記(默認爲等比縮放)
      public CompressPicUtil() { // 初始化變量
              inputDir = ""; 
              outputDir = ""; 
              inputFileName = ""; 
              outputFileName = ""; 
              outputWidth = 0; 
              outputHeight = 0; 
      } 
      public void setInputDir(String inputDir) { 
              this.inputDir = inputDir; 
      } 
      public void setOutputDir(String outputDir) { 
              this.outputDir = outputDir; 
      } 
      public void setInputFileName(String inputFileName) { 
              this.inputFileName = inputFileName;
      } 
      public void setOutputFileName(String outputFileName) { 
              this.outputFileName = outputFileName; 
      } 
      public void setOutputWidth(int outputWidth) {
              this.outputWidth = outputWidth; 
      } 
      public void setOutputHeight(int outputHeight) { 
              this.outputHeight = outputHeight; 
      } 
      public void setWidthAndHeight(int width, int height) { 
              this.outputWidth = width;
              this.outputHeight = height; 
      } 
      
      /* 
       * 得到圖片大小 
       * 傳入參數 String path :圖片路徑 
       */ 
      public long getPicSize(String path) { 
              file = new File(path); 
              return file.length(); 
      }
      
      // 圖片處理 
      public String compressPic() { 
              try { 
                      //得到源文件 
                      file = new File(inputDir + inputFileName); 
                      if (!file.exists()) { 
                              return ""; 
                      } 
                      Image img = ImageIO.read(file); 
                      // 判斷圖片格式是否正確 
                      if (img.getWidth(null) == -1) {
                              System.out.println(" can't read,retry!" + "<BR>");
                              return "no"; 
                      } else { 
                              int newWidth; int newHeight; 
                              // 判斷是不是等比縮放 
                              if (this.proportion == true) { 
                                      // 爲等比縮放計算輸出的圖片寬度及高度 
                                      double rate1 = ((double) img.getWidth(null)) / (double) outputWidth + 0.1;
                                      double rate2 = ((double) img.getHeight(null)) / (double) outputHeight + 0.1;
                                      // 根據縮放比率大的進行縮放控制 
                                      double rate = rate1 > rate2 ? rate1 : rate2;
                                      newWidth = (int) (((double) img.getWidth(null)) / rate);
                                      newHeight = (int) (((double) img.getHeight(null)) / rate);
                              } else { 
                                      newWidth = img.getWidth(null); // 輸出的圖片寬度
                                      newHeight = img.getHeight(null); // 輸出的圖片高度
                              } 
                              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);
                              FileOutputStream out = new FileOutputStream(outputDir + outputFileName);
                              // JPEGImageEncoder可適用於其餘圖片類型的轉換 
                              JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                              encoder.encode(tag); 
                              out.close(); 
                      } 
              } catch (IOException ex) { 
                      ex.printStackTrace(); 
              } 
              return "ok"; 
     } 
      
      public String compressPic (String inputDir, String outputDir, String inputFileName, String outputFileName) {
          // 輸入圖路徑 
          this.inputDir = inputDir; 
          // 輸出圖路徑 
          this.outputDir = outputDir; 
          // 輸入圖文件名 
          this.inputFileName = inputFileName; 
          // 輸出圖文件名
          this.outputFileName = outputFileName; 
          return compressPic(); 
  } 
      public String compressPic(String inputDir, String outputDir, String inputFileName, String outputFileName, int width, int height, boolean gp) {
          // 輸入圖路徑 
          this.inputDir = inputDir; 
          // 輸出圖路徑 
          this.outputDir = outputDir; 
          // 輸入圖文件名 
          this.inputFileName = inputFileName; 
          // 輸出圖文件名 
          this.outputFileName = outputFileName; 
          // 設置圖片長寬
          setWidthAndHeight(width, height); 
          // 是不是等比縮放 標記 
          this.proportion = gp; 
          return compressPic(); 
  } 
      
  

2.方法調用
 // main測試 
      // compressPic(大圖片路徑,生成小圖片路徑,大圖片文件名,生成小圖片文名,生成小圖片寬度,生成小圖片高度,是否等比縮放(默認爲true))
      public static void main(String[] arg) { 
          CompressPicUtil mypic = new CompressPicUtil(); 
          System.out.println("輸入的圖片大小:" + mypic.getPicSize("D:\\upload\\20150710021601333618.png")/1024 + "KB");
         mypic.compressPic("D:\\upload\\", "D:\\upload\\", "20150710021601333618.png", "r1.png", 120, 120, true);

  } 
相關文章
相關標籤/搜索