`

JAVA 根据exif旋转图片

    博客分类:
  • JAVA
阅读更多

关于什么是exif不再多说,小伙伴们可以自己去查查。

 

由于项目使用的spring mvc上传组件,上传的参数为MultipartFile

 

使用的包为

 

 

<!--exif-->
<!-- https://mvnrepository.com/artifact/com.drewnoakes/metadata-extractor -->
<dependency>
	<groupId>com.drewnoakes</groupId>
	<artifactId>metadata-extractor</artifactId>
	<version>2.8.1</version>
</dependency>

 简单来说这是一个获取文件exif信息的包,当然他的功能还有很多,此处只用了很小的一个功能

 

File jpegFile = new File(deskURL);
FileUtil.byte2File(file.getBytes(),jpegFile);//将图片输出到文件中
Metadata metadata = ImageMetadataReader.readMetadata(jpegFile);
Collection<ExifIFD0Directory> directory = metadata.getDirectoriesOfType(ExifIFD0Directory.class);
ExifIFD0Directory db = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
//获取exif旋转信息
int orientation=db==null?1:db.getInt(ExifIFD0Directory.TAG_ORIENTATION);
Integer turn=360;
//确定旋转度数
if(orientation==0||orientation==1) {
	turn=360;
} else if(orientation==3) {
	turn=180;
} else if(orientation==6) {
	turn=90;
} else if(orientation==8) {
	turn=270;
}
//旋转图片
BufferedImage src = ImageIO.read(jpegFile);
BufferedImage des = RotateImage.Rotate(src, turn);
//旋转图片后写入临时文件
ImageIO.write(des,FileUtil.getSuffix(deskURL), jpegFile);

其中的file是指上传的MultipartFile

我们将MultipartFile写入了一个临时文件中

对临时文件获取了其exif信息,根据exif信息确定了旋转的读书turn

然后对临时文件旋转之后又再次写入临时文件中

至此已经实现了标题所描述功能

下面放旋转图片的代码

package cn.org.dpm.cms.util;

import java.awt.*;
import java.awt.image.BufferedImage;
/**
 * 图片翻转工具
 * Created by LiaoKe on 2017/6/26.
 */
public class RotateImage {

    public static BufferedImage Rotate(Image src, int angel) {
        int src_width = src.getWidth(null);
        int src_height = src.getHeight(null);
        // calculate the new image size
        Rectangle rect_des = CalcRotatedSize(new Rectangle(new Dimension(
                src_width, src_height)), angel);

        BufferedImage res = null;
        res = new BufferedImage(rect_des.width, rect_des.height,
                BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = res.createGraphics();
        // transform
        g2.translate((rect_des.width - src_width) / 2,
                (rect_des.height - src_height) / 2);
        g2.rotate(Math.toRadians(angel), src_width / 2, src_height / 2);

        g2.drawImage(src, null, null);
        return res;
    }

    public static Rectangle CalcRotatedSize(Rectangle src, int angel) {
        // if angel is greater than 90 degree, we need to do some conversion
        if (angel >= 90) {
            if(angel / 90 % 2 == 1){
                int temp = src.height;
                src.height = src.width;
                src.width = temp;
            }
            angel = angel % 90;
        }

        double r = Math.sqrt(src.height * src.height + src.width * src.width) / 2;
        double len = 2 * Math.sin(Math.toRadians(angel) / 2) * r;
        double angel_alpha = (Math.PI - Math.toRadians(angel)) / 2;
        double angel_dalta_width = Math.atan((double) src.height / src.width);
        double angel_dalta_height = Math.atan((double) src.width / src.height);

        int len_dalta_width = (int) (len * Math.cos(Math.PI - angel_alpha
                - angel_dalta_width));
        int len_dalta_height = (int) (len * Math.cos(Math.PI - angel_alpha
                - angel_dalta_height));
        int des_width = src.width + len_dalta_width * 2;
        int des_height = src.height + len_dalta_height * 2;
        return new Rectangle(new Dimension(des_width, des_height));
    }
}

 

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics