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í)間線(xiàn)也是提取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. python實(shí)現(xiàn)讀取類(lèi)別頻數(shù)數(shù)據(jù)畫(huà)水平條形圖案例2. PHP獲取時(shí)間戳等相關(guān)函數(shù)匯總3. ASP.NET MVC前臺(tái)動(dòng)態(tài)添加文本框并在后臺(tái)使用FormCollection接收值4. 如何基于python3和Vue實(shí)現(xiàn)AES數(shù)據(jù)加密5. Python編寫(xiě)nmap掃描工具6. python 如何停止一個(gè)死循環(huán)的線(xiàn)程7. Java 基于UDP協(xié)議實(shí)現(xiàn)消息發(fā)送8. 關(guān)于HTML5的img標(biāo)簽9. php5.6不能擴(kuò)展redis.so的解決方法10. python 爬取嗶哩嗶哩up主信息和投稿視頻
