mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix code
This commit is contained in:
parent
a99dd899d6
commit
b2cac34f9a
@ -597,6 +597,10 @@ public class Img implements Serializable {
|
||||
* @since 3.2.2
|
||||
*/
|
||||
public Img rotate(final int degree) {
|
||||
if(0 == degree){
|
||||
// 不旋转
|
||||
return this;
|
||||
}
|
||||
final Image image = getValidSrcImg();
|
||||
final int width = image.getWidth(null);
|
||||
final int height = image.getHeight(null);
|
||||
|
@ -8,9 +8,6 @@ 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;
|
||||
@ -24,49 +21,6 @@ import java.io.InputStream;
|
||||
*/
|
||||
public class ImgMetaUtil {
|
||||
|
||||
/**
|
||||
* 纠正图片旋转<br>
|
||||
* 通过读取图片元数据信息获取旋转角度,然后根据旋转角度修正图片的角度
|
||||
*
|
||||
* @param file 图片文件
|
||||
* @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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取图片文件旋转角度
|
||||
*
|
||||
|
@ -2,6 +2,7 @@ package cn.hutool.swing.img;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.swing.img.color.ColorUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
@ -170,4 +171,16 @@ public class ImgUtilTest {
|
||||
ImageIO.createImageOutputStream(new File("d:/test/createTransparentImageTest.png"))
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void issue2765Test() {
|
||||
// 利用图片元数据工具读取图片旋转角度信息
|
||||
final File file = FileUtil.file("d:/test/204691690-715c29d9-793a-4b29-ab1d-191a741438bb.jpg");
|
||||
final int orientation = ImgMetaUtil.getOrientation(file);
|
||||
Console.log(orientation);
|
||||
Img.from(file)
|
||||
.rotate(orientation)
|
||||
.write(FileUtil.file("d:/test/aaa.jpg"));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user