前言
在做项目中使用到OSS存储手机上传到服务器的图片, 但发现使用IOS手机进行竖着拍出的照片上传到OSS后出现了旋转的情况, 为了解决这个问题, 总结出一个工具类, 需要时也就是在将图片上传到OSS之前执行该工具类对图片进行旋转调整(注意该工具类执行一定要压缩图片之前, 否则图片信息丢失将无法旋转), 这样上传到OSS的图片就是正常的
gradle引用
// https://mvnrepository.com/artifact/com.drewnoakes/metadata-extractor
compile group: 'com.drewnoakes', name: 'metadata-extractor', version: '2.11.0'
工具类
package com.welsee.tools;
import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.exif.ExifDirectoryBase;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
/**
* Created by 李新宇
* 2019-05-17 11:33
*/
public class RotateImageUtil {
// 处理ios图片旋转的问题
public static void getPictureByName(String filePath, String name) {
try {
//name为前端请求图片名,如 a.jpg
BufferedImage src = getPicture(filePath + name);
BufferedImage bi = null;
//图片存在
if (src != null) {
//获取图片旋转角度
int angel = getRotateAngleForPhoto(filePath + name);
//图片被翻转,调整图片
if (angel != 0) {
int src_width = src.getWidth(null);
int src_height = src.getHeight(null);
Rectangle rect_des = CalcRotatedSize(new Rectangle(new Dimension(src_width, src_height)), angel);
bi = new BufferedImage(rect_des.width, rect_des.height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi.createGraphics();
// transform(这里先平移、再旋转比较方便处理;绘图时会采用这些变化,绘图默认从画布的左上顶点开始绘画,源图片的左上顶点与画布左上顶点对齐,然后开始绘画,修改坐标原点后,绘画对应的画布起始点改变,起到平移的效果;然后旋转图片即可)
//平移(原理修改坐标系原点,绘图起点变了,起到了平移的效果,如果作用于旋转,则为旋转中心点)
g2.translate((rect_des.width - src_width) / 2, (rect_des.height - src_height) / 2);
//旋转(原理translate(dx,dy)->rotate(radians)->translate(-dx,-dy);修改坐标系原点后,旋转90度,然后再还原坐标系原点为(0,0),但是整个坐标系已经旋转了相应的度数 )
g2.rotate(Math.toRadians(angel), src_width / 2, src_height / 2);
g2.drawImage(src, null, null);
int index = name.lastIndexOf(".");
String format = name.substring(index + 1);
ImageIO.write(bi, format, new File(filePath + name));
}
} else {
//图片不存在
System.out.println("图片不存在");
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 读取指定图片
*/
public static BufferedImage getPicture(String path) {
BufferedImage bi = null;
try {
File file = new File(path);
if (!file.exists()) {
return null;
}
bi = ImageIO.read(file);
} catch (Exception e) {
e.printStackTrace();
}
return bi;
}
/**
* 图片翻转时,计算图片翻转到正常显示需旋转角度
*/
public static int getRotateAngleForPhoto(String fileName) {
File file = new File(fileName);
int angel = 0;
Metadata metadata;
try {
metadata = JpegMetadataReader.readMetadata(file);
Directory directory = metadata.getFirstDirectoryOfType(ExifDirectoryBase.class);
if (directory != null && directory.containsTag(ExifDirectoryBase.TAG_ORIENTATION)) {
// Exif信息中方向
int orientation = directory.getInt(ExifDirectoryBase.TAG_ORIENTATION);
// 原图片的方向信息
if (6 == orientation) {
//6旋转90
angel = 90;
} else if (3 == orientation) {
//3旋转180
angel = 180;
} else if (8 == orientation) {
//8旋转90
angel = 270;
}
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("图片旋转角度:" + angel);
return angel;
}
/**
* 计算旋转参数
*/
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_data_width = Math.atan((double) src.height / src.width);
double angel_data_height = Math.atan((double) src.width / src.height);
int len_data_width = (int) (len * Math.cos(Math.PI - angel_alpha - angel_data_width));
int len_data_height = (int) (len * Math.cos(Math.PI - angel_alpha - angel_data_height));
int des_width = src.width + len_data_width * 2;
int des_height = src.height + len_data_height * 2;
return new java.awt.Rectangle(new Dimension(des_width, des_height));
}
public static void main(String[] args) {
String filePath = "/Users/lixinyu/Downloads/";
String picOne = "方向6.jpeg";
String picTwo = "方向8.jpeg";
getPictureByName(filePath, picOne);
getPictureByName(filePath, picTwo);
}
}
执行main测试
图片旋转角度:90
图片旋转角度:270
Process finished with exit code 0