当前位置:   article > 正文

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

java exif

前言

       随着现在实景地图的如火如荼建设,无人机等航拍测绘手段的不断升级,我们在获取全景照片或者正射影像,全景视频等数据上更加快速、便捷。由于无人机本身不进行相关数据的处理,比如全景地图的生成、视频的信息解析等。以全景照片为例,无人机作业时一般会在拍摄时自动记录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

  1. <!-- https://mvnrepository.com/artifact/com.drewnoakes/metadata-extractor -->
  2. <dependency>
  3. <groupId>com.drewnoakes</groupId>
  4. <artifactId>metadata-extractor</artifactId>
  5. <version>2.18.0</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>junit</groupId>
  9. <artifactId>junit</artifactId>
  10. <version>4.11</version>
  11. <scope>test</scope>
  12. </dependency>

 2、Metadata信息获取

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

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

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

  1. Compression Type===Baseline
  2. Data Precision===8 bits
  3. Image Height===4096 pixels
  4. Image Width===8192 pixels
  5. Number of Components===3
  6. Component 1===Y component: Quantization table 0, Sampling factors 2 horiz/2 vert
  7. Component 2===Cb component: Quantization table 1, Sampling factors 1 horiz/1 vert
  8. Component 3===Cr component: Quantization table 1, Sampling factors 1 horiz/1 vert
  9. Image Width===8192 pixels
  10. Image Height===4096 pixels
  11. Bits Per Sample===8 8 8 bits/component/pixel
  12. Image Description===default
  13. Make===Hasselblad
  14. Model===L1D-20c
  15. Orientation===Top, left side (Horizontal / normal)
  16. Samples Per Pixel===3 samples/pixel
  17. X Resolution===72 dots per inch
  18. Y Resolution===72 dots per inch
  19. Resolution Unit===Inch
  20. Software===10.00.12.07
  21. Date/Time===2021:03:21 14:13:31
  22. YCbCr Positioning===Center of pixel array
  23. Windows XP Comment===0.9.142
  24. Windows XP Keywords===pano
  25. Exposure Time===1/200 sec
  26. F-Number===f/5.0
  27. Exposure Program===Program normal
  28. ISO Speed Ratings===100
  29. Exif Version===2.30
  30. Date/Time Original===2021:03:21 14:13:31
  31. Date/Time Digitized===2021:03:21 14:13:31
  32. Components Configuration===YCbCr
  33. Exposure Bias Value===0.3 EV
  34. Max Aperture Value===f/2.8
  35. Metering Mode===Average
  36. White Balance===Daylight
  37. Flash===Flash did not fire
  38. Focal Length===10.3 mm
  39. Makernote===[19829 values]
  40. FlashPix Version===1.00
  41. Color Space===sRGB
  42. Exif Image Width===8192 pixels
  43. Exif Image Height===4096 pixels
  44. File Source===Digital Still Camera (DSC)
  45. Scene Type===Directly photographed image
  46. Exposure Mode===Auto exposure
  47. White Balance Mode===Auto white balance
  48. Digital Zoom Ratio===1
  49. Scene Capture Type===Standard
  50. Gain Control===None
  51. Contrast===None
  52. Saturation===None
  53. Sharpness===None
  54. Device Setting Description===0 0 0 0
  55. Body Serial Number===0K8TGB40121511
  56. Lens Specification===28mm f/2.8-11.0
  57. Interoperability Index===Recommended Exif Interoperability Rules (ExifR98)
  58. Interoperability Version===1.00
  59. GPS Version ID===2.300
  60. GPS Latitude Ref===N
  61. GPS Latitude===28° 14' 37.6"
  62. GPS Longitude Ref===E
  63. GPS Longitude===112° 53' 24.86"
  64. GPS Altitude Ref===Sea level
  65. GPS Altitude===126 metres
  66. Image Width===192 pixels
  67. Image Height===90 pixels
  68. Compression===JPEG
  69. X Resolution===72 dots per inch
  70. Y Resolution===72 dots per inch
  71. Resolution Unit===Inch
  72. Thumbnail Offset===21114 bytes
  73. Thumbnail Length===18699 bytes
  74. XMP Value Count===30
  75. Number of Tables===4 Huffman tables
  76. Detected File Type Name===JPEG
  77. Detected File Type Long Name===Joint Photographic Experts Group
  78. Detected MIME Type===image/jpeg
  79. Expected File Name Extension===jpg
  80. File Name===1.jpg
  81. File Size===14057645 bytes
  82. File Modified Date===星期二 三月 23 20:14:37 +08:00 2021

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

  1. File jpegFile = new File(pathname);
  2. Metadata metadata = JpegMetadataReader.readMetadata(jpegFile);
  3. boolean type = metadata.containsDirectoryOfType(GpsDirectory.class);
  4. System.out.println(type);
  5. System.out.println(metadata.getDirectoryCount());
  6. Iterable<Directory> it = metadata.getDirectories();
  7. for(Directory d : it) {
  8. System.out.println(d);
  9. Collection<Tag> tags = d.getTags();
  10. for(Tag tag :tags) {
  11. System.out.println(tag.getTagName()+"==="+ tag.getDescription());
  12. }
  13. }
  1. Image Width===849
  2. Image Height===504
  3. Bits Per Sample===8
  4. Color Type===True Color
  5. Compression Type===Deflate
  6. Filter Method===Adaptive
  7. Interlace Method===No Interlace
  8. Detected File Type Name===PNG
  9. Detected File Type Long Name===Portable Network Graphics
  10. Detected MIME Type===image/png
  11. Expected File Name Extension===png
  12. File Name===111.png
  13. File Size===61265 bytes
  14. File Modified Date===星期日 十月 02 19:48:34 +08:00 2022

4、经纬度提取

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

  1. System.out.println("开始读取gps信息...");
  2. Collection<GpsDirectory> gpsDirectories = metadata.getDirectoriesOfType(GpsDirectory.class);
  3. for(GpsDirectory gps : gpsDirectories) {
  4. //获取图片的经纬度信息
  5. GeoLocation geoLocation = gps.getGeoLocation();
  6. System.out.println(geoLocation.getLongitude());
  7. System.out.println(geoLocation.getLatitude());
  8. System.out.println("********************************************************");
  9. }
  1. 开始读取gps信息...
  2. 112.89023869444445
  3. 28.243777055555558
  4. ********************************************************
  5. GPS Version ID===2.300
  6. GPS Latitude Ref===N
  7. GPS Latitude===28° 14' 37.6"
  8. GPS Longitude Ref===E
  9. GPS Longitude===112° 53' 24.86"
  10. GPS Altitude Ref===Sea level
  11. GPS Altitude===126 metres

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

 5、视频元数据提取

  1. System.out.println("视频信息提取");
  2. File file = new File("E:/静园历史影像.mp4");
  3. Metadata metadata = Mp4MetadataReader.readMetadata(file);
  4. for (Directory directory : metadata.getDirectories()) {
  5. for (Tag tag : directory.getTags()) {
  6. String tagName = tag.getTagName(); //标签名
  7. String desc = tag.getDescription(); //标签信息
  8. System.out.println(tagName + "===" + desc);//照片信息
  9. }
  10. }
  1. 视频信息提取
  2. Major Brand===MP4  Base Media v1 [IS0 14496-12:2003]
  3. Minor Version===512
  4. 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]]
  5. Creation Time===Fri Jan 01 08:00:00 CST 1904
  6. Modification Time===Fri Jan 01 08:00:00 CST 1904
  7. Duration===52608
  8. Media Time Scale===1000
  9. Duration in Seconds===00:00:53
  10. Transformation Matrix===65536 0 0 0 65536 0 0 0 1073741824
  11. Preferred Rate===1
  12. Preferred Volume===1
  13. Next Track ID===3
  14. Rotation===0
  15. Creation Time===星期五 一月 01 08:00:00 +08:00 1904
  16. Modification Time===星期五 一月 01 08:00:00 +08:00 1904
  17. ISO 639-2 Language Code===und
  18. Opcolor===0 0 0
  19. Graphics Mode===Copy
  20. Compression Type===H.264
  21. Width===1366 pixels
  22. Height===768 pixels
  23. Depth===24-bit color
  24. Horizontal Resolution===72
  25. Vertical Resolution===72
  26. Frame Rate===9.905
  27. Creation Time===星期五 一月 01 08:00:00 +08:00 1904
  28. Modification Time===星期五 一月 01 08:00:00 +08:00 1904
  29. ISO 639-2 Language Code===und
  30. Balance===0
  31. Format===MPEG-4, Advanced Audio Coding (AAC)
  32. Number of Channels===2
  33. Sample Size===16
  34. Sample Rate===48000
  35. File Name===静园历史影像.mp4
  36. File Size===16800279 bytes
  37. File Modified Date===星期六 七月 16 23:16:24 +08:00 2022

三、对Exif的中文解释

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

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

四、总结

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

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号