编程语言
首页 > 编程语言> > java 获取图片的媒体属性,以大疆无人机照片为例

java 获取图片的媒体属性,以大疆无人机照片为例

作者:互联网

<!--依赖的工具类-->
<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

在这里插入图片描述

标签:java,String,dji,为例,get,param,xmpProperties,directory,以大疆
来源: https://blog.csdn.net/qq_43180307/article/details/122533056