<!-- 图片处理 -->
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
package com.startx.http.wordfilter.utils;
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.lang3.StringUtils;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
@Slf4j
public class PicUtil {
public static void main(String[] args) {
commpressPicForSize("d:\\file\\image\\20210107\\a.png",
"d:\\file\\image\\20210107\\d.jpg", 900, 0.36); // 图片小于1024kb
}
/**
* 根据指定大小和指定精度压缩图片
*
* @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();
log.debug("源图片:" + srcPath + ",大小:" + srcFileSize / 1024
+ "kb");
// 1、先转换成jpg
Thumbnails.of(srcPath).scale(1f).toFile(desPath);
// 递归压缩,直到目标文件大小小于desFileSize
// //按照比例进行缩放
File desFile = new File(desPath);
long fileNameSize = desFile.length();
if (fileNameSize > desFileSize * 1024) {
imgScaleSize(desPath, fileNameSize, desFileSize, accuracy, 1);
}
log.debug("目标图片:" + desPath + ",大小" + desFile.length()
/ 1024 + "kb");
log.debug("图片压缩完成!");
} catch (Exception e) {
e.printStackTrace();
return null;
}
return desPath;
}
/**
* 图片压缩:按指定大小把图片进行缩放(会遵循原图高宽比例)
* 并设置图片文件大小
*/
public 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);
}
/**
* 图片尺寸不变,压缩文件大小
*/
public static void imgScaleSize(String desPath, long fileSize, long desFileSize, double accuracy, int index) throws IOException {
File fileName = new File(desPath);
long fileNameSize = fileName.length();
//判断大小,如果小于指定大小,不压缩;如果大于等于指定大小,压缩
long size = desFileSize * 1024;
if (fileNameSize <= size) {
return;
}
if (index++ > 2) {
if (fileSize == fileNameSize) {
return;
}
}
//图片尺寸不变,压缩图片文件大小
//图片尺寸不变,压缩图片文件大小outputQuality实现,参数1为最高质量
Thumbnails.of(desPath).scale(1f).outputQuality(accuracy).toFile(desPath);
log.debug("图片尺寸不变,压缩文件大小");
accuracy -= 0.1;
if (accuracy < 0) {
accuracy = 0.01;
}
imgScaleSize(desPath, fileNameSize, desFileSize, accuracy, index);
}
/**
* 按照比例进行缩放
*/
public 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);//按比例缩小
log.debug("按照比例进行缩放");
imgScale(desPath, desFileSize, accuracy);
}
}