引入maven

com.drewnoakes
metadata-extractor
2.11.0

创建工具类:

public class ImageUtil {

public static int getRotateAngleForPhoto(File file) {
int angel = 0;
try {
//核心对象操作对象
Metadata metadata = ImageMetadataReader.readMetadata(file);
//获取所有不同类型的Directory,如ExifSubIFDDirectory, ExifInteropDirectory, ExifThumbnailDirectory等,这些类均为ExifDirectoryBase extends Directory子类
//分别遍历每一个Directory,根据Directory的Tags就可以读取到相应的信息
int orientation = 0;
Iterable iterable = metadata.getDirectories();
for (Iterator iter = iterable.iterator(); iter.hasNext(); ) {
Directory dr = iter.next();
if (dr.getString(ExifIFD0Directory.TAG_ORIENTATION) != null) {
orientation = dr.getInt(ExifIFD0Directory.TAG_ORIENTATION);
}
Collection tags = dr.getTags();
for (Tag tag : tags) {
System.out.println(tag.getTagName() + ": " + tag.getDescription());
}

  }
  if (orientation == 0 || orientation == 1) {
    angel = 360;
  } else if (orientation == 3) {
    angel = 180;
  } else if (orientation == 6) {
    angel = 90;
  } else if (orientation == 8) {
    angel = 270;
  }
} catch (Exception e) {
  e.printStackTrace();
}
return angel;

}
}