通过google插件Thumbnails实现图片指定大小压缩

来源:互联网 发布:中国金融软件 编辑:程序博客网 时间:2024/06/05 06:56

1、由于商户进件时,上游对图片大小有要求(500kb以下),而我们平台图片过大(10M以上),所以必须通过程序将图片压缩后再上传;

2、java api可以通过ImageIO实现图片压缩,但效果不好,图片压缩后出现变红现象,故舍弃;

3、测试谷歌Thumbnails插件后,觉得还不错,故选用该插件来实现;

4、谷歌插件固然好,能指定不同的参数进行压缩,例如:宽高(size)、缩放(scale)、旋转()、指定质量比(outputQuality),但不能指定图片占存大小进行压缩(如果可以,请留言告知我,感激不尽!),故自己实现了此功能;

代码功能:

1、指定源文件路径、目标文件路径、最大图片大小(单位kb)、递归压缩的比率(0-1之间,建议0.8),如果测试出现java OutOfMemoryError,大多是递归压缩比例设置有问题;

2、可以实现图片格式之间的互转,只需在源文件和目标文件路径指定即可;

3、通过测试可知,png转jpg图片占存大小变小,jgp转png图片占存大小变大;

引用的jar包:

thumbnailator-0.4.8.jar

代码如下:

[java] view plain copy
  1. import java.awt.image.BufferedImage;  
  2. import java.io.File;  
  3. import java.io.IOException;  
  4. import java.math.BigDecimal;  
  5.   
  6. import javax.imageio.ImageIO;  
  7.   
  8. import net.coobird.thumbnailator.Thumbnails;  
  9.   
  10. import org.apache.commons.lang3.StringUtils;  
  11.   
  12. public class PicUtils {  
  13.   
  14.     public static void main(String[] args) {  
  15.   
  16.         PicUtils.commpressPicForScale("C:\\Users\\123\\Desktop\\1.png",  
  17.                 "C:\\Users\\123\\Desktop\\12.jpg"5000.8); // 图片小于500kb  
  18.     }  
  19.   
  20.     /** 
  21.      * 根据指定大小和指定精度压缩图片 
  22.      *  
  23.      * @param srcPath 
  24.      *            源图片地址 
  25.      * @param desPath 
  26.      *            目标图片地址 
  27.      * @param desFilesize 
  28.      *            指定图片大小,单位kb 
  29.      * @param accuracy 
  30.      *            精度,递归压缩的比率,建议小于0.9 
  31.      * @return 
  32.      */  
  33.     public static String commpressPicForScale(String srcPath, String desPath,  
  34.             long desFileSize, double accuracy) {  
  35.         if (StringUtils.isEmpty(srcPath) || StringUtils.isEmpty(srcPath)) {  
  36.             return null;  
  37.         }  
  38.         if (!new File(srcPath).exists()) {  
  39.             return null;  
  40.         }  
  41.         try {  
  42.             File srcFile = new File(srcPath);  
  43.             long srcFileSize = srcFile.length();  
  44.             System.out.println("源图片:" + srcPath + ",大小:" + srcFileSize / 1024  
  45.                     + "kb");  
  46.   
  47.             // 1、先转换成jpg  
  48.             Thumbnails.of(srcPath).scale(1f).toFile(desPath);  
  49.             // 递归压缩,直到目标文件大小小于desFileSize  
  50.             commpressPicCycle(desPath, desFileSize, accuracy);  
  51.   
  52.             File desFile = new File(desPath);  
  53.             System.out.println("目标图片:" + desPath + ",大小" + desFile.length()  
  54.                     / 1024 + "kb");  
  55.             System.out.println("图片压缩完成!");  
  56.         } catch (Exception e) {  
  57.             e.printStackTrace();  
  58.             return null;  
  59.         }  
  60.         return desPath;  
  61.     }  
  62.   
  63.     private static void commpressPicCycle(String desPath, long desFileSize,  
  64.             double accuracy) throws IOException {  
  65.         File srcFileJPG = new File(desPath);  
  66.         long srcFileSizeJPG = srcFileJPG.length();  
  67.         // 2、判断大小,如果小于500kb,不压缩;如果大于等于500kb,压缩  
  68.         if (srcFileSizeJPG <= desFileSize * 1024) {  
  69.             return;  
  70.         }  
  71.         // 计算宽高  
  72.         BufferedImage bim = ImageIO.read(srcFileJPG);  
  73.         int srcWdith = bim.getWidth();  
  74.         int srcHeigth = bim.getHeight();  
  75.         int desWidth = new BigDecimal(srcWdith).multiply(  
  76.                 new BigDecimal(accuracy)).intValue();  
  77.         int desHeight = new BigDecimal(srcHeigth).multiply(  
  78.                 new BigDecimal(accuracy)).intValue();  
  79.   
  80.         Thumbnails.of(desPath).size(desWidth, desHeight)  
  81.                 .outputQuality(accuracy).toFile(desPath);  
  82.         commpressPicCycle(desPath, desFileSize, accuracy);  
  83.     }  
  84.   
  85. }