<!--依赖的工具类-->
<dependency>
<groupId>com.drewnoakes</groupId>
<artifactId>metadata-extractor</artifactId>
<version>2.6.2</version>
</dependency>
以下仅使用大疆的图片作为参考,每种图片自定义的媒体信息有所不同,debug即可获得实际key值;
/**
* @author zy
* @Description InputStream 通过读取流的方式获取文件的媒体信息
* @time 2022/1/17 9:23
* @Param [inputStream]
* @return java.lang.String[]
*/
public static String[] getImageParam(InputStream inputStream) {
try {
String[] param = new String[5];
Metadata metadata = JpegMetadataReader.readMetadata(inputStream);
for (Directory directory : metadata.getDirectories()) {
//获取图片的Exif拍摄时间
if (directory instanceof ExifIFD0Directory) {
ExifIFD0Directory exifIFD0Directory = (ExifIFD0Directory) directory;
exifIFD0Directory.getName();
param[4] = DateUtil.format.format(exifIFD0Directory.getDate(306));
}
if (directory instanceof XmpDirectory) {
XmpDirectory xmpDirectory = (XmpDirectory) directory;
Map<String, String> xmpProperties = xmpDirectory.getXmpProperties();
String degree = xmpProperties.get("drone-dji:GimbalYawDegree");
String lat = xmpProperties.get("drone-dji:GpsLatitude");
String lon = xmpProperties.get("drone-dji:GpsLongtitude");
String relativeAltitude = xmpProperties.get("drone-dji:RelativeAltitude");
param[0] = degree;
param[1] = lat;
param[2] = lon;
param[3] = relativeAltitude;
}
}
return param;
} catch (Exception e) {
return null;
}
}
该方法可以读取以下几种文件形式;JpegMetadataReader.readMetadata