Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # this file's in JRuby, which is just Ruby that can access Java. it's based on
- # code samples here: http://www.drewnoakes.com/code/exif/sampleUsage.html
- # you can download/install JRuby to run this code, or just use it as a guide to translate into
- # Java. it's mostly just copied from the sample code; I just used JRuby because I figured it'd
- # be quicker than Java as I only needed to demo libraries and make sure they worked nicely.
- # the stable release handles JPEGs, and has a great track record (used in Mathematica for example).
- # http://www.drewnoakes.com/code/exif/releases/metadata-extractor-2.3.1.jar
- require 'java'
- # file
- poseidon = java.io.File.new("metadata/Poseidon0466.JPG")
- # extract metadata container object from file
- meta_poseidon = com.drew.imaging.ImageMetadataReader.read_metadata(poseidon)
- exif_reader_with_poseidon = com.drew.metadata.exif.ExifReader.new(poseidon)
- extracted_metadata = exif_reader_with_poseidon.extract(meta_poseidon)
- # this is weird to me but basically the metadata's stored in directories of tags, so to
- # get the actual information, you need to get directories and then pull out the tags.
- exif_dir = extracted_metadata.getDirectory(com.drew.metadata.exif.ExifDirectory.java_class)
- iptc_dir = extracted_metadata.getDirectory(com.drew.metadata.iptc.IptcDirectory.java_class)
- # get some specific tags
- make = exif_dir.get_string(com.drew.metadata.exif.ExifDirectory::TAG_MAKE)
- model = exif_dir.get_string(com.drew.metadata.exif.ExifDirectory::TAG_MODEL)
- caption = iptc_dir.get_string(com.drew.metadata.iptc.IptcDirectory::TAG_CAPTION)
- # print to STDOUT
- puts "\n\n\n\nmake, model, caption:\n\n\n\n"
- puts make
- puts model
- puts caption
- # loop over all tags - I'm not sure this Java-esque approach to iterating is really necessary here,
- # but since it's going to be translated into Java anyway, it probably does no harm
- def print_all_tags(metadata)
- java_style_directory_iterator = metadata.get_directory_iterator
- while java_style_directory_iterator.has_next
- tag_directory = java_style_directory_iterator.next
- java_style_tag_iterator = tag_directory.get_tag_iterator
- while java_style_tag_iterator.has_next
- tag = java_style_tag_iterator.next
- puts tag
- end
- end
- end
- puts "\n\n\n\nlooping over jpeg\n\n\n\n"
- print_all_tags(meta_poseidon)
- # abort "skipping TIFFs for now"
- # the TIFF functionality is in beta, but it appears to be a GMail kind of beta. the library appears to
- # work perfectly for these sample files.
- # http://www.drewnoakes.com/code/exif/releases/metadata-extractor-2.4.0-beta-1.jar
- tiff_filename = "metadata/100+Girls+redux+Times+test.tif"
- %w{metadata/100+Girls+redux+Times+test.tif
- metadata/Army+of+Darkness+bad+guys.tif
- metadata/ER+Anthony+Edwards+hands.tif}.each do |tiff_filename|
- puts "\n\n\n\nlooping over #{tiff_filename}\n\n\n\n"
- print_all_tags(com.drew.imaging.ImageMetadataReader.read_metadata(java.io.File.new(tiff_filename)))
- end
- # the looping code is much easier to read, and the tags present in the metadata for our sample files varies
- # somewhat unpredictably, so I'd say the looping approach is the best one. one caveat: TIFF metadata includes
- # a lot of numeric noise.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement