Java 提取照片的EXIF信息批量重命名
手機或照機拍攝的照片名稱通常是”IMG_001.JPG”這種格式,這種文件名稱是無意義的。使用照片拍攝時間命名可以讓我們在多年以后查找照片時根據(jù)文件名就能快速篩選出某一時間段的照片。
原始照片或視頻是帶有EXIF信息的。這些信息是設(shè)備在拍攝時生成,記錄了照片的拍攝時間,設(shè)備信息,拍攝GPS位置等信息,在文件屬性中可以查看到:

圖片APP和網(wǎng)盤軟件中圖片時間線也是提取EXIF信息生成的。如果對照片進行處理,如美化操作,另存為時可能會丟失EXIF信息,或者EXIF信息被改寫,會導(dǎo)致識別信息不準(zhǔn)。
我以前備份的照片,大多是原始文件名,現(xiàn)在我想根據(jù)拍攝日期批量重命名。
找了一圈,發(fā)現(xiàn)老牌看圖軟件ADSee帶有這個功能:

但是存在幾個問題:
- 不能排除已丟失EXIF的文件,這類的文件無法重命名
- 官方ADSee免費版下載安裝后,要注冊賬號才能使用
于是動動手,用JAVA代碼實現(xiàn)這個小功能。
提取EXIF信息使用的是開源項目 metadata extractor ,它支持市面上常見的媒體文件格式和設(shè)備:

metadata extractor 官網(wǎng):https://drewnoakes.com/code/exif/
引入依賴:
<dependency> <groupId>com.drewnoakes</groupId> <artifactId>metadata-extractor</artifactId> <version>2.15.0</version> </dependency>
官方讀取示例代碼:
Metadata metadata = ImageMetadataReader.readMetadata(file);
for (Directory directory : metadata.getDirectories()) {
for (Tag tag : directory.getTags()) {
System.out.format("[%s] - %s = %s \n",
directory.getName(), tag.getTagName(), tag.getDescription());
}
if (directory.hasErrors()) {
for (String error : directory.getErrors()) {
System.err.format("ERROR: %s", error);
}
}
}
以下是我使用示例代碼讀取一張圖片輸出的部分結(jié)果:

其中 Date/Time Original 就是我要取的攝像日期。
代碼如下:
/**
* 如果是目錄則遞歸查找
* @param file 文件或目錄
*/
public static void recursion(File file) {
if (file.isDirectory()) {
// 目錄
File[] fileList = file.listFiles();
for (File f : fileList) {
recursion(f);
}
} else {
// 文件
if (file.isFile()) {
// 格式:2019:06:27 11:23:55 或 2019:07:13 19:07:42下午
String originDateTime = getOriginDateTime(file);
if (null != originDateTime) {
int lastDoc = file.getPath().lastIndexOf(".");
String suffix = file.getPath().substring(lastDoc);
String fileName = originDateTime.replace("下午", "").replaceAll(":", "-") + suffix;
File newFile = new File(file.getParentFile(), fileName);
if (newFile.exists()) {
System.out.format("文件【%s】已存在 \n", newFile.getPath());
} else {
System.out.format("重命名【%s】 -> 【%s】 \n", file.getPath(), newFile.getPath());
file.renameTo(newFile);
}
} else {
System.out.format("文件【%s】中未找到 Origin DateTime 信息 \n", file.getPath());
}
}
}
}
/**
* 提取拍攝日期
* @param file
* @return
*/
public static String getOriginDateTime(File file) {
String originDateTime = null;
try {
Metadata metadata = ImageMetadataReader.readMetadata(file);
for (Directory directory : metadata.getDirectories()) {
for (Tag tag : directory.getTags()) {
if ("Date/Time Original".equals(tag.getTagName())) {
// System.out.format("[%s] - %s = %s \n",
// directory.getName(), tag.getTagName(), tag.getDescription());
originDateTime = tag.getDescription();
}
}
if (directory.hasErrors()) {
for (String error : directory.getErrors()) {
System.err.format("ERROR: %s %s \n", error, file.getPath());
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return originDateTime;
}
Main方法測試:
public static void main(String[] args) throws ImageProcessingException, IOException {
recursion(new File("圖片目錄"));
}
執(zhí)行結(jié)果:

可以根據(jù)自己需求重寫重命名方法。比如在拍攝日期相同時加上一個自增數(shù)。
以上就是Java 提取照片的EXIF信息批量重命名的詳細內(nèi)容,更多關(guān)于Java 提取EXIF信息重命名的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
springboot如何接收復(fù)雜參數(shù)(同時接收JSON與文件)
文章介紹了在Spring Boot中同時處理JSON和文件上傳時使用`@RequestPart`注解的方法,`@RequestPart`可以接收多種格式的參數(shù),包括JSON和文件,并且可以作為`multipart/form-data`格式中的key2025-02-02
Java并發(fā)系列之AbstractQueuedSynchronizer源碼分析(共享模式)
這篇文章主要為大家詳細介紹了Java并發(fā)系列之AbstractQueuedSynchronizer源碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-02-02

