package com.dji.sample.territory.utils;
|
|
import com.dji.sample.media.service.impl.FileServiceImpl;
|
|
import javax.imageio.ImageIO;
|
import javax.imageio.ImageWriteParam;
|
import javax.imageio.ImageWriter;
|
import javax.imageio.stream.ImageOutputStream;
|
import javax.imageio.stream.MemoryCacheImageOutputStream;
|
import java.awt.*;
|
import java.awt.image.BufferedImage;
|
import java.io.*;
|
import java.nio.file.Files;
|
import java.nio.file.StandardCopyOption;
|
|
public class ImgZipUtil {
|
/**
|
* 图片压缩
|
*
|
* @return
|
* @throws IOException
|
*/
|
public static File compressImage(File inputFile, int targetSizeKB) throws IOException {
|
BufferedImage image = ImageIO.read(inputFile);
|
|
// 计算目标图片的尺寸
|
long targetSizeBytes = targetSizeKB * 1024L;
|
long originalSizeBytes = getImageSize(image);
|
double compressionRatio = (double) targetSizeBytes / originalSizeBytes;
|
int targetWidth = (int) (image.getWidth() * Math.sqrt(compressionRatio));
|
int targetHeight = (int) (image.getHeight() * Math.sqrt(compressionRatio));
|
|
// 使用ImageIO进行压缩
|
BufferedImage compressedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
|
Graphics2D graphics = compressedImage.createGraphics();
|
graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
graphics.drawImage(image, 0, 0, targetWidth, targetHeight, null);
|
graphics.dispose();
|
|
// 将压缩后的图片写入输出文件
|
File outputFile = new File(inputFile.getParent(), "compressed_" + inputFile.getName());
|
ImageIO.write(compressedImage, "jpeg", outputFile);
|
return outputFile;
|
}
|
|
public static long getImageSize(BufferedImage image) {
|
File tempFile;
|
try {
|
tempFile = File.createTempFile("temp", ".tmp");
|
ImageIO.write(image, "jpg", tempFile);
|
long size = tempFile.length();
|
tempFile.delete();
|
return size;
|
} catch (IOException ex) {
|
ex.printStackTrace();
|
return 0;
|
}
|
}
|
|
/**
|
* 图片压缩
|
*
|
* @param originalImageFile
|
* @param compressionQuality 图片压缩质量 0-1
|
* @return
|
* @throws IOException
|
*/
|
public static File compressImageAndGetFile(File originalImageFile, float compressionQuality) throws IOException {
|
BufferedImage originalImage = ImageIO.read(originalImageFile);
|
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
|
ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
|
try (ImageOutputStream ios = new MemoryCacheImageOutputStream(baos)) {
|
writer.setOutput(ios);
|
ImageWriteParam param = writer.getDefaultWriteParam();
|
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
|
param.setCompressionQuality(compressionQuality);
|
writer.write(null, new javax.imageio.IIOImage(originalImage, null, null), param);
|
} finally {
|
writer.dispose();
|
}
|
InputStream in = new ByteArrayInputStream(baos.toByteArray());
|
File tempFile = File.createTempFile("compressed-", ".jpg");
|
tempFile.deleteOnExit();
|
Files.copy(in, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
return tempFile;
|
}
|
}
|
|
public static void main(String[] args) throws IOException {
|
String name="test/瑞金图片.jpeg";
|
File file=compressImageAndGetFile(new File("D:\\ideawork\\used-car\\src\\main\\resources\\DJI_20240712113312_0007_V_航点dkbh2_null.jpeg"),0.5F);
|
FileServiceImpl.uploadFile("http://139.196.74.78:9000", "sxkj", "sxkj2024", "cloud-bucket", name, file, "image/jpeg");
|
}
|
}
|