114 lines
3.9 KiB
Java
114 lines
3.9 KiB
Java
package com.example.sso.dao;
|
||
|
||
import net.coobird.thumbnailator.Thumbnails;
|
||
import net.coobird.thumbnailator.geometry.Positions;
|
||
import javax.imageio.ImageIO;
|
||
import java.awt.image.BufferedImage;
|
||
import java.io.*;
|
||
import java.nio.file.Files;
|
||
import java.nio.file.Paths;
|
||
|
||
public class ThumbnailCompressor {
|
||
|
||
private static final long MAX_SIZE = 300 * 1024; // 500KB
|
||
|
||
/**
|
||
* 使用Thumbnailator压缩图片
|
||
*/
|
||
public static long compressWithThumbnailator(String inputPath, String outputPath) throws IOException {
|
||
File inputFile = new File(inputPath);
|
||
long fileSize = inputFile.length();
|
||
|
||
System.out.println("原始大小: " + formatFileSize(fileSize));
|
||
|
||
// 如果小于500KB,直接复制
|
||
if (fileSize <= MAX_SIZE) {
|
||
Files.copy(inputFile.toPath(), Paths.get(outputPath));
|
||
return fileSize;
|
||
}
|
||
|
||
// 获取图片尺寸
|
||
BufferedImage originalImage = ImageIO.read(inputFile);
|
||
int width = originalImage.getWidth();
|
||
int height = originalImage.getHeight();
|
||
|
||
// 计算缩放比例
|
||
float scale = 1.0f;
|
||
if (width > 1920 || height > 1080) {
|
||
float widthRatio = 1920f / width;
|
||
float heightRatio = 1080f / height;
|
||
scale = Math.min(widthRatio, heightRatio);
|
||
}
|
||
|
||
// 逐步压缩
|
||
float quality = 0.9f;
|
||
long compressedSize = Long.MAX_VALUE;
|
||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||
|
||
while (quality > 0.1f && compressedSize > MAX_SIZE) {
|
||
baos.reset();
|
||
|
||
Thumbnails.of(inputFile)
|
||
.scale(scale)
|
||
.outputQuality(quality)
|
||
.outputFormat("jpg")
|
||
.toOutputStream(baos);
|
||
|
||
compressedSize = baos.size();
|
||
System.out.println("质量: " + quality + ", 大小: " + formatFileSize(compressedSize));
|
||
|
||
quality -= 0.1f;
|
||
}
|
||
|
||
// 保存文件
|
||
try (FileOutputStream fos = new FileOutputStream(outputPath)) {
|
||
fos.write(baos.toByteArray());
|
||
}
|
||
|
||
return compressedSize;
|
||
}
|
||
|
||
/**
|
||
* 批量压缩
|
||
*/
|
||
public static void batchCompress(String inputDir, String outputDir, long maxSize) throws IOException {
|
||
File dir = new File(inputDir);
|
||
File[] imageFiles = dir.listFiles((d, name) ->
|
||
name.toLowerCase().endsWith(".jpg") ||
|
||
name.toLowerCase().endsWith(".jpeg") ||
|
||
name.toLowerCase().endsWith(".png") ||
|
||
name.toLowerCase().endsWith(".bmp")
|
||
);
|
||
|
||
if (imageFiles == null) return;
|
||
|
||
for (File imageFile : imageFiles) {
|
||
String outputPath = outputDir + File.separator + "compressed_" + imageFile.getName();
|
||
long compressedSize = compressWithThumbnailator(imageFile.getPath(), outputPath);
|
||
|
||
System.out.println(imageFile.getName() +
|
||
" 压缩后: " + formatFileSize(compressedSize));
|
||
}
|
||
}
|
||
|
||
private static String formatFileSize(long size) {
|
||
if (size < 1024) return size + " B";
|
||
if (size < 1024 * 1024) return String.format("%.2f KB", size / 1024.0);
|
||
if (size < 1024 * 1024 * 1024) return String.format("%.2f MB", size / (1024.0 * 1024.0));
|
||
return String.format("%.2f GB", size / (1024.0 * 1024.0 * 1024.0));
|
||
}
|
||
|
||
public static void main(String[] args) {
|
||
try {
|
||
// 压缩单个图片
|
||
long size = compressWithThumbnailator("D:\\下载模板\\微信图片_20251224111834_4_27.jpg", "D:\\下载模板\\微信图片_20251224111834_4_27.jpg");
|
||
System.out.println("最终大小: " + formatFileSize(size));
|
||
|
||
// 批量压缩
|
||
// batchCompress("input_images", "output_images", 500 * 1024);
|
||
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
} |