Java讀取圖片EXIF信息的辦法。本站提示廣大學習愛好者:(Java讀取圖片EXIF信息的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是Java讀取圖片EXIF信息的辦法正文
本文實例講述了Java讀取圖片EXIF信息的辦法。分享給年夜家供年夜家參考。詳細剖析以下:
起首引見一下甚麼是EXIF,EXIF是Exchangeable Image File的縮寫,這是一種專門為數碼相機照片設定的格局。這類格局可以用來記載數字照片的屬性信息,例如相機的品牌及型號、相片的拍攝時光、拍攝時所設置 的光圈年夜小、快門速度、ISO等等信息。除此以外它還可以或許記載拍攝數據,和照片格局化方法,如許便可以輸入到兼容EXIF格局的外設上,例如照片打印機 等。
今朝最多見的支撐EXIF信息的圖片格局是JPG,許多的圖象對象都可以直接顯示圖片的EXIF信息,包含如今的一些有名的相冊網站也供給頁面用於 顯示照片的EXIF信息。本文重要引見Java說話若何讀取圖象的EXIF信息,包含若何依據EXIF信息對圖象停止調劑以合適用戶閱讀。
今朝最簡略易用的EXIF信息處置的Java包是Drew Noakes寫的metadata-extractor,該項目最新的版本是2.3.4,支撐EXIF 2.2版本。你可以直接從http://www.drewnoakes.com/code/exif/ 下載該項目標最新版本包含其源碼。
須要留意的是,其實不是每一個JPG圖象文件都包括有EXIF信息,你可以在Windows資本治理器單擊選中圖片後,假如該圖片包括EXIF信息,則會在屬性->摘要中顯示出來。
ExifTester.java以下:
import java.io.File; import java.util.Iterator; import com.drew.imaging.jpeg.JpegMetadataReader; import com.drew.metadata.Directory; import com.drew.metadata.Metadata; import com.drew.metadata.Tag; import com.drew.metadata.exif.ExifDirectory; /** * 測試用於讀取圖片的EXIF信息 * @author Winter Lau */ public class ExifTester { public static void main(String[] args) throws Exception { File jpegFile = new File("C:/1.JPG"); Metadata metadata = JpegMetadataReader.readMetadata(jpegFile); Directory exif = metadata.getDirectory(ExifDirectory.class); Iterator tags = exif.getTagIterator(); while (tags.hasNext()) { Tag tag = (Tag)tags.next(); System.out.println(tag); } } }
運轉成果:
[Exif] Make - OLYMPUS OPTICAL CO.,LTD [Exif] Model - u10D,S300D,u300D [Exif] Orientation - Top, left side (Horizontal / normal) [Exif] X Resolution - 72 dots per inch [Exif] Y Resolution - 72 dots per inch [Exif] Resolution Unit - Inch [Exif] Software - 22-1012 [Exif] Date/Time - 2005:04:14 13:47:10 [Exif] YCbCr Positioning - Datum point [Exif] Exposure Time - 0.01 sec [Exif] F-Number - F5.2 [Exif] Exposure Program - Program creative (slow program) [Exif] ISO Speed Ratings - 80 [Exif] Exif Version - 2.20 [Exif] Date/Time Original - 2005:04:14 13:47:10 [Exif] Date/Time Digitized - 2005:04:14 13:47:10 [Exif] Components Configuration - YCbCr [Exif] Exposure Bias Value - 0 EV [Exif] Max Aperture Value - F3.1 [Exif] Metering Mode - Multi-segment [Exif] Light Source - Unknown [Exif] Flash - Flash did not fire, auto [Exif] Focal Length - 17.4 mm [Exif] User Comment - [Exif] FlashPix Version - 1.00 [Exif] Color Space - sRGB [Exif] Exif Image Width - 1024 pixels [Exif] Exif Image Height - 768 pixels [Exif] File Source - Digital Still Camera (DSC) [Exif] Windows XP Title - 景致 [Exif] Windows XP Author - 一路風塵 [Exif] Windows XP Keywords - 你是我的獨一 [Exif] Windows XP Subject - 我的第一張 [Exif] Custom Rendered - Normal process [Exif] Exposure Mode - Auto exposure [Exif] White Balance - Auto white balance [Exif] Digital Zoom Ratio - 1 [Exif] Scene Capture Type - Landscape [Exif] Gain Control - None [Exif] Contrast - None [Exif] Saturation - None [Exif] Sharpness - None [Exif] Unknown tag (0xc4a5) - 80 114 105 110 116 73 77 0 480 2 -10... [Exif] Compression - JPEG (old-style) [Exif] Thumbnail Offset - 2022 bytes [Exif] Thumbnail Length - 5864 bytes [Exif] Thumbnail Data - [5864 bytes of thumbnail data]
只讀取某項信息:
package test; import java.io.File; import java.util.Iterator; import com.drew.imaging.jpeg.JpegMetadataReader; import com.drew.metadata.Directory; import com.drew.metadata.Metadata; import com.drew.metadata.Tag; import com.drew.metadata.exif.ExifDirectory; /** * 測試用於讀取圖片的EXIF信息 * @author Winter Lau */ public class PicExif { public static void main(String[] args) throws Exception { File jpegFile = new File( "C:/1.JPG"); Metadata metadata = JpegMetadataReader.readMetadata(jpegFile); Directory exif = metadata.getDirectory(ExifDirectory.class); Iterator tags = exif.getTagIterator(); if(exif.containsTag(ExifDirectory.TAG_WIN_AUTHOR)){ System.out.println("Pic author is "+exif.getDescription(ExifDirectory.TAG_WIN_AUTHOR)); } if(exif.containsTag(ExifDirectory.TAG_WIN_TITLE)){ System.out.println("Pic title is "+exif.getDescription(ExifDirectory.TAG_WIN_TITLE)); } if(exif.containsTag(ExifDirectory.TAG_WIN_KEYWORDS)){ System.out.println("Pic keyword is "+exif.getDescription(ExifDirectory.TAG_WIN_KEYWORDS)); } } }
願望本文所述對年夜家的java法式設計有所贊助。