001 // License: GPL. Copyright 2007 by Immanuel Scholz and others 002 package org.openstreetmap.josm.tools; 003 004 import java.io.File; 005 import java.text.ParseException; 006 import java.util.Date; 007 import java.util.Iterator; 008 009 import com.drew.imaging.jpeg.JpegMetadataReader; 010 import com.drew.imaging.jpeg.JpegProcessingException; 011 import com.drew.metadata.Directory; 012 import com.drew.metadata.Metadata; 013 import com.drew.metadata.MetadataException; 014 import com.drew.metadata.Tag; 015 import com.drew.metadata.exif.ExifDirectory; 016 017 /** 018 * Read out exif file information from a jpeg file 019 * @author Imi 020 */ 021 public class ExifReader { 022 023 @SuppressWarnings("unchecked") public static Date readTime(File filename) throws ParseException { 024 try { 025 Metadata metadata = JpegMetadataReader.readMetadata(filename); 026 String dateStr = null; 027 OUTER: 028 for (Iterator<Directory> dirIt = metadata.getDirectoryIterator(); dirIt.hasNext();) { 029 for (Iterator<Tag> tagIt = dirIt.next().getTagIterator(); tagIt.hasNext();) { 030 Tag tag = tagIt.next(); 031 if (tag.getTagType() == ExifDirectory.TAG_DATETIME_ORIGINAL /* 0x9003 */) { 032 dateStr = tag.getDescription(); 033 break OUTER; // prefer this tag 034 } 035 if (tag.getTagType() == ExifDirectory.TAG_DATETIME /* 0x0132 */ || 036 tag.getTagType() == ExifDirectory.TAG_DATETIME_DIGITIZED /* 0x9004 */) { 037 dateStr = tag.getDescription(); 038 } 039 } 040 } 041 dateStr = dateStr.replace('/', ':'); // workaround for HTC Sensation bug, see #7228 042 return DateParser.parse(dateStr); 043 } catch (ParseException e) { 044 throw e; 045 } catch (Exception e) { 046 e.printStackTrace(); 047 } 048 return null; 049 } 050 051 @SuppressWarnings("unchecked") public static Integer readOrientation(File filename) throws ParseException { 052 Integer orientation = null; 053 try { 054 final Metadata metadata = JpegMetadataReader.readMetadata(filename); 055 final Directory dir = metadata.getDirectory(ExifDirectory.class); 056 orientation = dir.getInt(ExifDirectory.TAG_ORIENTATION); 057 } catch (JpegProcessingException e) { 058 e.printStackTrace(); 059 } catch (MetadataException e) { 060 e.printStackTrace(); 061 } 062 return orientation; 063 } 064 065 }