天天看點

java圖檔壓縮--Thumbnailator

最近在java開發中遇到了圖檔處理的問題,我使用的是Thumbnailator。Thumbnailator是一個優秀的圖檔處理的開源java類庫,使用起來很友善。

圖檔原圖:

java圖檔壓縮--Thumbnailator

1、指定大小進行縮放

public class PicUtil {
	public static void main(String[] args) {  
		  
        PicUtil.commpressPicForSize("G:\\images\\ceshi.jpg",  
                "G:\\images\\datas\\ceshi.jpg", 100, 0.3); // 圖檔小于100kb
        
    }  
  
    /** 
     * 根據指定大小和指定精度壓縮圖檔 
     *  
     * @param srcPath 
     *            源圖檔位址 
     * @param desPath 
     *            目标圖檔位址 
     * @param desFilesize 
     *            指定圖檔大小,機關kb 
     * @param accuracy 
     *            精度,遞歸壓縮的比率,建議小于0.9 
     * @return 
     */  
    public static String commpressPicForSize(String srcPath, String desPath,  
            long desFileSize, double accuracy) {  
        if (StringUtils.isEmpty(srcPath) || StringUtils.isEmpty(srcPath)) {  
            return null;  
        }  
        if (!new File(srcPath).exists()) {  
            return null;  
        }  
        try {  
            File srcFile = new File(srcPath);  
            long srcFileSize = srcFile.length();  
            System.out.println("源圖檔:" + srcPath + ",大小:" + srcFileSize / 1024  
                    + "kb");  
  
            // 1、先轉換成jpg  
            Thumbnails.of(srcPath).scale(1f).toFile(desPath);  
            // 遞歸壓縮,直到目标檔案大小小于desFileSize  
            commpressPicCycle(desPath, desFileSize, accuracy);
  
            File desFile = new File(desPath);  
            System.out.println("目标圖檔:" + desPath + ",大小" + desFile.length()  
                    / 1024 + "kb");  
            System.out.println("圖檔壓縮完成!");  
        } catch (Exception e) {  
            e.printStackTrace();  
            return null;  
        }  
        return desPath;  
    }
    /**
     * 圖檔壓縮:按指定大小把圖檔進行縮放(會遵循原圖高寬比例)
     * 并設定圖檔檔案大小
     */
    private static void commpressPicCycle(String desPath, long desFileSize,  
            double accuracy) throws IOException {  
        File srcFileJPG = new File(desPath);  
        long srcFileSizeJPG = srcFileJPG.length();  
        // 2、判斷大小,如果小于指定大小,不壓縮;如果大于等于指定大小,壓縮  
        if (srcFileSizeJPG <= desFileSize * 1024) {
            return;  
        }  
        // 計算寬高  
        BufferedImage bim = ImageIO.read(srcFileJPG);  
        int srcWdith = bim.getWidth();  
        int srcHeigth = bim.getHeight();  
        int desWidth = new BigDecimal(srcWdith).multiply(  
                new BigDecimal(accuracy)).intValue();  
        int desHeight = new BigDecimal(srcHeigth).multiply(  
                new BigDecimal(accuracy)).intValue();  
  
        Thumbnails.of(desPath).size(desWidth, desHeight)  
                .outputQuality(accuracy).toFile(desPath);  
        commpressPicCycle(desPath, desFileSize, accuracy);  
    }
}
           

壓縮後圖檔展示:

java圖檔壓縮--Thumbnailator

運作結果列印:

java圖檔壓縮--Thumbnailator

2、按照比例進行縮放

public class PicUtil {
	public static void main(String[] args) {
            PicUtil.commpressPicForScale("G:\\images\\ceshi.jpg", 
        		"G:\\images\\scales\\ceshi.jpg", 100,0.3);
        }
        public static String commpressPicForScale(String srcPath, String desPath,  
            long desFileSize, double accuracy) {  
           if (StringUtils.isEmpty(srcPath) || StringUtils.isEmpty(srcPath)) {  
               return null;  
             }  
           if (!new File(srcPath).exists()) {  
               return null;  
           }  
           try {  
              File srcFile = new File(srcPath);  
              long srcFileSize = srcFile.length();  
              System.out.println("源圖檔:" + srcPath + ",大小:" + srcFileSize / 1024  
                    + "kb");  
  
              // 1、先轉換成jpg  
              Thumbnails.of(srcPath).scale(1f).toFile(desPath);  
              //按照比例進行縮放
              imgScale(desPath, desFileSize, accuracy);
  
              File desFile = new File(desPath);  
              System.out.println("目标圖檔:" + desPath + ",大小" + desFile.length()  
                    / 1024 + "kb");  
              System.out.println("圖檔壓縮完成!");  
          } catch (Exception e) {  
              e.printStackTrace();  
              return null;  
          }  
          return desPath;  
     }
   	/**
     	* 按照比例進行縮放 
     	* 
     	*/
      private static void imgScale(String desPath, long desFileSize,  
            double accuracy) throws IOException{
    	  File file=new File(desPath);
    	  long fileSize=file.length();
    	  //判斷大小,如果小于指定大小,不壓縮;如果大于等于指定大小,壓縮
    	  if(fileSize<=desFileSize*1024){
    		return;
    	  }
    	  //按照比例進行縮小
          Thumbnails.of(desPath).scale(accuracy).toFile(desPath);//按比例縮小
          System.out.println("按照比例進行縮放");
          imgScale(desPath, desFileSize, accuracy);
    }
}
           

圖檔壓縮效果:

java圖檔壓縮--Thumbnailator

運作結果列印:

java圖檔壓縮--Thumbnailator

3、圖檔尺寸不變,大小改變

public class PicUtil {
	public static void main(String[] args) {  
		PicUtil.commpressPicForScaleSize("G:\\images\\ceshi.jpg", 
        		"G:\\images\\scaleSize\\ceshi.jpg", 100,0.25);
    }
public static String commpressPicForScaleSize(String srcPath, String desPath,  
            long desFileSize, double accuracy) {  
        if (StringUtils.isEmpty(srcPath) || StringUtils.isEmpty(srcPath)) {  
            return null;  
        }  
        if (!new File(srcPath).exists()) {  
            return null;  
        }  
        try {  
            File srcFile = new File(srcPath);  
            long srcFileSize = srcFile.length();  
            System.out.println("源圖檔:" + srcPath + ",大小:" + srcFileSize / 1024  
                    + "kb");  
  
            // 1、先轉換成jpg  
            Thumbnails.of(srcPath).scale(1f).toFile(desPath);  
            //按照比例進行縮放
            imgScaleSize(desPath, desFileSize, accuracy);
  
            File desFile = new File(desPath);  
            System.out.println("目标圖檔:" + desPath + ",大小" + desFile.length()  
                    / 1024 + "kb");  
            System.out.println("圖檔壓縮完成!");  
        } catch (Exception e) {  
            e.printStackTrace();  
            return null;  
        }  
        return desPath;  
    }
    /**
     * 圖檔尺寸不變,壓縮檔案大小
     */
    private static void imgScaleSize(String desPath, long desFileSize,  
    		double accuracy) throws IOException{
    	File fileName=new File(desPath);
    	long fileNameSize=fileName.length();
    	//判斷大小,如果小于指定大小,不壓縮;如果大于等于指定大小,壓縮
    	if(fileNameSize<=desFileSize*1024){
    		return;
    	}
    	//圖檔尺寸不變,壓縮圖檔檔案大小
    	//圖檔尺寸不變,壓縮圖檔檔案大小outputQuality實作,參數1為最高品質
        Thumbnails.of(desPath).scale(1f).outputQuality(accuracy).toFile(desPath);
        System.out.println("圖檔尺寸不變,壓縮檔案大小");
        imgScaleSize(desPath, desFileSize, accuracy);
    }
}
           

圖檔壓縮效果展示:

java圖檔壓縮--Thumbnailator

運作結果列印:

java圖檔壓縮--Thumbnailator

參考文章連結:http://blog.csdn.net/u010355502/article/details/77197616

繼續閱讀