圖片的EXIF 訊息內包含了相機資訊,光圈、焦距 等等訊息。
可以用com.drew.imaging.ImageMetadataReader 讀取圖片檔案內的 EXIF 訊息,以便拿來分析。
為了分析鏡頭到底哪個焦段最常使用,寫了個小工具…歡迎自行取用
以下為主要的程式碼,拿來取得圖片中的 焦距 資訊
public String getFocusInfo(File imgFile) {
Metadata metadata;
try {
metadata = ImageMetadataReader.readMetadata(imgFile);
Iterable<Directory> directories = metadata.getDirectories();
for (Directory directory : directories) {
if(!"Exif SubIFD".equals(directory.getName())) {
continue;
}
Collection<Tag> tags = directory.getTags();
for (Tag tag : tags) {
int tagType = tag.getTagType();
String tagName = tag.getTagName();
String description = tag.getDescription();
if("Focal Length".equals(tagName)) {
return description;
}
}
}
} catch (ImageProcessingException e) {
System.out.println("未知檔案格式:" + imgFile.getPath() + "/" + imgFile.getName());
e.printStackTrace();
} catch (IOException e) {
System.out.println("未知錯誤:" + imgFile.getPath() + "/" + imgFile.getName());
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
當然同樣的方法可以取得所有圖片中的EXIF訊息。
if(“Focal Length”.equals(tagName)) { 這個判斷式拿掉即可跑完全部的項目。
附上maven 倉儲的pom.xml 內容
<dependency>
<groupId>com.drewnoakes</groupId>
<artifactId>metadata-extractor</artifactId>
<version>2.18.0</version>
</dependency>