Java图片压缩三种高效压缩方案详细解析
2025年04月08日
•
Java •
一、基于opencv的智能尺寸压缩java public static void extracted2() { 'string path = "c:\test.jpg"; string savepat
一、基于opencv的智能尺寸压缩
java public static void extracted2() { '
string path = "c:\test.jpg";
string savepath = "d:\compressed.jpg";
int maxwidth = 800;
int maxheight = 600;
compressimage(new file(path), new file(savepath), maxwidth, maxheight);
}
compressimage 写法为kotlin语法法,需要自己转换
fun compressimage(inputfile: file, outputfile: file, maxwidth: int, maxheight: int) {
try {
val image = imageio.read(inputfile)
val originalwidth = image.width
val originalheight = image.height
var newwidth = originalwidth
var newheight = originalheight
// 计算新的宽度和高度,保持比例
if (originalwidth > maxwidth || originalheight > maxheight) {
val ratio = math.min(maxwidth.todouble() / originalwidth, maxheight.todouble() / originalheight)
newwidth = (originalwidth * ratio).toint()
newheight = (originalheight * ratio).toint()
}
val resizedimage = bufferedimage(newwidth, newheight, bufferedimage.type_int_rgb)
resizedimage.creategraphics().apply {
drawimage(image.getscaledinstance(newwidth, newheight, java.awt.image.scale_smooth), 0, 0, null)
dispose()
}
// 确保输出目录存在
val outputpath: path = paths.get(outputfile.parent)
if (!files.exists(outputpath)) {
files.createdirectories(outputpath)
}
imageio.write(resizedimage, "jpg", outputfile)
} catch (e: ioexception) {
e.printstacktrace()
}
}
技术亮点:
- 动态尺寸调整:通过设置最大宽高(800x600),自动保持原图比例
- opencv加持:使用imgproc.resize()进行高质量缩放
- 跨平台支持:需配置opencv本地库(system.loadlibrary)
适用场景:
二、jpeg质量参数压缩
java public static void extracted4() {
for (int i = 1; i <=10; i++) {
float quality = 0.1f * i;
compressimage(inputfile, outputfile, quality);
}
}
public static void compressimage(file inputfile, file outputfile, float quality) throws ioexception {
// 读取图片
bufferedimage image = imageio.read(inputfile);
// 获取图片写入器
iterator<imagewriter> writers = imageio.getimagewritersbyformatname("webp");
imagewriter writer = writers.next();
// 设置写入器的输出目标
imageoutputstream ios = imageio.createimageoutputstream(outputfile);
writer.setoutput(ios);
// 创建图片写入器配置
iioimage imageio = new iioimage(image, null, null);
imagewriteparam param = writer.getdefaultwriteparam();
// 设置压缩质量
if (param.canwritecompressed()) {
param.setcompressionmode(imagewriteparam.mode_explicit);
param.setcompressionquality(quality);
}
// 写入图片
writer.write(null, imageio, param);
// 关闭资源
ios.close();
writer.dispose();
}
关键技术:
- 质量梯度测试:从0.1到1.0进行10级压缩测试
- 无损压缩支持:通过imagewriteparam控制压缩模式
- 视觉质量平衡:找到文件大小与清晰度的最佳平衡点
压缩效果对比:
质量参数 | 文件大小 | 清晰度 |
---|
0.3 | 45kb | 可接受 |
0.7 | 120kb | 良好 |
1.0 | 350kb | 无损 |
三、webp高效格式转换
public static void extracted6() {
string path = "c:\\users\\美众\\pictures\\test2.jpg";
for (int i = 1; i <=10; i++) {
float quality = 0.0f + i * 0.1f;
system.out.println("quality:" + quality);
string savepath = "d:\\save\\test2-webp-"+quality+".jpg";
file inputfile = new file(path); // 原始图片文件
file outputfile = new file(savepath);
try {
jpg2webp(inputfile, outputfile,quality);
} catch (exception e) {
throw new runtimeexception(e);
}
}
}
public static void jpg2webp(file oldfile, file newfile,float quality){
try {
// 获取原始文件的编码
bufferedimage image = imageio.read(oldfile);
// 创建webp imagewriter实例
imagewriter writer = imageio.getimagewritersbymimetype("image/webp").next();
// 配置编码参数
webpwriteparam writeparam = new webpwriteparam(writer.getlocale());
// 设置压缩模式
writeparam.setcompressionmode(webpwriteparam.mode_explicit);
system.out.println("getcompressiontypes:"+json.tojson(writeparam.getcompressiontypes()));
// "lossy"-有损,"lossless"-无损
writeparam.setcompressiontype(writeparam.getcompressiontypes()[0]);
writeparam.setcompressionquality(quality);
// 配置imagewriter输出
writer.setoutput(new fileimageoutputstream(newfile));
// 进行编码,重新生成新图片
writer.write(null, new iioimage(image, null, null), writeparam);
system.out.println("jpg文件转成webp格式成功");
} catch (filenotfoundexception e) {
e.printstacktrace();
} catch (ioexception e) {
e.printstacktrace();
}
}
核心优势:
- 压缩率提升:比jpeg节省25-35%空间
- 透明通道支持:支持alpha通道透明效果
- 渐进式加载:支持渐进式解码加载
性能对比:
格式 | 质量0.8 | 加载速度 | 兼容性 |
---|
jpeg | 150kb | 快 | 100% |
webp | 95kb | 较快 | 95%+ |
四、方案选型建议
- 移动端优先:webp + 质量压缩(0.6-0.8)
- 用户上传处理:尺寸压缩 + jpeg质量0.7
- 专业图库存储:opencv双算法校验(直方图对比+尺寸压缩)
总结
到此这篇关于java图片压缩三种高效压缩方案的文章就介绍到这了,更多相关java图片高效压缩内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!