| 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others |
|---|
| 2 | package org.openstreetmap.josm.tools; |
|---|
| 3 | |
|---|
| 4 | import java.io.File; |
|---|
| 5 | import java.text.ParseException; |
|---|
| 6 | import java.util.Date; |
|---|
| 7 | import java.util.Iterator; |
|---|
| 8 | |
|---|
| 9 | import com.drew.imaging.jpeg.JpegMetadataReader; |
|---|
| 10 | import com.drew.imaging.jpeg.JpegProcessingException; |
|---|
| 11 | import com.drew.metadata.Directory; |
|---|
| 12 | import com.drew.metadata.Metadata; |
|---|
| 13 | import com.drew.metadata.MetadataException; |
|---|
| 14 | import com.drew.metadata.Tag; |
|---|
| 15 | import com.drew.metadata.exif.ExifDirectory; |
|---|
| 16 | |
|---|
| 17 | /** |
|---|
| 18 | * Read out exif file information from a jpeg file |
|---|
| 19 | * @author Imi |
|---|
| 20 | */ |
|---|
| 21 | public class ExifReader { |
|---|
| 22 | |
|---|
| 23 | @SuppressWarnings("unchecked") public static Date readTime(File filename) throws ParseException { |
|---|
| 24 | try { |
|---|
| 25 | Metadata metadata = JpegMetadataReader.readMetadata(filename); |
|---|
| 26 | String dateStr = null; |
|---|
| 27 | OUTER: |
|---|
| 28 | for (Iterator<Directory> dirIt = metadata.getDirectoryIterator(); dirIt.hasNext();) { |
|---|
| 29 | for (Iterator<Tag> tagIt = dirIt.next().getTagIterator(); tagIt.hasNext();) { |
|---|
| 30 | Tag tag = tagIt.next(); |
|---|
| 31 | if (tag.getTagType() == ExifDirectory.TAG_DATETIME_ORIGINAL /* 0x9003 */) { |
|---|
| 32 | dateStr = tag.getDescription(); |
|---|
| 33 | break OUTER; // prefer this tag |
|---|
| 34 | } |
|---|
| 35 | if (tag.getTagType() == ExifDirectory.TAG_DATETIME /* 0x0132 */ || |
|---|
| 36 | tag.getTagType() == ExifDirectory.TAG_DATETIME_DIGITIZED /* 0x9004 */) { |
|---|
| 37 | dateStr = tag.getDescription(); |
|---|
| 38 | } |
|---|
| 39 | } |
|---|
| 40 | } |
|---|
| 41 | dateStr = dateStr.replace('/', ':'); // workaround for HTC Sensation bug, see #7228 |
|---|
| 42 | return DateParser.parse(dateStr); |
|---|
| 43 | } catch (ParseException e) { |
|---|
| 44 | throw e; |
|---|
| 45 | } catch (Exception e) { |
|---|
| 46 | e.printStackTrace(); |
|---|
| 47 | } |
|---|
| 48 | return null; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | @SuppressWarnings("unchecked") public static Integer readOrientation(File filename) throws ParseException { |
|---|
| 52 | Integer orientation = null; |
|---|
| 53 | try { |
|---|
| 54 | final Metadata metadata = JpegMetadataReader.readMetadata(filename); |
|---|
| 55 | final Directory dir = metadata.getDirectory(ExifDirectory.class); |
|---|
| 56 | orientation = dir.getInt(ExifDirectory.TAG_ORIENTATION); |
|---|
| 57 | } catch (JpegProcessingException e) { |
|---|
| 58 | e.printStackTrace(); |
|---|
| 59 | } catch (MetadataException e) { |
|---|
| 60 | e.printStackTrace(); |
|---|
| 61 | } |
|---|
| 62 | return orientation; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | } |
|---|