本文用到了一个谷歌的图片处理插件 thumbnailator
<!-- 图片处理 -->
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
package com.zl.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.coobird.thumbnailator.Thumbnails;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
public class PicUtil {
private static Logger logger = LoggerFactory.getLogger(PicUtil.class);
public static byte[] compressPicForScale(byte[] imageBytes, long desFileSize) {
if (imageBytes == null || imageBytes.length <= 0 || imageBytes.length < desFileSize * 1024) {
return imageBytes;
}
long srcSize = imageBytes.length;
double accuracy = getAccuracy(srcSize / 1024);
try {
while (imageBytes.length > desFileSize * 1024) {
ByteArrayInputStream inputStream = new ByteArrayInputStream(imageBytes);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(imageBytes.length);
Thumbnails.of(inputStream)
.scale(accuracy)
.outputQuality(accuracy)
.toOutputStream(outputStream);
imageBytes = outputStream.toByteArray();
}
logger.info("图片原大小={}kb | 压缩后大小={}kb", srcSize / 1024, imageBytes.length / 1024);
} catch (Exception e) {
logger.error("【图片压缩】msg=图片压缩失败!", e);
}
return imageBytes;
}
private static double getAccuracy(long size) {
double accuracy;
if (size < 2048) {
accuracy = 0.8;
} else if (size < 7168) {
accuracy = 0.7;
} else if (size < 10240){
accuracy = 0.5;
}else {
accuracy = 0.4;
}
return accuracy;
}
}
package com.zl.service.impl.ocr;
import com.zl.framework.validator.annotation.NotEmpty;
import com.zl.service.ocr.CommonOssService;
import com.zl.framework.util.OssUtil;
import com.zl.util.PicUtil;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
@Service
public class CommonOssServiceImpl implements CommonOssService {
private static Logger logger = LoggerFactory.getLogger(CommonOssServiceImpl.class);
@NotEmpty(params = {"sourcefile"})
@Override
public Map<String, Object> commonPersistPic(Map<String, Object> params) {
String sourceFile = MapUtils.getString(params, "sourcefile");
String ossSourceFile = getOssUploadKey(sourceFile);
params.put("ossurl",ossSourceFile);
InputStream inputStream = null;
try {
inputStream = OssUtil.getObjectContent(sourceFile);
byte[] sourceByte = toByteArray(inputStream);
byte[] resultByte = PicUtil.compressPicForScale(sourceByte, 1500);
InputStream sbs = new ByteArrayInputStream(resultByte);
OssUtil.uploadFile2OSS(sbs,ossSourceFile);
return params;
} catch (Exception e) {
try {
throw e;
} catch (IOException ex) {
ex.printStackTrace();
}
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return params;
}
private static byte[] toByteArray(InputStream input) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024*4];
int n ;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
return output.toByteArray();
}
private String getOssUploadKey(String sourceFile) {
return "attachment/incoming/" + sourceFile;
}
}