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.io.IOException;
|
---|
6 | import java.text.ParseException;
|
---|
7 | import java.util.Date;
|
---|
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.ExifIFD0Directory;
|
---|
16 | import com.drew.metadata.exif.ExifSubIFDDirectory;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Read out exif file information from a jpeg file
|
---|
20 | * @author Imi
|
---|
21 | */
|
---|
22 | public class ExifReader {
|
---|
23 |
|
---|
24 | @SuppressWarnings("unchecked") public static Date readTime(File filename) throws ParseException {
|
---|
25 | try {
|
---|
26 | Metadata metadata = JpegMetadataReader.readMetadata(filename);
|
---|
27 | String dateStr = null;
|
---|
28 | OUTER:
|
---|
29 | for (Directory dirIt : metadata.getDirectories()) {
|
---|
30 | for (Tag tag : dirIt.getTags()) {
|
---|
31 | if (tag.getTagType() == ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL /* 0x9003 */) {
|
---|
32 | dateStr = tag.getDescription();
|
---|
33 | break OUTER; // prefer this tag
|
---|
34 | }
|
---|
35 | if (tag.getTagType() == ExifIFD0Directory.TAG_DATETIME /* 0x0132 */ ||
|
---|
36 | tag.getTagType() == ExifSubIFDDirectory.TAG_DATETIME_DIGITIZED /* 0x9004 */) {
|
---|
37 | dateStr = tag.getDescription();
|
---|
38 | }
|
---|
39 | }
|
---|
40 | }
|
---|
41 | if (dateStr != null) {
|
---|
42 | dateStr = dateStr.replace('/', ':'); // workaround for HTC Sensation bug, see #7228
|
---|
43 | return DateParser.parse(dateStr);
|
---|
44 | }
|
---|
45 | } catch (ParseException e) {
|
---|
46 | throw e;
|
---|
47 | } catch (Exception e) {
|
---|
48 | e.printStackTrace();
|
---|
49 | }
|
---|
50 | return null;
|
---|
51 | }
|
---|
52 |
|
---|
53 | public static Integer readOrientation(File filename) throws ParseException {
|
---|
54 | Integer orientation = null;
|
---|
55 | try {
|
---|
56 | final Metadata metadata = JpegMetadataReader.readMetadata(filename);
|
---|
57 | final Directory dir = metadata.getDirectory(ExifIFD0Directory.class);
|
---|
58 | orientation = dir.getInt(ExifIFD0Directory.TAG_ORIENTATION);
|
---|
59 | } catch (JpegProcessingException e) {
|
---|
60 | e.printStackTrace();
|
---|
61 | } catch (MetadataException e) {
|
---|
62 | e.printStackTrace();
|
---|
63 | } catch (IOException e) {
|
---|
64 | e.printStackTrace();
|
---|
65 | }
|
---|
66 | return orientation;
|
---|
67 | }
|
---|
68 |
|
---|
69 | }
|
---|