Java 提取照片的EXIF信息批量重命名
手機(jī)或照機(jī)拍攝的照片名稱通常是”IMG_001.JPG”這種格式,這種文件名稱是無意義的。使用照片拍攝時(shí)間命名可以讓我們在多年以后查找照片時(shí)根據(jù)文件名就能快速篩選出某一時(shí)間段的照片。
原始照片或視頻是帶有EXIF信息的。這些信息是設(shè)備在拍攝時(shí)生成,記錄了照片的拍攝時(shí)間,設(shè)備信息,拍攝GPS位置等信息,在文件屬性中可以查看到:
圖片APP和網(wǎng)盤軟件中圖片時(shí)間線也是提取EXIF信息生成的。如果對照片進(jìn)行處理,如美化操作,另存為時(shí)可能會丟失EXIF信息,或者EXIF信息被改寫,會導(dǎo)致識別信息不準(zhǔn)。
我以前備份的照片,大多是原始文件名,現(xiàn)在我想根據(jù)拍攝日期批量重命名。
找了一圈,發(fā)現(xiàn)老牌看圖軟件ADSee帶有這個(gè)功能:
但是存在幾個(gè)問題:
不能排除已丟失EXIF的文件,這類的文件無法重命名 官方ADSee免費(fèi)版下載安裝后,要注冊賬號才能使用于是動(dòng)動(dòng)手,用JAVA代碼實(shí)現(xiàn)這個(gè)小功能。
提取EXIF信息使用的是開源項(xiàng)目 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í)加上一個(gè)自增數(shù)。
以上就是Java 提取照片的EXIF信息批量重命名的詳細(xì)內(nèi)容,更多關(guān)于Java 提取EXIF信息重命名的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
