一、前提说明
业务要求需要将图片在上传服务器之前进行一波压缩。要求:图片保持原尺寸,所占内存压缩
注:网上各种类似的文章基本都是重复的,资源少并且很多文章的内容根本无法实现对应说明的效果。自己从开发开始到现在也一直是在面向百度编程,本着饮水思源的心理,记一下这个坑,以及自己的处理方式。
二、目前能做到压缩的几种方式
1.Thumbnailator (推荐)
1.1简介
Thumbnailator 是一个用来生成图像缩略图的 Java 类库,通过很简单的代码即可生成图片缩略图,也可直接对一整个目录的图片生成缩略图。
支持:图片缩放,区域裁剪,水印,旋转,保持比例,图片压缩。
Thumbnailator官网:code.google.com/p/thumbnail…
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>[0.4, 0.5)</version>
</dependency>
复制代码
目前官网上发布的最新版本为0.4.11 (记录当前时间:2020/07/22)
Thumbnailator文档地址:coobird.github.io/thumbnailat…
1.2 使用
public class ImageUtils {
/**
* 根据指定大小压缩图片
*
* @param imageBytes 源图片字节数组
* @param desFileSize 指定图片大小,单位kb
* @param imageId 影像编号
* @return 压缩质量后的图片字节数组
*/
public static byte[] compressPicForScale(
byte[] imageBytes, long desFileSize, String imageId, Double quality) {
if (imageBytes == null || imageBytes.length <= 0 || imageBytes.length < desFileSize * 1024) {
return imageBytes;
}
long srcSize = imageBytes.length;
try {
ByteArrayInputStream inputStream = new ByteArrayInputStream(imageBytes);
ByteArrayOutputStream outputStream= new ByteArrayOutputStream(imageBytes.length);
Thumbnails.of(inputStream).scale(1f).outputQuality(quality).toOutputStream(outputStream);
imageBytes = outputStream.toByteArray();
log.info(
"【图片压缩】imageId={} | 图片原大小={}kb | 压缩后大小={}kb",
imageId,
srcSize / 1024,
imageBytes.length / 1024);
} catch (Exception e) {
log.error("【图片压缩】msg=图片压缩失败!", e);
}
return imageBytes;
}
}
复制代码
注意:
1.scale()为尺寸压缩,与这个方法类似的是size()方法,这两个都是指定图片尺寸的,不同的是,scale是按照比例来处理的图片尺寸,而size()是直接指定图片的width和height。outputQuality指定的是图片质量。
2.实测证明不管哪种压缩,都对JPG格式的支持力度是最好的。所以没有格式要求的情况下,请转为jpg格式。尤其是png格式的 请必转!
3.上述方法,在输出图片时,最终图片会比日志中打印的大小大30%左右,byte[],猜测是压缩后的空间碎片导致的。
1.3 深坑说明
重点!!!!!
不要做循环调用这种傻事!!!质量压缩是有固定算法的,大小固定的情况下不存在outputQuality(0.9)的时候,下一次循环质量就变成0.81了!!! 循环只会导致一个结果:亲,死循环了~
举个栗子: 循环调用导致死循环,也是在一个博客中摘得。排坑所得...
2.java原生API (挺坑的,质量系数设置小了,大小反而会变大。大家看看就好)
2.1 上代码
public static boolean compressPic(String srcFilePath, String descFilePath) throws IOException {
File file = null;
BufferedImage src = null;
FileOutputStream out = null;
ImageWriter imgWrier;
ImageWriteParam imgWriteParams;
// 指定写图片的方式为 jpg
imgWrier = ImageIO.getImageWritersByFormatName("jpg").next();
imgWriteParams = new javax.imageio.plugins.jpeg.JPEGImageWriteParam(
null);
// 要使用压缩,必须指定压缩方式为MODE_EXPLICIT
imgWriteParams.setCompressionMode(imgWriteParams.MODE_EXPLICIT);
// 这里指定压缩的程度,参数qality是取值0~1范围内,
imgWriteParams.setCompressionQuality((float)1);
imgWriteParams.setProgressiveMode(imgWriteParams.MODE_DISABLED);
ColorModel colorModel = ImageIO.read(new File(srcFilePath)).getColorModel();// ColorModel.getRGBdefault();
// 指定压缩时使用的色彩模式
// imgWriteParams.setDestinationType(new javax.imageio.ImageTypeSpecifier(
// colorModel, colorModel.createCompatibleSampleModel(16, 16)));
imgWriteParams.setDestinationType(new javax.imageio.ImageTypeSpecifier(
colorModel, colorModel.createCompatibleSampleModel(16, 16)));
try {
if (isBlank(srcFilePath)) {
return false;
} else {
file = new File(srcFilePath);
System.out.println(file.length());
src = ImageIO.read(file);
out = new FileOutputStream(descFilePath);
imgWrier.reset();
// 必须先指定 out值,才能调用write方法, ImageOutputStream可以通过任何
// OutputStream构造
imgWrier.setOutput(ImageIO.createImageOutputStream(out));
// 调用write方法,就可以向输入流写图片
imgWrier.write(null, new IIOImage(src, null, null),
imgWriteParams);
out.flush();
out.close();
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public static boolean isBlank(String string) {
if (string == null || string.length() == 0 || string.trim().equals("")) {
return true;
}
return false;
}
复制代码
3.总结
1.其实应该不会有巨神坑的产品,循环压缩到固定大小以下的需求,不是扯淡是啥啊。
分析一下啊,比如一张5M的照片,格式相同的情况下压到500k,这图还能看吗?? 压缩图片无非就是为了效率,个人测试的时候压缩系数在0.5的话,图片不会出现明显的失真,一旦比这个低了就相当危险了。这种情况下产品觉得还不行的话,那估计就只能开战了,哈哈。
2. 结尾:png转jpg的时候,可能会出现图片颜色失真的情况。这个可以用画图工具构建一张原图大小的空白画卷,然后将想要转换的图画进去。原理不了解~有点玄学。