不修改圖片的大小尺寸,壓縮圖片佔用內存
1、使用thumbnailator庫
支持所有格式圖片,存在壓縮後圖片文件佔用內存變大的情況
maven依賴
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
github源碼:https://github.com/coobird/thumbnailator
工具代碼
/**
* 使用thumbnailator庫,不修改圖片尺寸,壓縮圖片佔用內存
* 支持所有格式圖片,存在壓縮後圖片文件佔用內存變大的情況
* @param srcPath 原文件路徑
* @param destPath 壓縮後的文件路徑
* @param quality 圖片質量,取值範圍[0, 1]
* @return
* @throws IOException
*/
public static void compressImageByThumbnails (String srcPath, String destPath, float quality) throws IOException {
Thumbnails.of(srcPath).scale(1f).outputQuality(quality).toFile(destPath);
}
2、使用jdk
僅僅支持文件頭是jpg格式圖片(png格式圖片壓縮後背景顏色存在問題)
工具代碼
/**
* 使用JDK,不修改圖片尺寸,壓縮圖片佔用內存
* 僅僅支持文件頭是jpg格式圖片(png格式圖片壓縮後背景顏色存在問題)
* 存在壓縮後圖片文件佔用內存變大的情況
* @param srcPath 原文件路徑
* @param destPath 壓縮後的文件路徑
* @param quality 圖片質量,取值範圍[0, 1]
* @return
* @throws IOException
*/
public static boolean compressImageByJdk(String srcPath, String destPath, float quality) {
boolean isSuccess = false;
File image = new File(srcPath);
if (image.exists()) {
String imageType = null;
try {
imageType = getImageType(image);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Iterator<ImageWriter> imageWriterIte = ImageIO.getImageWritersByFormatName(imageType);
if (imageWriterIte != null && imageWriterIte.hasNext()) {
ImageWriter writer = imageWriterIte.next();
ImageWriteParam imgWriteParam;
BufferedImage srcImage = null;
FileOutputStream fos = null;
try {
//寫圖片參數
imgWriteParam = writer.getDefaultWriteParam();
//必須指定壓縮方式爲MODE_EXPLICIT
imgWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
//設置圖片質量
imgWriteParam.setCompressionQuality(quality);
// imgWriteParam.setProgressiveMode(ImageWriteParam.MODE_DISABLED);
// //圖片顏色模式
// ColorModel colorModel = ImageIO.read(image).getColorModel();
// imgWriteParam.setDestinationType(new ImageTypeSpecifier(
// colorModel, colorModel.createCompatibleSampleModel(16, 16)));
fos = new FileOutputStream(destPath);
writer.reset();
writer.setOutput(ImageIO.createImageOutputStream(fos));
srcImage = ImageIO.read(image);
writer.write(null, new IIOImage(srcImage, null, null), imgWriteParam);
isSuccess = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.flush();
fos.close();
writer.dispose();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
} else {
throw new RuntimeException("文件不存在[ " + srcPath + "]");
}
return isSuccess;
}
測試代碼
/**
* 測試圖片壓縮
* @throws IOException
*/
@Test
public void testCompressImageByThumbnails() throws IOException {
String imageName = "java_coffee.png";
String srcPath = IMAGE_PATH + imageName;
imageName = "java_coffee_compress.png";
String destPath = IMAGE_PATH + imageName;
ImageUtil.compressImageByThumbnails(srcPath, destPath, 0.5f);
long srcImageLength = new File(srcPath).length();
long destImageLength = new File(destPath).length();
System.out.println(srcPath + ":" + srcImageLength);
System.out.println(destPath + ":" + destImageLength);
}
/**
* 測試Jdk圖片壓縮
* @throws IOException
*/
@Test
public void testCompressImageByJdk() throws IOException {
String imageName = "java_coffee.jpg";
String srcPath = IMAGE_PATH + imageName;
System.out.println(ImageUtil.getImageType(new File(srcPath)));
imageName = "java_coffee_compress.jpg";
String destPath = IMAGE_PATH + imageName;
Assert.assertTrue(ImageUtil.compressImageByJdk(srcPath, destPath, 0.5f));
long srcImageLength = new File(srcPath).length();
long destImageLength = new File(destPath).length();
System.out.println(srcPath + ":" + srcImageLength);
System.out.println(destPath + ":" + destImageLength);
}