修改模糊图
parent
473998f9ce
commit
6dcafcca0a
|
|
@ -2,6 +2,9 @@ package com.zcloud.edu.domain.utils;
|
||||||
|
|
||||||
import com.deepoove.poi.data.PictureRenderData;
|
import com.deepoove.poi.data.PictureRenderData;
|
||||||
import com.deepoove.poi.data.Pictures;
|
import com.deepoove.poi.data.Pictures;
|
||||||
|
import com.drew.imaging.ImageMetadataReader;
|
||||||
|
import com.drew.metadata.Metadata;
|
||||||
|
import com.drew.metadata.exif.ExifIFD0Directory;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
import javax.imageio.ImageReader;
|
import javax.imageio.ImageReader;
|
||||||
|
|
@ -27,12 +30,19 @@ public class FixedPictureUtils {
|
||||||
* @return 正常显示的PictureRenderData
|
* @return 正常显示的PictureRenderData
|
||||||
* @throws IOException IO异常
|
* @throws IOException IO异常
|
||||||
*/
|
*/
|
||||||
public static PictureRenderData ofNormalUrl(String imgUrl, int width, int height) throws IOException {
|
public static PictureRenderData ofNormalUrl(String imgUrl, int width, int height) {
|
||||||
byte[] imgBytes = readBytesFromUrl(imgUrl);
|
try{
|
||||||
byte[] fixedImgBytes = fixImageRotation(imgBytes);
|
byte[] imgBytes = readBytesFromUrl(imgUrl);
|
||||||
return Pictures.ofBytes(fixedImgBytes)
|
byte[] fixedImgBytes = fixImageRotation(imgBytes);
|
||||||
.size(width, height)
|
return Pictures.ofBytes(fixedImgBytes)
|
||||||
.create();
|
.size(width, height)
|
||||||
|
.create();
|
||||||
|
}catch (Exception e){
|
||||||
|
return Pictures.ofUrl(imgUrl)
|
||||||
|
.size(width, height)
|
||||||
|
.create();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -54,29 +64,65 @@ public class FixedPictureUtils {
|
||||||
/**
|
/**
|
||||||
* 核心:修复图片旋转(JDK 8兼容)
|
* 核心:修复图片旋转(JDK 8兼容)
|
||||||
*/
|
*/
|
||||||
private static byte[] fixImageRotation(byte[] imageBytes) throws IOException {
|
private static byte[] fixImageRotation(byte[] imageBytes) throws Exception {
|
||||||
// 1. 解析EXIF旋转角度
|
try (ByteArrayInputStream bais = new ByteArrayInputStream(imageBytes)) {
|
||||||
int rotation = getExifRotation(imageBytes);
|
// 1. 读取 EXIF 元数据
|
||||||
|
Metadata metadata = ImageMetadataReader.readMetadata(bais);
|
||||||
|
|
||||||
// 2. 兜底检测:无EXIF时自动判断
|
// 2. 获取旋转角度
|
||||||
if (rotation == 0) {
|
int rotation = getRotationFromMetadata(metadata);
|
||||||
rotation = detectImageOrientation(imageBytes);
|
|
||||||
|
// 3. 如果不需要旋转,直接返回原字节
|
||||||
|
if (rotation == 0) {
|
||||||
|
return imageBytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. 读取并旋转图片
|
||||||
|
bais.reset(); // 重置流到开始位置
|
||||||
|
BufferedImage original = ImageIO.read(bais);
|
||||||
|
BufferedImage rotated = rotateImage(original, rotation);
|
||||||
|
|
||||||
|
// 5. 转为字节数组返回
|
||||||
|
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
|
||||||
|
String format = getImageFormat(imageBytes);
|
||||||
|
ImageIO.write(rotated, format, baos);
|
||||||
|
return baos.toByteArray();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 从EXIF读取需要旋转的角度
|
||||||
|
* 返回:0-不需要旋转,90/180/270-需要旋转的角度
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* 从 Metadata 获取旋转角度
|
||||||
|
* @param metadata EXIF 元数据
|
||||||
|
* @return 需要旋转的角度(0/90/180/270)
|
||||||
|
*/
|
||||||
|
private static int getRotationFromMetadata(Metadata metadata) {
|
||||||
|
try {
|
||||||
|
ExifIFD0Directory exifIFD0Directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
|
||||||
|
|
||||||
// 3. 无需旋转则返回原字节
|
if (exifIFD0Directory == null || !exifIFD0Directory.containsTag(ExifIFD0Directory.TAG_ORIENTATION)) {
|
||||||
if (rotation == 0) {
|
return 0; // 没有 EXIF 或 Orientation 标签,不需要旋转
|
||||||
return imageBytes;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// 4. 读取并旋转图片
|
int orientation = exifIFD0Directory.getInt(ExifIFD0Directory.TAG_ORIENTATION);
|
||||||
BufferedImage original = readImage(imageBytes);
|
|
||||||
BufferedImage rotated = rotateImage(original, rotation);
|
|
||||||
|
|
||||||
// 5. 转为字节返回
|
// 根据 Orientation 值返回对应的旋转角度
|
||||||
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
|
switch (orientation) {
|
||||||
String format = getImageFormat(imageBytes);
|
case 3: // 旋转 180 度
|
||||||
ImageIO.write(rotated, format, baos);
|
return 180;
|
||||||
return baos.toByteArray();
|
case 6: // 顺时针旋转 90 度
|
||||||
|
return 90;
|
||||||
|
case 8: // 逆时针旋转 90 度(270 度)
|
||||||
|
return 270;
|
||||||
|
default: // 1 或其他,不需要旋转
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
// 任何异常都返回 0 度
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue