简介
主要利用了工具类对图片进行了循环压缩至指定大小效率不是太高
案例代码如下
- 输入输出都是
byte
数组,后续使用更方便
public class ImageFileCompressUtils {
/**
* 压缩图片
* @param sourceBytes
* @param quality
* @param maxWidth
* @param maxHeight
* @param targetFileSize
* @return
* @throws Exception
*/
public static byte[] compressImageFile(byte[] sourceBytes, double quality, Integer maxWidth, Integer maxHeight, Integer targetFileSize) throws Exception{
BufferedImage bim = ImageIO.read(new ByteArrayInputStream(sourceBytes));
int srcWidth = bim.getWidth();
int srcHeight = bim.getHeight();
//先转换成jpg
Thumbnails.Builder builder = Thumbnails.of(new ByteArrayInputStream(sourceBytes)).outputFormat("png");
// 指定大小(宽或高超出会才会被缩放)
if(srcWidth > maxWidth || srcHeight > maxHeight) {
builder.size(maxWidth, maxHeight);
}else{
//宽高均小,指定原大小
builder.size(srcWidth,srcHeight);
}
// 写入到内存
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); //字节输出流(写入到内存)
builder.toOutputStream(byteArrayOutputStream);
return compressImageCircle(byteArrayOutputStream.toByteArray(),targetFileSize,quality);
}
/**
* 压缩图片至指定大小
* @param sourceBytes 源
* @param targetFileSize 目标
* @param quality 压缩比例0-1最好小于0.9
* @return
* @throws Exception
*/
public static byte[] compressImageCircle(byte[] sourceBytes, long targetFileSize, double quality) throws Exception{
long sourceFileSize = sourceBytes.length;
// 判断大小
if (sourceFileSize <= targetFileSize * 1024) {
return sourceBytes;
}
// 计算宽高
BufferedImage bim = ImageIO.read(new ByteArrayInputStream(sourceBytes));
int sourceWidth = bim.getWidth();
int sourceHeight = bim.getHeight();
int targetWidth = new BigDecimal(sourceWidth).multiply(new BigDecimal(quality)).intValue();
int targetHeight = new BigDecimal(sourceHeight).multiply(new BigDecimal(quality)).intValue();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Thumbnails.of(new ByteArrayInputStream(sourceBytes)).size(targetWidth, targetHeight).outputQuality(quality).toOutputStream(byteArrayOutputStream);
return compressImageCircle(byteArrayOutputStream.toByteArray(), targetFileSize, quality);
}
}
复制代码
使用demo
@Test
public void tt03() throws Exception{
byte[] source = ImageFileCompressUtils.compressImageCircle(Files.readAllBytes(new File("C:\\Users\\86183\\Pictures\\20200911131241.jpg").toPath()),200,0.8);
getFileByBytes(source,"C:\\Users\\86183\\Pictures","2222.png");
}
//将Byte数组转换成文件
public static void getFileByBytes(byte[] bytes, String filePath, String fileName) {
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
try {
File dir = new File(filePath);
if (!dir.exists() && dir.isDirectory()) {// 判断文件目录是否存在
dir.mkdirs();
}
file = new File(filePath + "\\" + fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bytes);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
复制代码
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END