轻松获取图片和视频文件的Exif信息-Java篇

前言

       随着现在实景地图的如火如荼建设,无人机等航拍测绘手段的不断升级,我们在获取全景照片或者正射影像,全景视频等数据上更加快速、便捷。由于无人机本身不进行相关数据的处理,比如全景地图的生成、视频的信息解析等。以全景照片为例,无人机作业时一般会在拍摄时自动记录GPS信息,拍照的坐标信息。通过自动获取图片的经纬度信息,可以快速对照片进行定位。而我们在旅游时,通常都会进行拍照,通过开启自动记录位置后,随时可以帮助我们生成旅游地图。而这些基本信息的获取,就离不开对文件元数据(metadata)的读取。

      因此,本文将重点介绍如何使用Java编程语言结合metadata-extractor去自动获取全景图片的Exif信息,获取照片的拍摄坐标信息。

一、相关知识简介

1、Metadata 元数据

       元数据(Matedata),又称中介数据、中继数据,为描述数据的数据(data about data),主要是描述数据属性(property)的信息。用来支持如指示存储位置、历史数据、资源查找、文件记录等功能。元数据是关于据的组织、数据域及其关系的信息。

       图片元数据(Metadata) 是嵌入到图片文件中的一些标签。比较像文件属性,但是种类繁多。对于数码图像,目前常见的研数据有EXIF, IPTC和XMP三种。

      EXIF:通常被数码相机在拍摄照片时自动添加,比如相机型号、镜头、曝光、图片尺寸等信息

      IPTF:比如图片标题、关键字、说明、作者、版权等信息。主要由人工在后期通过软件写入。

     XMP:XMP实际上是一种元数据存储和管理的标准,可以将Exif,IPTC或其他的数据都按XMP统一的格式存放在图像文件中。

2、Exif 可交换图像文件信息

       可交换图像文件(Exchangeable Image File,Exif)信息图像在拍摄时保留的相关参数:比如图像信息(厂商,分辨率等),相机拍摄记录(ISO,白平衡,饱和度,锐度等),缩略图(缩略图宽度,高度等),GPS(拍摄时的经度,纬度,高度)等,按照图像文件标准存储在图像头文件。一般使用支持图像读取的软件即可查看部分参数,但是图像如果修改,Exif信息可能丢失。

       上图是一张带了坐标的JPG照片信息,在Windows中通过查看文件的详细信息,可以看到这张图片的Exif信息。

3、metadata-extractor 库

      metadata-extractor 库是一个用于提取图片和视频的Exif信息的组件库。它主要提供的功能有:

    更多的信息可以查看metadata-extractor 相关介绍

二、使用步骤

    1、创建Maven项目,在Pom.xml中引入metadata-extractor

<!-- https://mvnrepository.com/artifact/com.drewnoakes/metadata-extractor --><dependency>	<groupId>com.drewnoakes</groupId>	<artifactId>metadata-extractor</artifactId>	<version>2.18.0</version></dependency> <dependency>	<groupId>junit</groupId>	<artifactId>junit</artifactId>	<version>4.11</version>	<scope>test</scope></dependency>

 2、Metadata信息获取

         这里使用ImageMetadataReader统一获取元数据信息,针对Jpg、Png、Gif等还有针对性的类,JpegMetadataReader、PngMetadataReader、GifMetadataReader等。

Metadata metadata = ImageMetadataReader.readMetadata(file);for (Directory directory : metadata.getDirectories()) {     for (Tag tag : directory.getTags()) {         String tagName = tag.getTagName();  //标签名         String desc = tag.getDescription(); //标签信息         System.out.println(tagName + "===" + desc);//照片信息      }}

         通过代码,正常输出可以看到以下的信息:

Compression Type===BaselineData Precision===8 bitsImage Height===4096 pixelsImage Width===8192 pixelsNumber of Components===3Component 1===Y component: Quantization table 0, Sampling factors 2 horiz/2 vertComponent 2===Cb component: Quantization table 1, Sampling factors 1 horiz/1 vertComponent 3===Cr component: Quantization table 1, Sampling factors 1 horiz/1 vertImage Width===8192 pixelsImage Height===4096 pixelsBits Per Sample===8 8 8 bits/component/pixelImage Description===defaultMake===HasselbladModel===L1D-20cOrientation===Top, left side (Horizontal / normal)Samples Per Pixel===3 samples/pixelX Resolution===72 dots per inchY Resolution===72 dots per inchResolution Unit===InchSoftware===10.00.12.07Date/Time===2021:03:21 14:13:31YCbCr Positioning===Center of pixel arrayWindows XP Comment===0.9.142Windows XP Keywords===panoExposure Time===1/200 secF-Number===f/5.0Exposure Program===Program normalISO Speed Ratings===100Exif Version===2.30Date/Time Original===2021:03:21 14:13:31Date/Time Digitized===2021:03:21 14:13:31Components Configuration===YCbCrExposure Bias Value===0.3 EVMax Aperture Value===f/2.8Metering Mode===AverageWhite Balance===DaylightFlash===Flash did not fireFocal Length===10.3 mmMakernote===[19829 values]FlashPix Version===1.00Color Space===sRGBExif Image Width===8192 pixelsExif Image Height===4096 pixelsFile Source===Digital Still Camera (DSC)Scene Type===Directly photographed imageExposure Mode===Auto exposureWhite Balance Mode===Auto white balanceDigital Zoom Ratio===1Scene Capture Type===StandardGain Control===NoneContrast===NoneSaturation===NoneSharpness===NoneDevice Setting Description===0 0 0 0Body Serial Number===0K8TGB40121511Lens Specification===28mm f/2.8-11.0Interoperability Index===Recommended Exif Interoperability Rules (ExifR98)Interoperability Version===1.00GPS Version ID===2.300GPS Latitude Ref===NGPS Latitude===28° 14' 37.6"GPS Longitude Ref===EGPS Longitude===112° 53' 24.86"GPS Altitude Ref===Sea levelGPS Altitude===126 metresImage Width===192 pixelsImage Height===90 pixelsCompression===JPEGX Resolution===72 dots per inchY Resolution===72 dots per inchResolution Unit===InchThumbnail Offset===21114 bytesThumbnail Length===18699 bytesXMP Value Count===30Number of Tables===4 Huffman tablesDetected File Type Name===JPEGDetected File Type Long Name===Joint Photographic Experts GroupDetected MIME Type===image/jpegExpected File Name Extension===jpgFile Name===1.jpgFile Size===14057645 bytesFile Modified Date===星期二 三月 23 20:14:37 +08:00 2021

3、根据不同的Directory精确Tag提取,示例代码如下: 

File jpegFile = new File(pathname);Metadata metadata = JpegMetadataReader.readMetadata(jpegFile);boolean type = metadata.containsDirectoryOfType(GpsDirectory.class);System.out.println(type);System.out.println(metadata.getDirectoryCount());Iterable<Directory> it = metadata.getDirectories();for(Directory d : it) {    System.out.println(d);    Collection<Tag> tags = d.getTags();    for(Tag tag :tags) {        System.out.println(tag.getTagName()+"==="+ tag.getDescription());    }   }
Image Width===849Image Height===504Bits Per Sample===8Color Type===True ColorCompression Type===DeflateFilter Method===AdaptiveInterlace Method===No InterlaceDetected File Type Name===PNGDetected File Type Long Name===Portable Network GraphicsDetected MIME Type===image/pngExpected File Name Extension===pngFile Name===111.pngFile Size===61265 bytesFile Modified Date===星期日 十月 02 19:48:34 +08:00 2022

4、经纬度提取

通过GpsDirectory来获取GeoLocation获取经坐标信息

System.out.println("开始读取gps信息...");Collection<GpsDirectory> gpsDirectories = metadata.getDirectoriesOfType(GpsDirectory.class);for(GpsDirectory gps : gpsDirectories) {    //获取图片的经纬度信息    GeoLocation geoLocation = gps.getGeoLocation();    System.out.println(geoLocation.getLongitude());    System.out.println(geoLocation.getLatitude());    System.out.println("********************************************************");}
开始读取gps信息...112.8902386944444528.243777055555558********************************************************GPS Version ID===2.300GPS Latitude Ref===NGPS Latitude===28° 14' 37.6"GPS Longitude Ref===EGPS Longitude===112° 53' 24.86"GPS Altitude Ref===Sea levelGPS Altitude===126 metres

         将图片的点在地图上定位信息如上图所示

 5、视频元数据提取

System.out.println("视频信息提取");File file = new File("E:/静园历史影像.mp4");Metadata metadata = Mp4MetadataReader.readMetadata(file);for (Directory directory : metadata.getDirectories()) {     for (Tag tag : directory.getTags()) {     String tagName = tag.getTagName();  //标签名     String desc = tag.getDescription(); //标签信息     System.out.println(tagName + "===" + desc);//照片信息     }}
视频信息提取Major Brand===MP4  Base Media v1 [IS0 14496-12:2003]Minor Version===512Compatible Brands===[MP4  Base Media v1 [IS0 14496-12:2003], MP4 Base Media v2 [ISO 14496-12:2005], MP4 Base w/ AVC ext [ISO 14496-12:2005], MP4 v1 [ISO 14496-1:ch13]]Creation Time===Fri Jan 01 08:00:00 CST 1904Modification Time===Fri Jan 01 08:00:00 CST 1904Duration===52608Media Time Scale===1000Duration in Seconds===00:00:53Transformation Matrix===65536 0 0 0 65536 0 0 0 1073741824Preferred Rate===1Preferred Volume===1Next Track ID===3Rotation===0Creation Time===星期五 一月 01 08:00:00 +08:00 1904Modification Time===星期五 一月 01 08:00:00 +08:00 1904ISO 639-2 Language Code===undOpcolor===0 0 0Graphics Mode===CopyCompression Type===H.264Width===1366 pixelsHeight===768 pixelsDepth===24-bit colorHorizontal Resolution===72Vertical Resolution===72Frame Rate===9.905Creation Time===星期五 一月 01 08:00:00 +08:00 1904Modification Time===星期五 一月 01 08:00:00 +08:00 1904ISO 639-2 Language Code===undBalance===0Format===MPEG-4, Advanced Audio Coding (AAC)Number of Channels===2Sample Size===16Sample Rate===48000File Name===静园历史影像.mp4File Size===16800279 bytesFile Modified Date===星期六 七月 16 23:16:24 +08:00 2022

三、对Exif的中文解释

      以上是参考了网友的博文,原文地址:Java获取图像Exif信息

      官网对相关参数的定义见:metadata-extractor

四、总结

       以上就是今天要讲的内容,本文简单介绍了Metadata 元数据以及Exif 可交换图像文件信息的基本知识,介绍了 metadata-extractor的具体用法,展示了png图像元数据读取、GPS坐标识别和定位以及视频的元数据信息提取,而metadata-extractor提供了大量能使我们快速便捷地获取元数据的方法。

全部评论

相关推荐

点赞 评论 收藏
转发
2 收藏 评论
分享
牛客网
牛客企业服务