开发者

Java实现获取图片和视频文件的Exif信息

开发者 https://www.devze.com 2022-11-29 11:10 出处:网络 作者: yelangking1
目录前言一、相关知识简介1、Metadata元数据2、Exif可交换图像文件信息3、metadata-extractor库二、使用步骤1、创建Maven项目2、Metadata信息获取3、根据不...
目录
  • 前言
  • 一、相关知识简介
    • 1、Metadata 元数据
    • 2、Exif 可交换图像文件信息
    • 3、metadata-extractor 库
  • 二、使用步骤
    • 1、创建Maven项目
    • 2、Metadata信息获取
    • 3、根据不同的Directory精确Tag提取
    • 4、经纬度提取
    • 5、视频元数据提取
  • 三、对Exif的中文解释
    • 四、总结

      前言

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

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

      一、相关知识简介

      1、Metadata 元数android据

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

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

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

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

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

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

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

      Java实现获取图片和视频文件的Exif信息

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

      3、metadata-extractor 库

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

      Java实现获取图片和视频文件的Exif信息

      Java实现获取图片和视频文件的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===Baseline

      Data Precision===8 bits

      Image Height===4096 pixels

      Image Width===8192 pixels

      Number of Components===3

      Component 1===Y component: Quantization table 0, Sampling factors 2 horiz/2 vert

      Component 2===Cb component: Quantization table 1, Sampling factors 1 horiz/1 vert

      Component 3===Cr component: Quantization table 1, Sampling factors 1 horiz/1 vert

      Image Width===8192 pixels

      Image Height===4096 pixels

      Bits Per Sample===8 8 8 bits/component/pixel

      Image Description===default

      Make===Hasselblad

      Model===L1D-20c

      Orientation===Top, left side (Horizontal js/ normal)

      Samples Per Pixel===3 samples/pixel

      X Resolutipythonon===72 dots per inch

      Y Resolution===72 dots per inch

      Resolution Unit===Inch

      Software===10.00.12.07

      Date/Time===2021:03:21 14:13:31

      YCbCr Positioning===Center of pixel array

      Windows XP Comment===0.9.142

      Windows XP Keywords===pano

      Exposure Time===1/200 sec

      F-Number===f/5.0

      Exposure Program===Program normal

      ISO Speed Ratings===100

      Exif Version===2.30

      Date/Time Original===2021:03:21 14:13:31

      Date/Time Digitized===2021:03:21 14:13:31

      Components Configuration===YCbCr

      Exposure Bias Value===0.3 EV

      Max Aperture Value===f/2.8

      Metering Mode===Average

      White Balance===Daylight

      Flash===Flash did not fire

      Focal Length===10.3 mm

      Makernote===[19829 values]

      FlashPix Version===1.00

      Color Space===sRGB

      Exif Image Width===8192 pixels

      Exif Image Height===4096 pixels

      File Source===Digital Still Camera (DSC)

      Scene Type===Directly photographed image

      Exposure M开发者_JS教程ode===Auto exposure

      White Balance Mode===Auto white balance

      Digital Zoom Ratio===1

      Scene Capture Type===Standard

      Gain Control===None

      Contrast===None

      Saturation===None

      Sharpness===None

      Device Setting Description===0 0 0 0

      Body Serial Number===0K8TGB40121511

      Lens Specification===28mm f/2.8-11.0

      Interoperability Index===Recommended Exif Interoperability Rules (ExifR98)

      Interoperability Version===1.00

      GPS Version ID===2.300

      GPS Latitude Ref===N

      GPS Latitude===28° 14' 37.6"

      GPS Longitude Ref===E

      GPS Longitude===112° 53' 24.86"

      GPS Altitude Ref===Sea level

      GPS Altitude===126 metres

      Image Width===192 pixels

      Image Height===90 pixels

      Compression===JPEG

      X Resolution===72 dots per inch

      Y Resolution===72 dots per inch

      Resolution Unit===Inch

      Thumbnail Offset===21114 bytes

      Thumbnail Length===18699 bytes

      XMP Value Count===30

      Number of Tables===4 Huffman tables

      Detected File Type Name===JPEG

      Detected File Type Long Name===Joint Photographic Experts Group

      Detected MIME Type===image/jpeg

      Expected File Name Extension===jpg

      File Name===1.jpg

      File Size===14057645 bytes

      File 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===849

      Image Height===504

      Bits Per Sample===8

      Color Type===True Color

      Compression Type===Deflate

      Filter Method===Adaptive

      Interlace Method===No Interlace

      Detected File Type Name===PNG

      Detected File Type Long Name===Portable Network Graphics

      Detected MIME Type===image/png

      Expected File Name Extension===png

      File Name===111.png

      File Size===61265 bytes

      File 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.89023869444445

      28.243777055555558

      ********************************************************

      GPS Version ID===2.300

      GPS Latitude Ref===N

      GPS Latitude===28° 14' 37.6"

      GPS Longitude Ref===E

      GPS Longitude===112° 53' 24.86"

      GPS Altitude Ref===Sea level

      GPS Altitude===126 metres

      Java实现获取图片和视频文件的Exif信息

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

      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===512

      Compatible 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 CSONXVIT 1904

      Modification Time===Fri Jan 01 08:00:00 CST 1904

      Duration===52608

      Media Time Scale===1000

      Duration in Seconds===00:00:53

      Transformation Matrix===65536 0 0 0 65536 0 0 0 1073741824

      Preferred Rate===1

      Preferred Volume===1

      Next Track ID===3

      Rotation===0

      Creation Time===星期五 一月 01 08:00:00 +08:00 1904

      Modification Time===星期五 一月 01 08:00:00 +08:00 1904

      ISO 639-2 Language Code===und

      Opcolor===0 0 0

      Graphics Mode===Copy

      Compression Type===H.264

      Width===1366 pixels

      Height===768 pixels

      Depth===24-bit color

      Horizontal Resolution===72

      Vertical Resolution===72

      Frame Rate===9.905

      Creation Time===星期五 一月 01 08:00:00 +08:00 1904

      Modification Time===星期五 一月 01 08:00:00 +08:00 1904

      ISO 639-2 Language Code===und

      Balance===0

      Format===MPEG-4, Advanced Audio Coding (AAC)

      Number of Channels===2

      Sample Size===16

      Sample Rate===48000

      File Name===静园历史影像.mp4

      File Size===16800279 bytes

      File Modified Date===星期六 七月 16 23:16:24 +08:00 2022

      三、对Exif的中文解释

      Java实现获取图片和视频文件的Exif信息

      Java实现获取图片和视频文件的Exif信息

      四、总结

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

      到此这篇关于Java实现获取图片和视频文件的Exif信息的文章就介绍到这了,更多相关Java获取图片视频Exif信息内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

      0

      精彩评论

      暂无评论...
      验证码 换一张
      取 消