第一步:添加依赖
因为是maven项目,需要在pom.xml中添加相关依赖
<!--获取照片exif信息-->
<dependency>
<groupId>com.drewnoakes</groupId>
<artifactId>metadata-extractor</artifactId>
<version>2.16.0</version>
</dependency>
第二步:工具类实现
package com.tjcloud.lvyou.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.drew.imaging.ImageMetadataReader;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;
import java.io.File;
/**
* @ClassName: EXIFUtil
* @Description: 读取照片EXIF信息工具类
* @Authror: XQD
* @Date: 2022/7/8 10:37
*/
public class EXIFUtil {
public static void main(String[] args) throws Exception {
File file = new File("E:\\data\\2207131130590010.jpg");
printImageTags(file);
}
/**
* 读取照片里面的信息
*/
public static JSONObject printImageTags(File file) throws Exception {
Metadata metadata = ImageMetadataReader.readMetadata(file);
JSONObject jsonObject = new JSONObject();
String lat = "";
String log = "";
for (Directory directory : metadata.getDirectories()) {
for (Tag tag : directory.getTags()) {
// 标签名
String tagName = tag.getTagName();
// 标签信息
String desc = tag.getDescription();
System.out.println(tagName + "=" + desc);
switch (tagName) {
// 经度
case "GPS Longitude":
lat = pointToLatlong(desc);
jsonObject.put(tagName, lat);
break;
// 纬度
case "GPS Latitude":
log = pointToLatlong(desc);
jsonObject.put(tagName, log);
break;
default:
jsonObject.put(tagName, desc);
break;
}
}
}
System.out.println("**********" + jsonObject.toJSONString());
return jsonObject;
}
/***
* 经纬度坐标格式转换(* °转十进制格式)
* @param point
*/
public static String pointToLatlong(String point) {
Double du = Double.parseDouble(point.substring(0, point.indexOf("°")).trim());
Double fen = Double.parseDouble(point.substring(point.indexOf("°") + 1, point.indexOf("'")).trim());
Double miao = Double.parseDouble(point.substring(point.indexOf("'") + 1, point.indexOf("\"")).trim());
Double duStr = du + fen / 60 + miao / 60 / 60;
return duStr.toString();
}
}
效果图:
解析出来的信息有多有少,这要看照片中具有多少信息。