java获取图片的GPS信息
简介
EXIF(Exchangeable Image File format)是“可交换图像文件”的缩写,当中包含了专门为数码相机的照片而定制的元数据,可以记录数码照片的拍摄参数、缩略图及其他属性信息。
EXIF 所记录的元数据信息非常丰富,主要包含了以下几种信息:
- 拍摄日期
- 拍摄器材(机身、镜头、闪光灯等)
- 拍摄参数(快门速度、光圈F值、ISO速度、焦距、测光模式等)
- 图像处理参数(锐化、对比度、饱和度、白平衡等)
- 图像描述及版权信息
- GPS定位数据
- 缩略图
读取图片的EXIF信息
引入依赖
<dependency>
<groupId>com.drewnoakes</groupId>
<artifactId>metadata-extractor</artifactId>
<version>2.6.2</version>
</dependency>
这个一个专门从图片或视频中提取EXIF,ICC等其他元数据信息的java库。
import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;
import java.io.File;
public class Client {
public static void main(String[] args) throws Exception {
File jpegFile = new File("D:/showqrcode.png");
Metadata metadata = JpegMetadataReader.readMetadata(jpegFile);
//获取图片所有EXIF信息
Iterable<Directory> directories = metadata.getDirectories();
for (Directory directory : directories) {
for (Tag tag : directory.getTags()) {
System.out.println(tag);
}
}
}
}
输出结果如下
[Jpeg] Compression Type - Baseline
[Jpeg] Data Precision - 8 bits
[Jpeg] Image Height - 2736 pixels
[Jpeg] Image Width - 3648 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
[Jfif] Version - 1.1
[Jfif] Resolution Units - inch
[Jfif] X Resolution - 96 dots
[Jfif] Y Resolution - 96 dots
[Exif SubIFD] Document Name -
[Exif SubIFD] Exposure Time - 0.03 sec
[Exif SubIFD] F-Number - F1.8
[Exif SubIFD] Exposure Program - Program normal
[Exif SubIFD] ISO Speed Ratings - 250
[Exif SubIFD] Exif Version - 2.10
[Exif SubIFD] Date/Time Original - 2020:10:31 16:09:14
[Exif SubIFD] Date/Time Digitized - 2020:10:31 16:09:14
[Exif SubIFD] Components Configuration - YCbCr
[Exif SubIFD] Compressed Bits Per Pixel - 0.95 bits/pixel
[Exif SubIFD] Shutter Speed Value - 1/999963864 sec
[Exif SubIFD] Aperture Value - F1.8
[Exif SubIFD] Brightness Value - 0
[Exif SubIFD] Exposure Bias Value - 0 EV
[Exif SubIFD] Max Aperture Value - F1.8
[Exif SubIFD] Metering Mode - Multi-segment
[Exif SubIFD] White Balance - Daylight
[Exif SubIFD] Flash - Flash did not fire, auto
[Exif SubIFD] Focal Length - 5.58 mm
[Exif SubIFD] Sub-Sec Time - 319846
[Exif SubIFD] Sub-Sec Time Original - 319846
[Exif SubIFD] Sub-Sec Time Digitized - 319846
[Exif SubIFD] FlashPix Version - 1.00
[Exif SubIFD] Color Space - sRGB
[Exif SubIFD] Exif Image Width - 3648 pixels
[Exif SubIFD] Exif Image Height - 2736 pixels
[Exif SubIFD] Sensing Method - One-chip color area sensor
[Exif SubIFD] File Source - Digital Still Camera (DSC)
[Exif SubIFD] Scene Type - Directly photographed image
[Exif SubIFD] Custom Rendered - Custom process
[Exif SubIFD] Exposure Mode - Auto exposure
[Exif SubIFD] White Balance Mode - Auto white balance
[Exif SubIFD] Digital Zoom Ratio - 1
[Exif SubIFD] Focal Length 35 - 27mm
[Exif SubIFD] Scene Capture Type - Standard
[Exif SubIFD] Gain Control - None
[Exif SubIFD] Contrast - None
[Exif SubIFD] Saturation - None
[Exif SubIFD] Sharpness - None
[Exif SubIFD] Subject Distance Range - Unknown
[Exif IFD0] Unknown tag (0x0100) - 3648
[Exif IFD0] Unknown tag (0x0101) - 2736
[Exif IFD0] Unknown tag (0x0102) - 8 8 8
[Exif IFD0] Make - HUAWEI
[Exif IFD0] Model - CLT-AL00
[Exif IFD0] Orientation - 0
[Exif IFD0] X Resolution - 72 dots per inch
[Exif IFD0] Y Resolution - 72 dots per inch
[Exif IFD0] Resolution Unit - Inch
[Exif IFD0] Software - CLT-AL00 10.0.0.175(C00E175R1P4)
[Exif IFD0] Date/Time - 2020:10:31 16:09:14
[Exif IFD0] YCbCr Positioning - Center of pixel array
[Exif IFD0] Unknown tag (0xa40b) - 105 112 112 0
[Interoperability] Interoperability Index - Recommended Exif Interoperability Rules (ExifR98)
[Interoperability] Interoperability Version - 1.00
[GPS] GPS Version ID - 2.200
[GPS] GPS Latitude Ref - N
[GPS] GPS Latitude - 30.0° 16.0' 10.113371999997298"
[GPS] GPS Longitude Ref - E
[GPS] GPS Longitude - 120.0° 5.0' 0.00915499999223357"
[GPS] GPS Altitude Ref - Below sea level
[GPS] GPS Altitude - 0 metres
[GPS] GPS Time-Stamp - 8:9:14 UTC
[GPS] GPS Processing Method - CELLID
[GPS] GPS Date Stamp - 2020:10:31
[Exif Thumbnail] Thumbnail Image Width - 512 pixels
[Exif Thumbnail] Thumbnail Image Height - 384 pixels
[Exif Thumbnail] Thumbnail Compression - JPEG (old-style)
[Exif Thumbnail] Orientation - 0
[Exif Thumbnail] X Resolution - 72 dots per inch
[Exif Thumbnail] Y Resolution - 72 dots per inch
[Exif Thumbnail] Resolution Unit - Inch
[Exif Thumbnail] Thumbnail Offset - 8862 bytes
[Exif Thumbnail] Thumbnail Length - 25465 bytes
其中就包括GPS信息,不过GPS信息不太可读,我们也可以使用下面这种方式
import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.lang.GeoLocation;
import com.drew.metadata.Metadata;
import com.drew.metadata.exif.GpsDirectory;
import java.io.File;
import java.util.Objects;
public class Client2 {
public static void main(String[] args) throws Exception {
File jpegFile = new File("D:/showqrcode.png");
Metadata metadata = JpegMetadataReader.readMetadata(jpegFile);
GpsDirectory gpsDirectory = metadata.getDirectory(GpsDirectory.class);
if (Objects.nonNull(gpsDirectory)) {
GeoLocation geoLocation = gpsDirectory.getGeoLocation();
System.out.println(geoLocation.getLongitude());
System.out.println(geoLocation.getLatitude());
}
}
}
如果图片不包括GPS信息的话,gpsDirectory就为空,输出经纬度结果为
120.08333587638889
30.269475936666666
进入百度拾取坐标系统,将经纬度转换成位置,截图如下
从上面可以看到,一张图片包含了太多信息,我们在分享图片时一定要慎重。我们使用QQ或微信发送图片时,不选择原图的话,QQ或微信就会自动帮我们压缩,这样就会去除GPS信息,更加安全。
参考
从技术的角度分析下为什么不要在网上发“原图”
Java Code Examples for com.drew.lang.GeoLocation
百度拾取坐标系统
图片EXIF信息获取