Advertisement
Guest User

Using metadata-extractor from JRuby

a guest
Dec 2nd, 2011
1,018
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.18 KB | None | 0 0
  1. # this file's in JRuby, which is just Ruby that can access Java. it's based on
  2. # code samples here: http://www.drewnoakes.com/code/exif/sampleUsage.html
  3.  
  4. # you can download/install JRuby to run this code, or just use it as a guide to translate into
  5. # Java. it's mostly just copied from the sample code; I just used JRuby because I figured it'd
  6. # be quicker than Java as I only needed to demo libraries and make sure they worked nicely.
  7.  
  8. # the stable release handles JPEGs, and has a great track record (used in Mathematica for example).
  9. # http://www.drewnoakes.com/code/exif/releases/metadata-extractor-2.3.1.jar
  10.  
  11. require 'java'
  12.  
  13. # file
  14. poseidon = java.io.File.new("metadata/Poseidon0466.JPG")
  15.  
  16. # extract metadata container object from file
  17. meta_poseidon = com.drew.imaging.ImageMetadataReader.read_metadata(poseidon)
  18. exif_reader_with_poseidon = com.drew.metadata.exif.ExifReader.new(poseidon)
  19. extracted_metadata = exif_reader_with_poseidon.extract(meta_poseidon)
  20.  
  21. # this is weird to me but basically the metadata's stored in directories of tags, so to
  22. # get the actual information, you need to get directories and then pull out the tags.
  23. exif_dir = extracted_metadata.getDirectory(com.drew.metadata.exif.ExifDirectory.java_class)
  24. iptc_dir = extracted_metadata.getDirectory(com.drew.metadata.iptc.IptcDirectory.java_class)
  25.  
  26. # get some specific tags
  27. make = exif_dir.get_string(com.drew.metadata.exif.ExifDirectory::TAG_MAKE)
  28. model = exif_dir.get_string(com.drew.metadata.exif.ExifDirectory::TAG_MODEL)
  29. caption = iptc_dir.get_string(com.drew.metadata.iptc.IptcDirectory::TAG_CAPTION)
  30.  
  31. # print to STDOUT
  32.  
  33. puts "\n\n\n\nmake, model, caption:\n\n\n\n"
  34. puts make
  35. puts model
  36. puts caption
  37.  
  38. # loop over all tags - I'm not sure this Java-esque approach to iterating is really necessary here,
  39. # but since it's going to be translated into Java anyway, it probably does no harm
  40.  
  41. def print_all_tags(metadata)
  42.   java_style_directory_iterator = metadata.get_directory_iterator
  43.   while java_style_directory_iterator.has_next
  44.     tag_directory = java_style_directory_iterator.next
  45.     java_style_tag_iterator = tag_directory.get_tag_iterator
  46.     while java_style_tag_iterator.has_next
  47.       tag = java_style_tag_iterator.next
  48.       puts tag
  49.     end
  50.   end
  51. end
  52. puts "\n\n\n\nlooping over jpeg\n\n\n\n"
  53. print_all_tags(meta_poseidon)
  54.  
  55. # abort "skipping TIFFs for now"
  56.  
  57. # the TIFF functionality is in beta, but it appears to be a GMail kind of beta. the library appears to
  58. # work perfectly for these sample files.
  59. # http://www.drewnoakes.com/code/exif/releases/metadata-extractor-2.4.0-beta-1.jar
  60.  
  61. tiff_filename = "metadata/100+Girls+redux+Times+test.tif"
  62.  
  63. %w{metadata/100+Girls+redux+Times+test.tif
  64.    metadata/Army+of+Darkness+bad+guys.tif
  65.    metadata/ER+Anthony+Edwards+hands.tif}.each do |tiff_filename|
  66.   puts "\n\n\n\nlooping over #{tiff_filename}\n\n\n\n"
  67.   print_all_tags(com.drew.imaging.ImageMetadataReader.read_metadata(java.io.File.new(tiff_filename)))
  68. end
  69.  
  70. # the looping code is much easier to read, and the tags present in the metadata for our sample files varies
  71. # somewhat unpredictably, so I'd say the looping approach is the best one. one caveat: TIFF metadata includes
  72. # a lot of numeric noise.
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement