Android操作Exif-ExifInterface全解析

簡介:

Exif是一種圖像文件格式,它的數據存儲與JPEG格式是完全相同的。實際上Exif格式就是在JPEG格式頭部插入了數碼照片的信息,包括拍攝時的光圈、快門、白平衡、ISO、焦距、日期時間等各種和拍攝條件以及相機品牌、型號、色彩編碼、拍攝時錄製的聲音以及GPS全球定位系統數據、縮略圖等。你可以利用任何可以查看JPEG文件的看圖軟件瀏覽Exif格式的照片,但並不是所有的圖形程序都能處理Exif信息。今天這篇文章就來講講Android中操作Exif。

段子:

老規矩,在節目開始之前,先來一個搞笑段子:
我:老闆,來30斤肉。
老闆:好嘞。
我:30斤這麼多啊!
老闆:來,我給您裝上。
我:哦不用了,我就想看看30斤肉有多少,我想減肥。
老闆:窩草,你別跑~

ExifInterface:

Android開發中,在對圖片進行展示、編輯、發送等操作時經常會涉及Exif的操作,Android中操作Exif主要是通過ExifInterface,ExifInterface看上去是一個接口,其實是一個類,位於Android.media.ExifInterface的位置。進入ExifInterface類,發現方法很少,主要就是三個方面:讀取、寫入、縮略圖。

一、讀取:

Exif信息在文件頭中是以二進制的形式存儲的,存儲的字段名稱和字段值格式都是固定的。我測試的Android23(6.0)版本中,總共有26個Exif字段,其中TAG_SUBSECTIME被加上了@hide註解,也就是還剩25個,我寫了個demo,獲取這25個字段的值,看看都是什麼樣的格式。

Demo:這個demo會對25個字段作解釋

ExifInterface exifInterface = new ExifInterface(filePath);

String orientation = exifInterface.getAttribute(ExifInterface.TAG_ORIENTATION);
String dateTime = exifInterface.getAttribute(ExifInterface.TAG_DATETIME);
String make = exifInterface.getAttribute(ExifInterface.TAG_MAKE);
String model = exifInterface.getAttribute(ExifInterface.TAG_MODEL);
String flash = exifInterface.getAttribute(ExifInterface.TAG_FLASH);
String imageLength = exifInterface.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);
String imageWidth = exifInterface.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);
String latitude = exifInterface.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
String longitude = exifInterface.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
String latitudeRef = exifInterface.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
String longitudeRef = exifInterface.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF);
String exposureTime = exifInterface.getAttribute(ExifInterface.TAG_EXPOSURE_TIME);
String aperture = exifInterface.getAttribute(ExifInterface.TAG_APERTURE);
String isoSpeedRatings = exifInterface.getAttribute(ExifInterface.TAG_ISO);
String dateTimeDigitized = exifInterface.getAttribute(ExifInterface.TAG_DATETIME_DIGITIZED);
String subSecTime = exifInterface.getAttribute(ExifInterface.TAG_SUBSEC_TIME);
String subSecTimeOrig = exifInterface.getAttribute(ExifInterface.TAG_SUBSEC_TIME_ORIG);
String subSecTimeDig = exifInterface.getAttribute(ExifInterface.TAG_SUBSEC_TIME_DIG);
String altitude = exifInterface.getAttribute(ExifInterface.TAG_GPS_ALTITUDE);
String altitudeRef = exifInterface.getAttribute(ExifInterface.TAG_GPS_ALTITUDE_REF);
String gpsTimeStamp = exifInterface.getAttribute(ExifInterface.TAG_GPS_TIMESTAMP);
String gpsDateStamp = exifInterface.getAttribute(ExifInterface.TAG_GPS_DATESTAMP);
String whiteBalance = exifInterface.getAttribute(ExifInterface.TAG_WHITE_BALANCE);
String focalLength = exifInterface.getAttribute(ExifInterface.TAG_FOCAL_LENGTH);
String processingMethod = exifInterface.getAttribute(ExifInterface.TAG_GPS_PROCESSING_METHOD);

Log.e("TAG", "## orientation=" + orientation);
Log.e("TAG", "## dateTime=" + dateTime);
Log.e("TAG", "## make=" + make);
Log.e("TAG", "## model=" + model);
Log.e("TAG", "## flash=" + flash);
Log.e("TAG", "## imageLength=" + imageLength);
Log.e("TAG", "## imageWidth=" + imageWidth);
Log.e("TAG", "## latitude=" + latitude);
Log.e("TAG", "## longitude=" + longitude);
Log.e("TAG", "## latitudeRef=" + latitudeRef);
Log.e("TAG", "## longitudeRef=" + longitudeRef);
Log.e("TAG", "## exposureTime=" + exposureTime);
Log.e("TAG", "## aperture=" + aperture);
Log.e("TAG", "## isoSpeedRatings=" + isoSpeedRatings);
Log.e("TAG", "## dateTimeDigitized=" + dateTimeDigitized);
Log.e("TAG", "## subSecTime=" + subSecTime);
Log.e("TAG", "## subSecTimeOrig=" + subSecTimeOrig);
Log.e("TAG", "## subSecTimeDig=" + subSecTimeDig);
Log.e("TAG", "## altitude=" + altitude);
Log.e("TAG", "## altitudeRef=" + altitudeRef);
Log.e("TAG", "## gpsTimeStamp=" + gpsTimeStamp);
Log.e("TAG", "## gpsDateStamp=" + gpsDateStamp);
Log.e("TAG", "## whiteBalance=" + whiteBalance);
Log.e("TAG", "## focalLength=" + focalLength);
Log.e("TAG", "## processingMethod=" + processingMethod);

打印結果如下:

E/TAG: ## orientation=0
E/TAG: ## dateTime=2016:05:23 17:30:11
E/TAG: ## make=Xiaomi
E/TAG: ## model=Mi-4c
E/TAG: ## flash=16
E/TAG: ## imageLength=4160
E/TAG: ## imageWidth=3120
E/TAG: ## latitude=31/1,58/1,253560/10000
E/TAG: ## longitude=118/1,44/1,491207/10000
E/TAG: ## latitudeRef=N
E/TAG: ## longitudeRef=E
E/TAG: ## exposureTime=0.050
E/TAG: ## aperture=2.0
E/TAG: ## iso=636
E/TAG: ## dateTimeDigitized=2016:05:23 17:30:11
E/TAG: ## subSecTime=379693
E/TAG: ## subSecTimeOrig=379693
E/TAG: ## subSecTimeDig=379693
E/TAG: ## altitude=0/1000
E/TAG: ## altitudeRef=0
E/TAG: ## gpsTimeStamp=9:30:8
E/TAG: ## gpsDateStamp=2016:05:23
E/TAG: ## whiteBalance=0
E/TAG: ## focalLength=412/100
E/TAG: ## processingMethod=NETWORK

這25個字段分別是代表什麼呢?

ExifInterface.TAG_ORIENTATION //旋轉角度,整形表示,在ExifInterface中有常量對應表示
ExifInterface.TAG_DATETIME //拍攝時間,取決於設備設置的時間
ExifInterface.TAG_MAKE //設備品牌
ExifInterface.TAG_MODEL //設備型號,整形表示,在ExifInterface中有常量對應表示
ExifInterface.TAG_FLASH //閃光燈
ExifInterface.TAG_IMAGE_LENGTH //圖片高度
ExifInterface.TAG_IMAGE_WIDTH //圖片寬度
ExifInterface.TAG_GPS_LATITUDE //緯度
ExifInterface.TAG_GPS_LONGITUDE //經度
ExifInterface.TAG_GPS_LATITUDE_REF //緯度名(N or S)
ExifInterface.TAG_GPS_LONGITUDE_REF //經度名(E or W)
ExifInterface.TAG_EXPOSURE_TIME //曝光時間
ExifInterface.TAG_APERTURE //光圈值
ExifInterface.TAG_ISO //ISO感光度
ExifInterface.TAG_DATETIME_DIGITIZED //數字化時間
ExifInterface.TAG_SUBSEC_TIME //
ExifInterface.TAG_SUBSEC_TIME_ORIG //
ExifInterface.TAG_SUBSEC_TIME_DIG //
ExifInterface.TAG_GPS_ALTITUDE //海拔高度
ExifInterface.TAG_GPS_ALTITUDE_REF //海拔高度
ExifInterface.TAG_GPS_TIMESTAMP //時間戳
ExifInterface.TAG_GPS_DATESTAMP //日期戳
ExifInterface.TAG_WHITE_BALANCE //白平衡
ExifInterface.TAG_FOCAL_LENGTH //焦距
ExifInterface.TAG_GPS_PROCESSING_METHOD //用於定位查找的全球定位系統處理方法。

其中TAG_SUBSEC_TIME 、TAG_SUBSEC_TIME_ORIG 、TAG_SUBSEC_TIME_DIG 沒有加註釋,我也沒查清楚具體是什麼意思,但是看log,三個值是一樣的。有知道的朋友可以跟我說下,謝謝!

細心的朋友會發現,上面所有的取值都是用的一個方法:exifInterface.getAttribute(String tag),其實ExifInterface還提供了其它方法。

exifInterface.getAltitude(long default); //返回海拔高度,單位米,如果exif的tag不存在,返回默認值。
exifInterface.getAttributeDouble(String tag, Double default) //返回double值,傳入默認值
exifInterface.getAttributeInt(String tag, int default) //返回int值,傳入默認值
exifInterface.getLatLong(float[] value) //返回緯度和經度,數組第一個是緯度,第二個是經度

二、寫入

相對讀取,寫入就簡單很多了。

ExifInterface exifInterface = new ExifInterface(filePath);
exifInterface.setAttribute(ExifInterface.TAG_GPS_ALTITUDE,"1/1000");
exifInterface.setAttribute(ExifInterface.TAG_ORIENTATION,"6");
exifInterface.setAttribute(ExifInterface.TAG_IMAGE_WIDTH,"2000");
exifInterface.saveAttributes();

代碼很簡答,但有一點需要說下,setAttributes只是設置屬性值,沒有保存,saveAttributes才真正保存,但是這個方法比較耗時,不要每set一次都save,全部set完後,再統一save一次。
有一點很尷尬,saveAttributes()這個方法內部會遍歷保存所有的值,哪怕你只改變了其中一個值。

三、縮略圖

getThumbnail()這個方法可以生成一個縮略圖,返回一個字節數組,得到字節數組就可以輕鬆生成Bitmap。

但是在調用這個方法前,最好先調用exifInterface.hasThumbnail()判斷一下是否有縮略圖。

getThumbnail()這個方法調用的是native方法,所以具體的實現就看不到了,我也不知道生成的縮略圖的分辨率是多少。。。

總結:

以上就是Android中操作Exif信息的全部內容,如果有更新我再修改。
本期節目就到這裏,感謝大家的收看,我們下期再見~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章