您好,登錄后才能下訂單哦!
引入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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。