|
@@ -13,13 +13,12 @@ import com.drew.metadata.Metadata;
|
|
import com.drew.metadata.exif.ExifIFD0Directory;
|
|
import com.drew.metadata.exif.ExifIFD0Directory;
|
|
import com.google.common.collect.Lists;
|
|
import com.google.common.collect.Lists;
|
|
import com.google.common.collect.Maps;
|
|
import com.google.common.collect.Maps;
|
|
|
|
+import org.apache.commons.imaging.ImageReadException;
|
|
|
|
+import org.apache.commons.imaging.Imaging;
|
|
import org.apache.commons.lang.StringUtils;
|
|
import org.apache.commons.lang.StringUtils;
|
|
import org.springframework.util.CollectionUtils;
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
-import javax.imageio.IIOImage;
|
|
|
|
-import javax.imageio.ImageIO;
|
|
|
|
-import javax.imageio.ImageWriteParam;
|
|
|
|
-import javax.imageio.ImageWriter;
|
|
|
|
|
|
+import javax.imageio.*;
|
|
import java.awt.*;
|
|
import java.awt.*;
|
|
import java.awt.color.ColorSpace;
|
|
import java.awt.color.ColorSpace;
|
|
import java.awt.color.ICC_ColorSpace;
|
|
import java.awt.color.ICC_ColorSpace;
|
|
@@ -141,8 +140,9 @@ public class CommonUtil {
|
|
/**
|
|
/**
|
|
* 根据OSS文件路径获取文件输入流
|
|
* 根据OSS文件路径获取文件输入流
|
|
*/
|
|
*/
|
|
- public static InputStream getOSSInputStream(String urlStr) {
|
|
|
|
|
|
+ public static synchronized InputStream getOSSInputStream(String urlStr) {
|
|
try {
|
|
try {
|
|
|
|
+ System.out.println("----前-------"+urlStr);
|
|
urlStr = replaceOssUrl(urlStr);
|
|
urlStr = replaceOssUrl(urlStr);
|
|
int lastIndex = urlStr.lastIndexOf("/") + 1;
|
|
int lastIndex = urlStr.lastIndexOf("/") + 1;
|
|
String fileName = urlStr.substring(lastIndex);
|
|
String fileName = urlStr.substring(lastIndex);
|
|
@@ -150,14 +150,11 @@ public class CommonUtil {
|
|
//获取OSS文件流
|
|
//获取OSS文件流
|
|
URL url = new URL(urlStr);
|
|
URL url = new URL(urlStr);
|
|
URLConnection conn = url.openConnection();
|
|
URLConnection conn = url.openConnection();
|
|
- /* // 设置连接超时时间
|
|
|
|
- conn.setConnectTimeout(10000); // 5秒
|
|
|
|
- // 设置读取超时时间
|
|
|
|
- conn.setReadTimeout(10000); // 5秒*/
|
|
|
|
|
|
+
|
|
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
|
|
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
|
|
return conn.getInputStream();
|
|
return conn.getInputStream();
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
- System.out.println("zw-----------");
|
|
|
|
|
|
+ System.out.println("-----后------"+urlStr);
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -526,26 +523,109 @@ public class CommonUtil {
|
|
* @throws MetadataException
|
|
* @throws MetadataException
|
|
*/
|
|
*/
|
|
|
|
|
|
- public static void main(String[] args) throws IOException {
|
|
|
|
|
|
+/* public static void main(String[] args) throws IOException, ImageProcessingException, MetadataException {
|
|
// String imgurl = "/Users/hongchuangyanfa/Desktop/excel/432123.jpg";
|
|
// String imgurl = "/Users/hongchuangyanfa/Desktop/excel/432123.jpg";
|
|
// String imgurl = "/Users/hongchuangyanfa/Desktop/excel/123.jpg";
|
|
// String imgurl = "/Users/hongchuangyanfa/Desktop/excel/123.jpg";
|
|
- String imgurl = "/Users/hongchuangyanfa/Downloads/bccb09f5e2caa85611d380f596d4febb.jpg";
|
|
|
|
- File file = ResourceUtils.getFile(imgurl);
|
|
|
|
|
|
|
|
- byte[] imageData = InputStreamToBytes(new FileInputStream(file));
|
|
|
|
|
|
|
|
|
|
+ // String imgurl = "/Users/hongchuangyanfa/Downloads/03c1994653c4d59ab596e683a109e247 (1).jpg";
|
|
|
|
+ String imgurl = "http://183.247.216.148:9000/minio-oss-chongqing/upload/20250414/03c1994653c4d59ab596e683a109e247.jpg";
|
|
|
|
+
|
|
|
|
+ compressImage(imgurl);
|
|
|
|
+
|
|
|
|
+ System.out.println("图片转换并保存成功!");
|
|
|
|
+ }*/
|
|
|
|
+
|
|
|
|
+ public static byte[] compressImage(String url) throws IOException, ImageProcessingException, MetadataException{
|
|
|
|
+ // 读取原始图像(处理旋转问题)
|
|
|
|
+ InputStream file = CommonUtil.getOSSInputStream(url);
|
|
|
|
+ InputStream file2 = CommonUtil.getOSSInputStream(url);
|
|
|
|
+ byte[] imageData = InputStreamToBytes(file);
|
|
|
|
+
|
|
|
|
+ Metadata metadata = ImageMetadataReader.readMetadata(new ByteArrayInputStream(imageData));
|
|
|
|
+ if (metadata.containsDirectoryOfType(ExifIFD0Directory.class)) {
|
|
|
|
+ ExifIFD0Directory exifIFD0Directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
|
|
|
|
+ if (exifIFD0Directory.containsTag(ExifIFD0Directory.TAG_ORIENTATION)) {
|
|
|
|
+ // 获取 Orientation 标签的值
|
|
|
|
+ int orientation = exifIFD0Directory.getInt(ExifIFD0Directory.TAG_ORIENTATION);
|
|
|
|
+ // 需要旋转图片
|
|
|
|
+ // 1 无需纠正 2 水平翻转(镜像)3 垂直翻转(旋转180°) 4 水平翻转+垂直翻转 5 水平翻转+旋转90°
|
|
|
|
+ // 6 旋转90° 7 水平翻转+旋转270° 8 +旋转270°
|
|
|
|
+ if (orientation > 1) {
|
|
|
|
+ BufferedImage originalImage = ImageIO.read(new ByteArrayInputStream(imageData));
|
|
|
|
+ AffineTransform transform = new AffineTransform();
|
|
|
|
+ if (orientation == 3) {
|
|
|
|
+ transform.rotate(Math.PI, originalImage.getWidth() / 2, originalImage.getHeight() / 2);
|
|
|
|
+ } else if (orientation == 6) {
|
|
|
|
+ transform.rotate(Math.PI / 2, originalImage.getWidth() / 2, originalImage.getHeight() / 2);
|
|
|
|
+ } else if (orientation == 8) {
|
|
|
|
+ transform.rotate(-Math.PI / 2, originalImage.getWidth() / 2, originalImage.getHeight() / 2);
|
|
|
|
+ }
|
|
|
|
+ AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
|
|
|
|
+ originalImage = op.filter(originalImage, null);
|
|
|
|
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
+ ImageIO.write(originalImage, "jpg", baos);
|
|
|
|
+ imageData = baos.toByteArray();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 缩放图像
|
|
|
|
+ String formatName = "JPEG";
|
|
ByteArrayInputStream bais = new ByteArrayInputStream(imageData);
|
|
ByteArrayInputStream bais = new ByteArrayInputStream(imageData);
|
|
- try {
|
|
|
|
- ImageIO.setUseCache(false);
|
|
|
|
- BufferedImage originalImage = ImageIO.read(bais);
|
|
|
|
- } catch (IOException e) {
|
|
|
|
- throw new RuntimeException(e);
|
|
|
|
|
|
+ BufferedImage originalImage =ImageIO.read(file2);
|
|
|
|
+
|
|
|
|
+ long sizeLimit = 1024*1024*5; //5M 1920 ×1080
|
|
|
|
+ int width = 1080;
|
|
|
|
+ int height = 1920;
|
|
|
|
+ Image scaledImage = originalImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
|
|
|
|
+ BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
|
|
|
|
+ resizedImage.getGraphics().drawImage(scaledImage, 0, 0, null);
|
|
|
|
+
|
|
|
|
+ // 压缩图像
|
|
|
|
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
+ ImageIO.write(resizedImage, formatName, baos);
|
|
|
|
+
|
|
|
|
+ if (baos.size() <= sizeLimit) {
|
|
|
|
+ // 图片大小已经小于等于目标大小,直接返回原始数据
|
|
|
|
+ return baos.toByteArray();
|
|
}
|
|
}
|
|
- System.out.println("图片转换并保存成功!");
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
+ float quality = 0.9f; // 初始化压缩质量
|
|
|
|
+ int retries = 10; // 最多尝试 10 次
|
|
|
|
+
|
|
|
|
+ while (baos.size() > sizeLimit && retries > 0) {
|
|
|
|
+ // 压缩图像并重新计算压缩质量
|
|
|
|
+ byte[] data = baos.toByteArray();
|
|
|
|
+ bais = new ByteArrayInputStream(data);
|
|
|
|
+ BufferedImage compressedImage = ImageIO.read(bais);
|
|
|
|
+ baos.reset();
|
|
|
|
|
|
- public static byte[] compressImage(byte[] imageData) throws IOException, ImageProcessingException, MetadataException {
|
|
|
|
|
|
+ ImageWriter writer = null;
|
|
|
|
+ Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(formatName);
|
|
|
|
+ if (writers.hasNext()) {
|
|
|
|
+ writer = writers.next();
|
|
|
|
+ } else {
|
|
|
|
+ throw new IllegalArgumentException("Unsupported image format: " + formatName);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ ImageWriteParam writeParam = writer.getDefaultWriteParam();
|
|
|
|
+ writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
|
|
|
|
+ writeParam.setCompressionQuality(1);
|
|
|
|
+
|
|
|
|
+ writer.setOutput(ImageIO.createImageOutputStream(baos));
|
|
|
|
+ writer.write(null, new IIOImage(compressedImage, null, null), writeParam);
|
|
|
|
+ writer.dispose();
|
|
|
|
+
|
|
|
|
+ float ratio = sizeLimit * 1.0f / baos.size();
|
|
|
|
+ quality *= Math.sqrt(ratio);
|
|
|
|
+ retries--;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return baos.toByteArray();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static byte[] compressImage(byte[] imageData) throws IOException, ImageProcessingException, MetadataException{
|
|
// 读取原始图像(处理旋转问题)
|
|
// 读取原始图像(处理旋转问题)
|
|
Metadata metadata = ImageMetadataReader.readMetadata(new ByteArrayInputStream(imageData));
|
|
Metadata metadata = ImageMetadataReader.readMetadata(new ByteArrayInputStream(imageData));
|
|
if (metadata.containsDirectoryOfType(ExifIFD0Directory.class)) {
|
|
if (metadata.containsDirectoryOfType(ExifIFD0Directory.class)) {
|
|
@@ -578,7 +658,13 @@ public class CommonUtil {
|
|
// 缩放图像
|
|
// 缩放图像
|
|
String formatName = "JPEG";
|
|
String formatName = "JPEG";
|
|
ByteArrayInputStream bais = new ByteArrayInputStream(imageData);
|
|
ByteArrayInputStream bais = new ByteArrayInputStream(imageData);
|
|
- BufferedImage originalImage = ImageIO.read(bais);
|
|
|
|
|
|
+ BufferedImage originalImage = null;
|
|
|
|
+ try {
|
|
|
|
+ originalImage = Imaging.getBufferedImage(imageData);
|
|
|
|
+ } catch (ImageReadException e) {
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
long sizeLimit = 1024*1024*5; //5M 1920 ×1080
|
|
long sizeLimit = 1024*1024*5; //5M 1920 ×1080
|
|
int width = 1080;
|
|
int width = 1080;
|
|
int height = 1920;
|
|
int height = 1920;
|