工具类
package com.song.springclouddemoshardingjdbc.utils;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
public class ImageUtils {
public static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) throws IOException {
Image resultingImage = originalImage.getScaledInstance(targetWidth, targetHeight, Image.SCALE_AREA_AVERAGING);
BufferedImage outputImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
outputImage.getGraphics().drawImage(resultingImage, 0, 0, null);
return outputImage;
}
public static byte[] imageToBytes(BufferedImage bImage) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
ImageIO.write(bImage, "jpg", out);
} catch (IOException e) {
e.printStackTrace();
}
return out.toByteArray();
}
private static BufferedImage bytesToBufferedImage(byte[] ImageByte) {
ByteArrayInputStream in = new ByteArrayInputStream(ImageByte);
BufferedImage image = null;
try {
image = ImageIO.read(in);
} catch (IOException e) {
e.printStackTrace();
}
return image;
}
public static void compressImageWithQuality(BufferedImage image, File outputFile, double compressionQuality) throws IOException {
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
ImageWriter writer = writers.next();
ImageOutputStream ios = ImageIO.createImageOutputStream(outputFile);
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality((float) compressionQuality);
writer.write(null, new IIOImage(image, null, null), param);
ios.flush();
ios.close();
writer.dispose();
}
}
实现
package com.song.springclouddemoshardingjdbc.controller;
import com.song.springclouddemoshardingjdbc.utils.ImageUtils;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.File;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
public class TestController {
public static void main(String[] args) {
try {
URL img = new URL("https://gitee.com/song-rice/upload-image/raw/2bb7a342209089bf38fc971851f25d5ba77e7ce6/%E7%99%BD%E5%BA%95%E8%AF%81%E4%BB%B6%E7%85%A7_215x300.jpg");
BufferedImage image = ImageIO.read(img);
BufferedImage resizedImage = ImageUtils.resizeImage(image, 215, 300);
File compressedFile = compressImage(resizedImage, 30);
long fileSizeInBytes = compressedFile.length();
System.out.println("Compressed Image File Size: " + fileSizeInBytes + " bytes");
File desktop = new File(System.getProperty("user.home"), "Desktop");
File output = new File(desktop, "compressed.jpg");
Files.copy(compressedFile.toPath(), output.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
}
private static File compressImage(BufferedImage image, int targetFileSizeKb) throws IOException {
double targetFileSizeBytes = targetFileSizeKb * 1024;
double compressionQuality = 1.0;
double maxCompressionQuality = 0.1;
File compressedFile = null;
while (compressionQuality > maxCompressionQuality) {
compressedFile = File.createTempFile("compressed", ".jpg");
ImageIO.write(image, "jpg", compressedFile);
long fileSizeInBytes = compressedFile.length();
if (fileSizeInBytes <= targetFileSizeBytes) {
break;
}
compressionQuality -= 0.1;
ImageUtils.compressImageWithQuality(image, compressedFile, compressionQuality);
}
return compressedFile;
}
}