metadata-extractor读取图片信息

2019-06-05 18:40:52

测试时metadata-extracto2.6.2,测试png文件报错(不支持png),所以后来换成了最新版

  • 依赖

Gradle依赖

  1. compile 'com.drewnoakes:metadata-extractor:2.11.0'
  2. compile group: 'com.adobe.xmp', name: 'xmpcore', version: '5.1.3'

Maven依赖

  1. <dependency>
  2. <groupId>com.adobe.xmp</groupId>
  3. <artifactId>xmpcore</artifactId>
  4. <version>5.1.3</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.drewnoakes</groupId>
  8. <artifactId>metadata-extractor</artifactId>
  9. <version>2.11.0</version>
  10. </dependency>
  • 测试代码
  1. package com.demo;
  2. import com.drew.imaging.ImageMetadataReader;
  3. import com.drew.imaging.ImageProcessingException;
  4. import com.drew.metadata.Directory;
  5. import com.drew.metadata.Metadata;
  6. import com.drew.metadata.Tag;
  7. import java.io.File;
  8. import java.io.IOException;
  9. public class FileTest {
  10. public static void main(String[] args) throws ImageProcessingException, IOException {
  11. File jpegFile = new File("e:/temp.jpg");
  12. System.out.println(jpegFile.isFile());
  13. Metadata metadata = ImageMetadataReader.readMetadata(jpegFile);
  14. String longitude = ""; // 精度
  15. String latitude = ""; // 纬度
  16. for (Directory directory : metadata.getDirectories()) {
  17. for (Tag tag : directory.getTags()) {
  18. System.out.format("[%s] - %s = %s\n", directory.getName(), tag.getTagName(), tag.getDescription());
  19. String tagName = tag.getTagName(); //标签名
  20. String desc = tag.getDescription(); //标签信息
  21. if (tagName.equals("GPS Latitude")) {
  22. longitude=desc;
  23. } else if (tagName.equals("GPS Longitude")) {
  24. latitude=desc;
  25. }
  26. }
  27. if (directory.hasErrors()) {
  28. for (String error : directory.getErrors()) {
  29. System.err.format("ERROR: %s", error);
  30. }
  31. }
  32. }
  33. System.out.printf("精度:%s \n", longitude);
  34. System.out.printf("纬度:%s \n", latitude);
  35. }
  36. }

0
1
0

添加评论

正在回复:
取消
0
0
1
0