读取图片信息(exif),使用com.drew.metadata.Metadata
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("C:/test.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 - 2012:13:12 12:37:12 [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 - 2012:13:12 12:37:12 [Exif] Date/Time Digitized - 2012:13:12 12:37:12 [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信息
*/
public class PicExif {
public static void main(String[] args) throws Exception {
File jpegFile = new File(
"C:/test.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));
}
}
}
API:
官方地址:
