Java 图片压缩优化 - 减少文件大小的技巧
Java 图片压缩优化 - 减少文件大小的技巧
本文将介绍几种优化 Java 图片压缩代码的方法,以减少图片文件大小,提升性能。
- 使用更高效的图片压缩算法,比如 JPEG 压缩算法。
- 调整压缩参数,比如降低压缩质量、调整压缩尺寸等。
- 引入图片压缩库,比如 ImageMagick、GraphicsMagick 等,利用它们提供的 API 进行图片压缩。这些库通常有更好的压缩效果和更高的性能。
- 优化代码逻辑,比如将多个 if 条件合并为一个、使用 switch 语句等,以提高代码的执行效率。
以下是 Java 代码示例,展示了如何使用 ImageIO 类进行图片压缩:
package com.iyunan.utils;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.file.Files;
import javax.imageio.ImageIO;
public class CompressPictureUtils {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
CompressPictureUtils.compress('E:\gallery/orign_pic71703.JPG', 'E:\gallery\110.jpg');
long endTime = System.currentTimeMillis();
long elapsedTime = endTime - startTime;
System.out.println('程序执行时间为:' + elapsedTime + ' 毫秒');
}
/**
* 图片压缩最大宽 1920 像素
*/
public static final Integer PICTURE_COMPRESS_WIDTH_MAX = 1920;// 1920 像素
/**
* 图片压缩最大高 1920 像素
*/
public static final Integer PICTURE_COMPRESS_HEIGHT_MAX = 1920;// 1920 像素
/**
* 图片压缩最小容量 64KB
*/
public static final Integer PICTURE_COMPRESS_MIN = 1 << 16;// 64KB
/**
* 图片压缩最大容量 64MB
*/
public static final Integer PICTURE_COMPRESS_MAX = 1 << 26;// 64MB
/**
* 图片压缩最小容量差 64KB
*/
public static final Integer PICTURE_COMPRESS_DIFFER_MIN = 1 << 16;// 64KB
public static void compress(String sourceImagePath, String destinationImagePath) {
CoputUtils.getFreeMemoryPercentage();
try {
/* 过滤小于 64kb 和大于 64mb 的图片 */
File sourceImageFile = new File(sourceImagePath);
File destinationImageFile = new File(destinationImagePath);
long sourceImageSize = sourceImageFile.length();
if (sourceImageSize == 0) {
throw new Exception('empty');
}
if (sourceImageSize < PICTURE_COMPRESS_MIN) {
throw new Exception('small');
}
if (sourceImageSize > PICTURE_COMPRESS_MAX) {
throw new Exception('large');
}
/* 设置图片大小 */
BufferedImage inputImage = ImageIO.read(sourceImageFile);
int sourceImageWidth = inputImage.getWidth();
int sourceImageHeight = inputImage.getHeight();
int destinationImageWidth = sourceImageWidth;
int destinationImageHeight = sourceImageHeight;
if (sourceImageWidth > PICTURE_COMPRESS_WIDTH_MAX || sourceImageHeight > PICTURE_COMPRESS_HEIGHT_MAX) {
if (sourceImageWidth > sourceImageHeight) {
destinationImageWidth = PICTURE_COMPRESS_WIDTH_MAX;
destinationImageHeight = destinationImageWidth * sourceImageHeight / sourceImageWidth;
} else {
destinationImageHeight = PICTURE_COMPRESS_HEIGHT_MAX;
destinationImageWidth = destinationImageHeight * sourceImageWidth / sourceImageHeight;
}
}
/* 压缩图片 */
Image image = inputImage.getScaledInstance(destinationImageWidth, destinationImageHeight, Image.SCALE_DEFAULT);
BufferedImage outputImage = new BufferedImage(destinationImageWidth, destinationImageHeight, BufferedImage.TYPE_INT_RGB);
Graphics graphics = outputImage.getGraphics();
graphics.drawImage(image, 0, 0, null);
graphics.dispose();
ImageIO.write(outputImage, 'jpg', destinationImageFile);
/* 过滤比原图小 8kb 以下的的图片 */
long destinationImageSize = destinationImageFile.length();
if (destinationImageSize > (sourceImageSize - PICTURE_COMPRESS_DIFFER_MIN)) {
throw new Exception('low');
}
CoputUtils.getFreeMemoryPercentage();
} catch (Exception e) {
try {
String msg = e.getMessage();
if (msg == 'small' || msg == 'low') {// 过小或压缩率过低,保存原图
File sourceImageFile = new File(sourceImagePath);
File destinationImageFile = new File(destinationImagePath);
destinationImageFile.delete();
Files.copy(sourceImageFile.toPath(), destinationImageFile.toPath());
} else {// 空或过大或异常,保存空图
File emptyfile = new File(destinationImagePath);
emptyfile.delete();
emptyfile.createNewFile();
}
} catch (Exception e1) {
}
}
}
}
代码优化示例:
- 使用
switch语句代替多个if条件,提高代码可读性和效率。 - 使用
try-with-resources语句,确保资源在使用后被释放。 - 使用
System.currentTimeMillis()来测量代码执行时间。 - 优化压缩参数,例如使用更低的压缩质量或更小的压缩尺寸。
- 引入第三方图片压缩库,例如 ImageMagick、GraphicsMagick 等,利用其提供的 API 进行图片压缩。
总结:
通过以上方法,可以有效优化 Java 图片压缩代码,减少图片文件大小,提升性能。选择合适的压缩算法、调整压缩参数、引入第三方图片压缩库以及优化代码逻辑,是提升图片压缩效率的关键因素。
原文地址: https://www.cveoy.top/t/topic/jw9Q 著作权归作者所有。请勿转载和采集!