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信息,则会在属性->摘要中显示出来。
代码片段(3)
[代码] ExifTester.java
01 | import java.io.File; |
02 | import java.util.Iterator; |
03 | |
04 | import com.drew.imaging.jpeg.JpegMetadataReader; |
05 | import com.drew.metadata.Directory; |
06 | import com.drew.metadata.Metadata; |
07 | import com.drew.metadata.Tag; |
08 | import com.drew.metadata.exif.ExifDirectory; |
09 | |
10 | /** |
11 | * 测试用于读取图片的EXIF信息 |
12 | * @author Winter Lau |
13 | */ |
14 | public class ExifTester { |
15 | public static void main(String[] args) throws Exception { |
16 | File jpegFile = new File( "C:/1.JPG" ); |
17 | Metadata metadata = JpegMetadataReader.readMetadata(jpegFile); |
18 | Directory exif = metadata.getDirectory(ExifDirectory. class ); |
19 | Iterator tags = exif.getTagIterator(); |
20 | while (tags.hasNext()) { |
21 | Tag tag = (Tag)tags.next(); |
22 | System.out.println(tag); |
23 | } |
24 | } |
25 | } |
[代码] 运行结果
01 | [Exif] Make - OLYMPUS OPTICAL CO.,LTD |
02 | [Exif] Model - u10D,S300D,u300D |
03 | [Exif] Orientation - Top, left side (Horizontal / normal) |
04 | [Exif] X Resolution - 72 dots per inch |
05 | [Exif] Y Resolution - 72 dots per inch |
06 | [Exif] Resolution Unit - Inch |
07 | [Exif] Software - 22-1012 |
08 | [Exif] Date/Time - 2005:04:14 13:47:10 |
09 | [Exif] YCbCr Positioning - Datum point |
10 | [Exif] Exposure Time - 0.01 sec |
11 | [Exif] F-Number - F5.2 |
12 | [Exif] Exposure Program - Program creative (slow program) |
13 | [Exif] ISO Speed Ratings - 80 |
14 | [Exif] Exif Version - 2.20 |
15 | [Exif] Date/Time Original - 2005:04:14 13:47:10 |
16 | [Exif] Date/Time Digitized - 2005:04:14 13:47:10 |
17 | [Exif] Components Configuration - YCbCr |
18 | [Exif] Exposure Bias Value - 0 EV |
19 | [Exif] Max Aperture Value - F3.1 |
20 | [Exif] Metering Mode - Multi-segment |
21 | [Exif] Light Source - Unknown |
22 | [Exif] Flash - Flash did not fire, auto |
23 | [Exif] Focal Length - 17.4 mm |
24 | [Exif] User Comment - |
25 | [Exif] FlashPix Version - 1.00 |
26 | [Exif] Color Space - sRGB |
27 | [Exif] Exif Image Width - 1024 pixels |
28 | [Exif] Exif Image Height - 768 pixels |
29 | [Exif] File Source - Digital Still Camera (DSC) |
30 | [Exif] Windows XP Title - 风景 |
31 | [Exif] Windows XP Author - 一路风尘 |
32 | [Exif] Windows XP Keywords - 你是我的唯一 |
33 | [Exif] Windows XP Subject - 我的第一张 |
34 | [Exif] Custom Rendered - Normal process |
35 | [Exif] Exposure Mode - Auto exposure |
36 | [Exif] White Balance - Auto white balance |
37 | [Exif] Digital Zoom Ratio - 1 |
38 | [Exif] Scene Capture Type - Landscape |
39 | [Exif] Gain Control - None |
40 | [Exif] Contrast - None |
41 | [Exif] Saturation - None |
42 | [Exif] Sharpness - None |
43 | [Exif] Unknown tag (0xc4a5) - 80 114 105 110 116 73 77 0 480 2 -10... |
44 | [Exif] Compression - JPEG (old-style) |
45 | [Exif] Thumbnail Offset - 2022 bytes |
46 | [Exif] Thumbnail Length - 5864 bytes |
47 | [Exif] Thumbnail Data - [5864 bytes of thumbnail data] |
[代码] 只读取某项信息
01 | package test; |
02 |
03 | import java.io.File; |
04 | import java.util.Iterator; |
05 | |
06 | import com.drew.imaging.jpeg.JpegMetadataReader; |
07 | import com.drew.metadata.Directory; |
08 | import com.drew.metadata.Metadata; |
09 | import com.drew.metadata.Tag; |
10 | import com.drew.metadata.exif.ExifDirectory; |
11 | |
12 | /** |
13 | * 测试用于读取图片的EXIF信息 |
14 | * @author Winter Lau |
15 | */ |
16 | public class PicExif { |
17 | public static void main(String[] args) throws Exception { |
18 | File jpegFile = new File( |
19 | "C:/1.JPG" ); |
20 | Metadata metadata = JpegMetadataReader.readMetadata(jpegFile); |
21 | Directory exif = metadata.getDirectory(ExifDirectory. class ); |
22 | Iterator tags = exif.getTagIterator(); |
23 |
24 | if (exif.containsTag(ExifDirectory.TAG_WIN_AUTHOR)){ |
25 | System.out.println( "Pic author is " +exif.getDescription(ExifDirectory.TAG_WIN_AUTHOR)); |
26 | } |
27 | if (exif.containsTag(ExifDirectory.TAG_WIN_TITLE)){ |
28 | System.out.println( "Pic title is " +exif.getDescription(ExifDirectory.TAG_WIN_TITLE)); |
29 | } |
30 | if (exif.containsTag(ExifDirectory.TAG_WIN_KEYWORDS)){ |
31 | System.out.println( "Pic keyword is " +exif.getDescription(ExifDirectory.TAG_WIN_KEYWORDS)); |
32 | } |
33 | } |
34 | } |