引入maven
<dependency>
<groupId>com.drewnoakes</groupId>
<artifactId>metadata-extractor</artifactId>
<version>2.11.0</version>
</dependency>
创建工具类:
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<Directory> iterable = metadata.getDirectories();
for (Iterator<Directory> iter = iterable.iterator(); iter.hasNext(); ) {
Directory dr = iter.next();
if (dr.getString(ExifIFD0Directory.TAG_ORIENTATION) != null) {
orientation = dr.getInt(ExifIFD0Directory.TAG_ORIENTATION);
}
Collection<Tag> 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;
}
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。