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信息 */ public class ExifTester { public static void main(String[] args) throws Exception { File jpegFile = new File("E:\\图片\\2.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); } } }
需要jar包:metadata-extractor-2.3.0
运行结果 [Exif] Thumbnail Image Width - 2896 pixels [Exif] Thumbnail Image Height - 1944 pixels [Exif] Bits Per Sample - 8 8 8 bits/component/pixel [Exif] Make - NIKON CORPORATION [Exif] Model - NIKON D200 [Exif] Orientation - Top, left side (Horizontal / normal) [Exif] Samples Per Pixel - 3 samples/pixel [Exif] X Resolution - 300 dots per inch [Exif] Y Resolution - 300 dots per inch [Exif] Resolution Unit - Inch [Exif] Software - Adobe Photoshop CC (Windows) [Exif] Date/Time - 2014:05:04 13:17:26 [Exif] YCbCr Positioning - Datum point [Exif] Exposure Time - 1/640 sec [Exif] F-Number - F4.5 [Exif] Exposure Program - Aperture priority [Exif] ISO Speed Ratings - 125 [Exif] Exif Version - 2.21 [Exif] Date/Time Original - 2014:05:02 15:00:40 [Exif] Date/Time Digitized - 2014:05:02 15:00:40 [Exif] Components Configuration - YCbCr [Exif] Compressed Bits Per Pixel - 4 bits/pixel [Exif] Shutter Speed Value - 1/639 sec [Exif] Aperture Value - F4.5 [Exif] Exposure Bias Value - 2/3 EV [Exif] Max Aperture Value - F4.4 [Exif] Metering Mode - Center weighted average [Exif] Light Source - Unknown [Exif] Flash - Flash did not fire [Exif] Focal Length - 70.0 mm [Exif] User Comment - XCB [Exif] Sub-Sec Time - 90 [Exif] Sub-Sec Time Original - 90 [Exif] Sub-Sec Time Digitized - 90 [Exif] FlashPix Version - 1.00 [Exif] Color Space - sRGB [Exif] Exif Image Width - 1944 pixels [Exif] Exif Image Height - 1944 pixels [Exif] Sensing Method - One-chip color area sensor [Exif] File Source - Digital Still Camera (DSC) [Exif] Scene Type - Directly photographed image [Exif] CFA Pattern - 0 2 0 2 1 0 2 1 [Exif] Custom Rendered - Normal process [Exif] Exposure Mode - Auto exposure [Exif] White Balance - Auto white balance [Exif] Digital Zoom Ratio - 1 [Exif] Focal Length 35 - 1mm [Exif] Scene Capture Type - Standard [Exif] Gain Control - None [Exif] Contrast - None [Exif] Saturation - High saturation [Exif] Sharpness - Hard [Exif] Subject Distance Range - Unknown
获取需要的数据 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.exif.ExifDirectory; /** * 测试用于读取图片的EXIF信息 * @author Winter Lau */ public class PicExif { public static void main(String[] args) throws Exception { File jpegFile = new File( "E:\\图片\\2.JPG"); Metadata metadata = JpegMetadataReader.readMetadata(jpegFile); Directory exif = metadata.getDirectory(ExifDirectory.class); Iterator tags = exif.getTagIterator(); // while(tags.hasNext()){ // System.out.println(tags.next()); // Iterator s = exif.getTagIterator(); // while(s.hasNext()){ // System.out.println(s.next()); // } // } if(exif.containsTag(ExifDirectory.TAG_MODEL)){ System.out.println("相机型号:"+exif.getDescription(ExifDirectory.TAG_MODEL)); } if(exif.containsTag(ExifDirectory.TAG_EXPOSURE_TIME)){ System.out.println("曝光时间:"+exif.getDescription(ExifDirectory.TAG_EXPOSURE_TIME)); } if(exif.containsTag(ExifDirectory.TAG_DATETIME_ORIGINAL)){ System.out.println("拍摄时间:"+exif.getDescription(ExifDirectory.TAG_DATETIME_ORIGINAL)); } if(exif.containsTag(ExifDirectory.TAG_APERTURE)){ System.out.println("光圈值:"+exif.getDescription(ExifDirectory.TAG_APERTURE)); } if(exif.containsTag(ExifDirectory.TAG_FOCAL_LENGTH)){ System.out.println("焦距:"+exif.getDescription(ExifDirectory.TAG_FOCAL_LENGTH)); } if(exif.containsTag(ExifDirectory.TAG_THUMBNAIL_IMAGE_HEIGHT)){ System.out.println("图片尺寸:"+exif.getDescription(ExifDirectory.TAG_THUMBNAIL_IMAGE_HEIGHT)); } } }
运行结果: 相机型号:NIKON D200 曝光时间:1/640 sec 拍摄时间:2014:05:02 15:00:40 光圈值:F4.5 焦距:70.0 mm 图片尺寸:1944 pixels
时间: 2024-10-06 05:06:43