Java 提取照片的EXIF信息批量重命名
手機(jī)或照機(jī)拍攝的照片名稱(chēng)通常是”IMG_001.JPG”這種格式,這種文件名稱(chēng)是無(wú)意義的。使用照片拍攝時(shí)間命名可以讓我們?cè)诙嗄暌院蟛檎艺掌瑫r(shí)根據(jù)文件名就能快速篩選出某一時(shí)間段的照片。
原始照片或視頻是帶有EXIF信息的。這些信息是設(shè)備在拍攝時(shí)生成,記錄了照片的拍攝時(shí)間,設(shè)備信息,拍攝GPS位置等信息,在文件屬性中可以查看到:
圖片APP和網(wǎng)盤(pán)軟件中圖片時(shí)間線也是提取EXIF信息生成的。如果對(duì)照片進(jìn)行處理,如美化操作,另存為時(shí)可能會(huì)丟失EXIF信息,或者EXIF信息被改寫(xiě),會(huì)導(dǎo)致識(shí)別信息不準(zhǔn)。
我以前備份的照片,大多是原始文件名,現(xiàn)在我想根據(jù)拍攝日期批量重命名。
找了一圈,發(fā)現(xiàn)老牌看圖軟件ADSee帶有這個(gè)功能:
但是存在幾個(gè)問(wèn)題:
不能排除已丟失EXIF的文件,這類(lèi)的文件無(wú)法重命名 官方ADSee免費(fèi)版下載安裝后,要注冊(cè)賬號(hào)才能使用于是動(dòng)動(dòng)手,用JAVA代碼實(shí)現(xiàn)這個(gè)小功能。
提取EXIF信息使用的是開(kāi)源項(xiàng)目 metadata extractor ,它支持市面上常見(jiàn)的媒體文件格式和設(shè)備:
metadata extractor 官網(wǎng):https://drewnoakes.com/code/exif/
引入依賴(lài):
<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方法測(cè)試:
public static void main(String[] args) throws ImageProcessingException, IOException { recursion(new File('圖片目錄'));}
執(zhí)行結(jié)果:
可以根據(jù)自己需求重寫(xiě)重命名方法。比如在拍攝日期相同時(shí)加上一個(gè)自增數(shù)。
以上就是Java 提取照片的EXIF信息批量重命名的詳細(xì)內(nèi)容,更多關(guān)于Java 提取EXIF信息重命名的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. xml中的空格之完全解說(shuō)2. ASP中解決“對(duì)象關(guān)閉時(shí),不允許操作。”的詭異問(wèn)題……3. msxml3.dll 錯(cuò)誤 800c0019 系統(tǒng)錯(cuò)誤:-2146697191解決方法4. html小技巧之td,div標(biāo)簽里內(nèi)容不換行5. XML入門(mén)的常見(jiàn)問(wèn)題(四)6. ASP中if語(yǔ)句、select 、while循環(huán)的使用方法7. 匹配模式 - XSL教程 - 48. 解決ASP中http狀態(tài)跳轉(zhuǎn)返回錯(cuò)誤頁(yè)的問(wèn)題9. WML語(yǔ)言的基本情況10. CSS3中Transition屬性詳解以及示例分享
