java读取图片的(尺寸、拍摄日期、标记)等EXIF信息
1.metadata-extractor是 处理图片EXIF信息的开源项目,最新代码及下载地址:https://github.com/drewnoakes/metadata-extractor
2.本demo工程的代码(包含所需的jar包)下载地址:https://files.cnblogs.com/files/haha12/readPic.rar
主要代码如下:
package com.test; import java.io.File; import java.io.IOException; import java.util.Iterator; import com.drew.imaging.jpeg.JpegMetadataReader; import com.drew.imaging.jpeg.JpegProcessingException; import com.drew.metadata.Directory; import com.drew.metadata.Metadata; import com.drew.metadata.Tag; public class ReadPic { /** * 导入标签,使用metadata-extractor * * @param args */ public static void main(String[] args) { readPic(); } /** * 处理 单张 图片 * * @return void * @date 2015-7-25 下午7:30:47 */ private static void readPic() { File jpegFile = new File("d:\\002.jpg"); Metadata metadata; try { metadata = JpegMetadataReader.readMetadata(jpegFile); Iterator<Directory> it = metadata.getDirectories().iterator(); while (it.hasNext()) { Directory exif = it.next(); Iterator<Tag> tags = exif.getTags().iterator(); while (tags.hasNext()) { Tag tag = (Tag) tags.next(); System.out.println(tag); } } } catch (JpegProcessingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
控制台打印信息如下:
[Exif IFD0] Software - Picasa
[Exif IFD0] Date/Time - 2015:02:15 12:09:22
[Exif IFD0] Windows XP Keywords - 白色;蓝色;颜色白
[Exif IFD0] Padding - [2060 bytes]
[Xmp] XMP Value Count - 13
[Xmp] Subject - 白色 蓝色 颜色白
[JFIF] Version - 1.1
[JFIF] Resolution Units - inch
[JFIF] X Resolution - 96 dots
[JFIF] Y Resolution - 96 dots
[File] File Name - 002.jpg
[File] File Size - 51798 bytes
[File] File Modified Date - Mon Jul 27 09:55:42 CST 2015
[IPTC] Enveloped Record Version - 4
[IPTC] Coded Character Set - UTF-8
[IPTC] Application Record Version - 4
[IPTC] Keywords - 白色;蓝色;颜色白
[Photoshop] Caption Digest - -68 -113 27 105 -101 114 34 -54 -56 20 16 108 64 37 -42 -58
[Exif SubIFD] Exif Version - 2.20
[Exif SubIFD] Unique Image ID - f9b137287bef9686897c8a258ffd089b
[Exif SubIFD] Padding - [2060 bytes]
[JPEG] Compression Type - Baseline
[JPEG] Data Precision - 8 bits
[JPEG] Image Height - 870 pixels
[JPEG] Image Width - 580 pixels
[JPEG] Number of Components - 3
[JPEG] Component 1 - Y component: Quantization table 0, Sampling factors 2 horiz/2 vert
[JPEG] Component 2 - Cb component: Quantization table 1, Sampling factors 1 horiz/1 vert
[JPEG] Component 3 - Cr component: Quantization table 1, Sampling factors 1 horiz/1 vert
也可以
public static void main(String[] args) throws JpegProcessingException, IOException { File img = new File("D:/2.jpg"); System.out.println("File Name:" + img.getName()); Metadata metadata = JpegMetadataReader.readMetadata(img); System.out.println("Directory Count: "+metadata.getDirectoryCount()); System.out.println(); //输出所有附加属性数据 for (Directory directory : metadata.getDirectories()) { System.out.println("******\t" + directory.getName() + "\t******"); for (Tag tag : directory.getTags()) { System.out.println(tag.getTagName() + ":" + tag.getDescription()); } System.out.println(); System.out.println(); } }