圖片 壓縮

原文http://cuisuqiang.iteye.com/blog/2045855

整理文檔,搜刮出一個Java做圖片壓縮的代碼,稍微整理精簡一下做下分享。
首先,要壓縮的圖片格式不能說動態圖片,你可以使用bmp、png、gif等,至於壓縮質量,可以通過BufferedImage來指定。
在C盤的temp下放置一張圖片pic123.jpg,儘量找一個像素高一點的圖片,這裏我找了一張5616*3744的。

 Java代碼  收藏代碼

  1. package test;  
  2. import java.io.*;  
  3. import java.util.Date;  
  4. import java.awt.*;  
  5. import java.awt.image.*;  
  6. import javax.imageio.ImageIO;  
  7. import com.sun.image.codec.jpeg.*;  
  8. /** 
  9.  * 圖片壓縮處理 
  10.  * @author 崔素強 
  11.  */  
  12. public class ImgCompress {  
  13.     private Image img;  
  14.     private int width;  
  15.     private int height;  
  16.     @SuppressWarnings("deprecation")  
  17.     public static void main(String[] args) throws Exception {  
  18.         System.out.println("開始:" + new Date().toLocaleString());  
  19.         ImgCompress imgCom = new ImgCompress("C:\\temp\\pic123.jpg");  
  20.         imgCom.resizeFix(400400);  
  21.         System.out.println("結束:" + new Date().toLocaleString());  
  22.     }  
  23.     /** 
  24.      * 構造函數 
  25.      */  
  26.     public ImgCompress(String fileName) throws IOException {  
  27.         File file = new File(fileName);// 讀入文件  
  28.         img = ImageIO.read(file);      // 構造Image對象  
  29.         width = img.getWidth(null);    // 得到源圖寬  
  30.         height = img.getHeight(null);  // 得到源圖長  
  31.     }  
  32.     /** 
  33.      * 按照寬度還是高度進行壓縮 
  34.      * @param w int 最大寬度 
  35.      * @param h int 最大高度 
  36.      */  
  37.     public void resizeFix(int w, int h) throws IOException {  
  38.         if (width / height > w / h) {  
  39.             resizeByWidth(w);  
  40.         } else {  
  41.             resizeByHeight(h);  
  42.         }  
  43.     }  
  44.     /** 
  45.      * 以寬度爲基準,等比例放縮圖片 
  46.      * @param w int 新寬度 
  47.      */  
  48.     public void resizeByWidth(int w) throws IOException {  
  49.         int h = (int) (height * w / width);  
  50.         resize(w, h);  
  51.     }  
  52.     /** 
  53.      * 以高度爲基準,等比例縮放圖片 
  54.      * @param h int 新高度 
  55.      */  
  56.     public void resizeByHeight(int h) throws IOException {  
  57.         int w = (int) (width * h / height);  
  58.         resize(w, h);  
  59.     }  
  60.     /** 
  61.      * 強制壓縮/放大圖片到固定的大小 
  62.      * @param w int 新寬度 
  63.      * @param h int 新高度 
  64.      */  
  65.     public void resize(int w, int h) throws IOException {  
  66.         // SCALE_SMOOTH 的縮略算法 生成縮略圖片的平滑度的 優先級比速度高 生成的圖片質量比較好 但速度慢  
  67.         BufferedImage image = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB );   
  68.         image.getGraphics().drawImage(img, 00, w, h, null); // 繪製縮小後的圖  
  69.         File destFile = new File("C:\\temp\\456.jpg");  
  70.         FileOutputStream out = new FileOutputStream(destFile); // 輸出到文件流  
  71.         // 可以正常實現bmp、png、gif轉jpg  
  72.         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);  
  73.         encoder.encode(image); // JPEG編碼  
  74.         out.close();  
  75.     }  
  76. }  

 

運行後在C盤temp下生成一個465.jpg,像素大小爲600*400,像素大小是我指定的。用時也就是一兩秒的事情,注意,我這張圖片是10M的,壓縮後是40.5KB
一些細節事項可以參考代碼中的註釋。

 

要注意的是,你可能想試一試較大圖片的處理能力,如果你的JDK沒有指定默認內存,那可能會有如下異常,因爲內存不夠大:

Java代碼  收藏代碼
  1. 開始:2014-4-14 16:25:11  
  2. Exception in thread "main" java.lang.OutOfMemoryError: Java heap space  
  3.     at java.awt.image.DataBufferByte.<init>(DataBufferByte.java:58)  
  4.     at java.awt.image.ComponentSampleModel.createDataBuffer(ComponentSampleModel.java:397)  
  5.     at java.awt.image.Raster.createWritableRaster(Raster.java:938)  
  6.     at javax.imageio.ImageTypeSpecifier.createBufferedImage(ImageTypeSpecifier.java:1169)  
  7.     at javax.imageio.ImageReader.getDestination(ImageReader.java:2879)  
  8.     at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:943)  
  9.     at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:915)  
  10.     at javax.imageio.ImageIO.read(ImageIO.java:1422)  
  11.     at javax.imageio.ImageIO.read(ImageIO.java:1282)  
  12.     at test.ImgCompress.<init>(ImgCompress.java:31)  
  13.     at test.ImgCompress.main(ImgCompress.java:21)  

 

解決方法:
在Eclipse裏選:Window->Preference->Installed JREs->Edit(選中jre),
在Default VM Arguments裏輸入-Xms256m -Xmx1024m,表示最小堆內存256M,最大堆1G,然後運行就可以了


發佈了42 篇原創文章 · 獲贊 6 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章