Java 提取照片的EXIF信息批量重命名
手機(jī)或照機(jī)拍攝的照片名稱通常是”IMG_001.JPG”這種格式,這種文件名稱是無意義的。使用照片拍攝時間命名可以讓我們在多年以后查找照片時根據(jù)文件名就能快速篩選出某一時間段的照片。
原始照片或視頻是帶有EXIF信息的。這些信息是設(shè)備在拍攝時生成,記錄了照片的拍攝時間,設(shè)備信息,拍攝GPS位置等信息,在文件屬性中可以查看到:
圖片APP和網(wǎng)盤軟件中圖片時間線也是提取EXIF信息生成的。如果對照片進(jìn)行處理,如美化操作,另存為時可能會丟失EXIF信息,或者EXIF信息被改寫,會導(dǎo)致識別信息不準(zhǔn)。
我以前備份的照片,大多是原始文件名,現(xiàn)在我想根據(jù)拍攝日期批量重命名。
找了一圈,發(fā)現(xiàn)老牌看圖軟件ADSee帶有這個功能:
但是存在幾個問題:
- 不能排除已丟失EXIF的文件,這類的文件無法重命名
- 官方ADSee免費(fèi)版下載安裝后,要注冊賬號才能使用
于是動動手,用JAVA代碼實(shí)現(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信息批量重命名的詳細(xì)內(nèi)容,更多關(guān)于Java 提取EXIF信息重命名的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
mybatis多個接口參數(shù)的注解使用方式(@Param)
這篇文章主要介紹了mybatis多個接口參數(shù)的注解使用方式(@Param),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-10crawler4j抓取頁面使用jsoup解析html時的解決方法
crawler4j對response沒有指定編碼的頁面,解析成亂碼,很讓人煩惱,下面給出解決方法,需要的朋友可以參考下2014-04-04解決SpringMVC、tomcat、Intellij idea、ajax中文亂碼問題
這篇文章主要介紹了解決SpringMVC、tomcat、Intellij idea、ajax中文亂碼問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09java static塊和構(gòu)造函數(shù)的實(shí)例詳解
這篇文章主要介紹了java static塊和構(gòu)造函數(shù)的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握J(rèn)ava static關(guān)鍵字的函數(shù)方法,需要的朋友可以參考下2017-09-09Java GUI進(jìn)階之流式布局管理器FlowLayout專項精講
FlowLayout-流式布局管理器,按水平方向依次排列放置組件,排滿一行,換下一行繼續(xù)排列。排列方向(左到右 或 右到左)取決于容器的componentOrientation屬性2022-04-04