source: josm/trunk/src/org/openstreetmap/josm/tools/ExifReader.java@ 5508

Last change on this file since 5508 was 4772, checked in by bastiK, 12 years ago

fixed #7228 - HTC Sensation uses / as date separator, date parsed wrong when geotagging

  • Property svn:eol-style set to native
File size: 2.4 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.tools;
3
4import java.io.File;
5import java.text.ParseException;
6import java.util.Date;
7import java.util.Iterator;
8
9import com.drew.imaging.jpeg.JpegMetadataReader;
10import com.drew.imaging.jpeg.JpegProcessingException;
11import com.drew.metadata.Directory;
12import com.drew.metadata.Metadata;
13import com.drew.metadata.MetadataException;
14import com.drew.metadata.Tag;
15import com.drew.metadata.exif.ExifDirectory;
16
17/**
18 * Read out exif file information from a jpeg file
19 * @author Imi
20 */
21public 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}
Note: See TracBrowser for help on using the repository browser.