Which focal length do you use most?

15-九月-2009 16:25 - Filed under: Examples

“Which focal length do you use most?” is the question that my friend Moritz asked himself. He used jrawio to quickly extract the focal lengths out of all the Nikon NEF photos in his archive, with this small application. It will be included in jrawio examples with the next release.

package it.tidalwave.imageio.example.stats;

import java.util.ArrayList;
import java.util.Collection;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.metadata.IIOMetadata;
import org.apache.commons.io.DirectoryWalker;
import it.tidalwave.imageio.nef.NEFMetadata;
import it.tidalwave.imageio.raw.TagRational;
import it.tidalwave.imageio.tiff.IFD;

public class FocalLengthStats
  {
    public static void main (final String[] args)
      {
        try
          {
            final PrintWriter out = new PrintWriter(new File(args[1]));
            new DirectoryWalker()
              {
                @Override
                protected void handleFile (final File file, final int depth, final Collection results)
                  throws IOException
                  {
                    if (file.getName().toUpperCase().endsWith(".NEF"))
                      {
                        System.out.printf("Processing %s...\n", file.getCanonicalPath());
                        final ImageReader reader = (ImageReader) ImageIO.getImageReaders(file).next();
                        reader.setInput(ImageIO.createImageInputStream(file));
                        final IIOMetadata metadata = reader.getImageMetadata(0);
                        final NEFMetadata nefMetadata = (NEFMetadata) metadata;
                        final IFD exifIFD = nefMetadata.getExifIFD();
                        final TagRational focalLength = exifIFD.getFocalLength();
                        out.println(focalLength.doubleValue());
                      }
                  }

                public void start() 
                  throws IOException
                  {
                    super.walk(new File(args[0]), new ArrayList<Object>());
                  }
              }.start();

            out.flush();
            out.close();
          }
        catch (Exception e)
          {
            e.printStackTrace();
          }
      }
  }

 

 

 

This blog doesn't allow to directly comment posts. Please provide us with your feedback by means of either the “dev” or the “users” mailing list, as described in the contact page. Thanks!