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 | Date date = null;
|
---|
25 | try {
|
---|
26 | Metadata metadata = JpegMetadataReader.readMetadata(filename);
|
---|
27 | for (Iterator<Directory> dirIt = metadata.getDirectoryIterator(); dirIt.hasNext();) {
|
---|
28 | for (Iterator<Tag> tagIt = dirIt.next().getTagIterator(); tagIt.hasNext();) {
|
---|
29 | Tag tag = tagIt.next();
|
---|
30 | if (tag.getTagType() == 0x9003)
|
---|
31 | return DateParser.parse(tag.getDescription());
|
---|
32 | if (tag.getTagType() == 0x132 || tag.getTagType() == 0x9004)
|
---|
33 | date = DateParser.parse(tag.getDescription());
|
---|
34 | }
|
---|
35 | }
|
---|
36 | } catch (ParseException e) {
|
---|
37 | throw e;
|
---|
38 | } catch (Exception e) {
|
---|
39 | e.printStackTrace();
|
---|
40 | }
|
---|
41 | return date;
|
---|
42 | }
|
---|
43 |
|
---|
44 | @SuppressWarnings("unchecked") public static Integer readOrientation(File filename) throws ParseException {
|
---|
45 | Integer orientation = null;
|
---|
46 | try {
|
---|
47 | final Metadata metadata = JpegMetadataReader.readMetadata(filename);
|
---|
48 | final Directory dir = metadata.getDirectory(ExifDirectory.class);
|
---|
49 | orientation = dir.getInt(ExifDirectory.TAG_ORIENTATION);
|
---|
50 | } catch (JpegProcessingException e) {
|
---|
51 | e.printStackTrace();
|
---|
52 | } catch (MetadataException e) {
|
---|
53 | e.printStackTrace();
|
---|
54 | }
|
---|
55 | return orientation;
|
---|
56 | }
|
---|
57 |
|
---|
58 | }
|
---|