This commit is contained in:
Looly 2023-03-03 12:09:08 +08:00
parent 820fbb14c0
commit 7160012e6b
4 changed files with 128 additions and 90 deletions

View File

@ -14,7 +14,7 @@
<artifactId>hutool-swing</artifactId>
<name>${project.artifactId}</name>
<description>Hutool swing桌面相关封装</description>
<description>Hutool swing桌面和图片多媒体相关封装</description>
<properties>
<Automatic-Module-Name>cn.hutool.swing</Automatic-Module-Name>
@ -26,6 +26,7 @@
<artifactId>hutool-core</artifactId>
<version>${project.parent.version}</version>
</dependency>
<!-- 图片 -->
<dependency>
<groupId>com.drewnoakes</groupId>
<artifactId>metadata-extractor</artifactId>

View File

@ -0,0 +1,125 @@
package cn.hutool.swing.img;
import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.io.IORuntimeException;
import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;
import javax.imageio.ImageIO;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
/**
* 图片元信息工具类<br>
* 借助metadata-extractor完成图片元信息的读取如旋转角度等问题
*
* @author wdz
* @since 5.8.13
*/
public class ImgMetaUtil {
/**
* 纠正图片旋转<br>
* 通过读取图片元数据信息获取旋转角度然后根据旋转角度修正图片的角度
*
* @param f 图片文件
* @return {@link BufferedImage}
* @throws IOException IO异常
*/
public static BufferedImage correctBufferImg(final File file) throws IOException {
// 获取偏转角度
final int orientation = getOrientation(file);
// 原始图片缓存
final BufferedImage srcImg = ImageIO.read(file);
//如果不偏转直接返回即可
if (orientation != 90 && orientation != 270) {
return srcImg;
}
// 宽高互换
// 原始宽度
final int imgWidth = srcImg.getHeight();
// 原始高度
final int imgHeight = srcImg.getWidth();
// 中心点位置
final double centerWidth = ((double) imgWidth) / 2;
final double centerHeight = ((double) imgHeight) / 2;
// 图片缓存
final BufferedImage targetImg = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
// 旋转对应角度
final Graphics2D g = targetImg.createGraphics();
g.rotate(Math.toRadians(orientation), centerWidth, centerHeight);
g.drawImage(srcImg, (imgWidth - srcImg.getWidth()) / 2, (imgHeight - srcImg.getHeight()) / 2, null);
g.rotate(Math.toRadians(-orientation), centerWidth, centerHeight);
g.dispose();
return targetImg;
}
/**
* 获取图片文件旋转角度
*
* @param file 上传图片
* @return 旋转角度
* @throws IORuntimeException IO异常
*/
public static int getOrientation(final File file) throws IORuntimeException {
final Metadata metadata;
try {
metadata = ImageMetadataReader.readMetadata(file);
} catch (final ImageProcessingException e) {
throw new UtilException(e);
} catch (final IOException e) {
throw new RuntimeException(e);
}
return getOrientation(metadata);
}
/**
* 获取图片旋转角度
*
* @param in 图片流
* @return 旋转角度
* @throws IORuntimeException IO异常
*/
public static int getOrientation(final InputStream in) throws IORuntimeException {
final Metadata metadata;
try {
metadata = ImageMetadataReader.readMetadata(in);
} catch (final ImageProcessingException e) {
throw new UtilException(e);
} catch (final IOException e) {
throw new RuntimeException(e);
}
return getOrientation(metadata);
}
private static int getOrientation(final Metadata metadata) {
for (final Directory directory : metadata.getDirectories()) {
for (final Tag tag : directory.getTags()) {
if ("Orientation".equals(tag.getTagName())) {
final String orientation = tag.getDescription();
if (orientation.contains("90")) {
return 90;
} else if (orientation.contains("180")) {
return 180;
} else if (orientation.contains("270")) {
return 270;
}
}
}
}
return 0;
}
}

View File

@ -1,88 +0,0 @@
package cn.hutool.swing.img;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;
/**
* 图片避免旋转工具类
*
*@Date: 2023/2/28 19:21<br/>
* @author wdz
* @since 5.8.13
*/
public class ImgRevolveUtil {
/**
* 纠正图片旋转
*
* @param srcFile
*/
public static BufferedImage correctBufferImg(File srcFile) throws IOException, ImageProcessingException {
// 获取偏转角度
int angle = getAngle(srcFile);
//如果不偏转直接返回即可
if (angle != 90 && angle != 270) {
return ImageIO.read(srcFile);
}
// 原始图片缓存
BufferedImage srcImg = ImageIO.read(srcFile);
// 宽高互换
// 原始宽度
int imgWidth = srcImg.getHeight();
// 原始高度
int imgHeight = srcImg.getWidth();
// 中心点位置
double centerWidth = ((double) imgWidth) / 2;
double centerHeight = ((double) imgHeight) / 2;
// 图片缓存
BufferedImage targetImg = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
// 旋转对应角度
Graphics2D g = targetImg.createGraphics();
g.rotate(Math.toRadians(angle), centerWidth, centerHeight);
g.drawImage(srcImg, (imgWidth - srcImg.getWidth()) / 2, (imgHeight - srcImg.getHeight()) / 2, null);
g.rotate(Math.toRadians(-angle), centerWidth, centerHeight);
g.dispose();
return targetImg;
}
/**
* 获取图片旋转角度
*
* @param file 上传图片
* @return
*/
public static int getAngle(File file) throws ImageProcessingException, IOException {
Metadata metadata = ImageMetadataReader.readMetadata(file);
for (Directory directory : metadata.getDirectories()) {
for (Tag tag : directory.getTags()) {
if ("Orientation".equals(tag.getTagName())) {
String orientation = tag.getDescription();
if (orientation.contains("90")) {
return 90;
} else if (orientation.contains("180")) {
return 180;
} else if (orientation.contains("270")) {
return 270;
}
}
}
}
return 0;
}
}

View File

@ -1631,7 +1631,7 @@ public class ImgUtil {
public static BufferedImage read(final File imageFile) {
final BufferedImage result;
try {
result = ImgRevolveUtil.correctBufferImg(imageFile);
result = ImageIO.read(imageFile);
} catch (final IOException e) {
throw new IORuntimeException(e);
}