<dependency>
<groupId>com.drewnoakes</groupId>
<artifactId>metadata-extractor</artifactId>
<version>2.7.2</version>
</dependency>
package com.etime.EXIF;
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;
import java.io.File;
import java.io.IOException;
public class EXIFTest {
public static void main(String[] args) {
//获取图片
File jpgFile = new File("D:"+File.separator+"picture"+File.separator+"test.jpg");
//
try {
//获取图片的信息
Metadata metadata = JpegMetadataReader.readMetadata(jpgFile);
//metadata的信息存到Directory中
/**其中有五项信息,每一项信息中都有多个tags
* 所以进行双重遍历
*JPEG Directory (8 tags)
* JFIF Directory (4 tags)
* Exif IFD0 Directory (6 tags)
* Exif SubIFD Directory (5 tags)
* GPS Directory (9 tags)
*/
for (Directory directory : metadata.getDirectories()){
//从每一个director中遍历获得tag
for (Tag tag : directory.getTags()){
String tagName = tag.getTagName();//标签名
String description = tag.getDescription();//标签信息
System.out.println(tagName+"\t"+description);
}
}
} catch (JpegProcessingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
得到数据还需要经过处理才能具有可读性
Compression Type Progressive, Huffman
Data Precision 8 bits
Image Height 1920 pixels
Image Width 1440 pixels
Number of Components 3
Component 1 Y component: Quantization table 0, Sampling factors 2 horiz/2 vert
Component 2 Cb component: Quantization table 1, Sampling factors 1 horiz/1 vert
Component 3 Cr component: Quantization table 1, Sampling factors 1 horiz/1 vert
Version 1.1
Resolution Units none
X Resolution 1 dot
Y Resolution 1 dot
Unknown tag (0x0100) 3456
Model BND-AL10
Unknown tag (0x0101) 4608
Make HUAWEI
Orientation Unknown (0)
Date/Time 2019:10:26 17:57:24
White Balance Mode Auto white balance
ISO Speed Ratings 500
Focal Length 3.83 mm
Flash Flash did not fire, auto
White Balance Unknown
GPS Latitude 31° 12’ 46.64"
GPS Altitude 0 metres
GPS Latitude Ref N
GPS Altitude Ref Below sea level
GPS Processing Method CELLID
GPS Longitude Ref E
GPS Time-Stamp 9:57:23 UTC
GPS Date Stamp 2019:10:26
GPS Longitude 107° 28’ 47.46"