本文实例讲述了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版本。你可以直接从/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 - :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 - :04:14 13:47:10
[Exif] Date/Time Digitized - :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 - 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程序设计有所帮助。