去下面的網址下載metadata-extractor-2.6.2.jar和xmpcore-5.1.2.jar兩個jar包:
https://mvnrepository.com/artifact/com.drewnoakes/metadata-extractor/2.6.2
https://mvnrepository.com/artifact/com.adobe.xmp/xmpcore/5.1.2
經測試,這兩個jar包都支持Java 6。下載後導入eclipse項目。
【測試代碼】
package test;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.MetadataException;
import com.drew.metadata.Tag;
public class JPGTest {
public static void main(String[] args) {
test("E:\\用戶的文檔\\Octopus\\Documents\\WeChat Files\\wxid_z90coebc6md122\\FileStorage\\File\\2024-10\\IMG_20240921_122601_1\\IMG_20240921_122601_1.jpg");
}
private static void test(String filename) {
try {
File file = new File(filename);
Metadata metadata = ImageMetadataReader.readMetadata(file);
Iterable<Directory> directories = metadata.getDirectories();
for (Directory directory : directories) {
String name = directory.getName();
if (name.equals("Exif IFD0")) {
Collection<Tag> tags = directory.getTags();
for (Tag tag : tags) {
name = tag.getTagName();
if (name.equals("Orientation")) {
int type = tag.getTagType();
System.out.println("Tag type: " + type);
int value = directory.getInt(type);
System.out.println("Tag value: " + value);
String desc = tag.getDescription();
System.out.println("Tag description: " + desc);
}
}
}
}
} catch (JpegProcessingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ImageProcessingException e) {
e.printStackTrace();
} catch (MetadataException e) {
e.printStackTrace();
}
}
}
【輸出結果】
Tag type: 274
Tag value: 6
Tag description: Right side, top (Rotate 90 CW)
CW代表順時針方向旋轉。 CW是clockwise的縮寫,表示順時針旋轉。
CCW代表逆時針方向旋轉。 CCW是counterclockwise的縮寫,表示逆時針旋轉。
Rotate 90 CW表示順時針旋轉90度。