1 | /*
|
---|
2 | * This is public domain software - that is, you can do whatever you want
|
---|
3 | * with it, and include it software that is licensed under the GNU or the
|
---|
4 | * BSD license, or whatever other licence you choose, including proprietary
|
---|
5 | * closed source licenses. I do ask that you leave this header in tact.
|
---|
6 | *
|
---|
7 | * If you make modifications to this code that you think would benefit the
|
---|
8 | * wider community, please send me a copy and I'll post it on my site.
|
---|
9 | *
|
---|
10 | * If you make use of this code, I'd appreciate hearing about it.
|
---|
11 | * drew@drewnoakes.com
|
---|
12 | * Latest version of this software kept at
|
---|
13 | * http://drewnoakes.com/
|
---|
14 | *
|
---|
15 | * Created by dnoakes on 12-Nov-2002 18:51:36 using IntelliJ IDEA.
|
---|
16 | */
|
---|
17 | package com.drew.imaging.jpeg;
|
---|
18 |
|
---|
19 | import com.drew.metadata.Directory;
|
---|
20 | import com.drew.metadata.Metadata;
|
---|
21 | import com.drew.metadata.MetadataException;
|
---|
22 | import com.drew.metadata.Tag;
|
---|
23 | import com.drew.metadata.exif.ExifDirectory;
|
---|
24 | import com.drew.metadata.exif.ExifReader;
|
---|
25 | import com.drew.metadata.iptc.IptcReader;
|
---|
26 | import com.drew.metadata.jpeg.JpegCommentReader;
|
---|
27 | import com.drew.metadata.jpeg.JpegReader;
|
---|
28 |
|
---|
29 | import java.io.File;
|
---|
30 | import java.io.IOException;
|
---|
31 | import java.io.InputStream;
|
---|
32 | import java.util.Iterator;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | *
|
---|
36 | */
|
---|
37 | public class JpegMetadataReader
|
---|
38 | {
|
---|
39 | // public static Metadata readMetadata(IIOMetadata metadata) throws JpegProcessingException {}
|
---|
40 | // public static Metadata readMetadata(ImageInputStream in) throws JpegProcessingException{}
|
---|
41 | // public static Metadata readMetadata(IIOImage image) throws JpegProcessingException{}
|
---|
42 | // public static Metadata readMetadata(ImageReader reader) throws JpegProcessingException{}
|
---|
43 |
|
---|
44 | public static Metadata readMetadata(InputStream in) throws JpegProcessingException
|
---|
45 | {
|
---|
46 | JpegSegmentReader segmentReader = new JpegSegmentReader(in);
|
---|
47 | return extractMetadataFromJpegSegmentReader(segmentReader);
|
---|
48 | }
|
---|
49 |
|
---|
50 | public static Metadata readMetadata(File file) throws JpegProcessingException
|
---|
51 | {
|
---|
52 | JpegSegmentReader segmentReader = new JpegSegmentReader(file);
|
---|
53 | return extractMetadataFromJpegSegmentReader(segmentReader);
|
---|
54 | }
|
---|
55 |
|
---|
56 | public static Metadata extractMetadataFromJpegSegmentReader(JpegSegmentReader segmentReader)
|
---|
57 | {
|
---|
58 | final Metadata metadata = new Metadata();
|
---|
59 | try {
|
---|
60 | byte[] exifSegment = segmentReader.readSegment(JpegSegmentReader.SEGMENT_APP1);
|
---|
61 | new ExifReader(exifSegment).extract(metadata);
|
---|
62 | } catch (JpegProcessingException e) {
|
---|
63 | // in the interests of catching as much data as possible, continue
|
---|
64 | // TODO lodge error message within exif directory?
|
---|
65 | }
|
---|
66 |
|
---|
67 | try {
|
---|
68 | byte[] iptcSegment = segmentReader.readSegment(JpegSegmentReader.SEGMENT_APPD);
|
---|
69 | new IptcReader(iptcSegment).extract(metadata);
|
---|
70 | } catch (JpegProcessingException e) {
|
---|
71 | // TODO lodge error message within iptc directory?
|
---|
72 | }
|
---|
73 |
|
---|
74 | try {
|
---|
75 | byte[] jpegSegment = segmentReader.readSegment(JpegSegmentReader.SEGMENT_SOF0);
|
---|
76 | new JpegReader(jpegSegment).extract(metadata);
|
---|
77 | } catch (JpegProcessingException e) {
|
---|
78 | // TODO lodge error message within jpeg directory?
|
---|
79 | }
|
---|
80 |
|
---|
81 | try {
|
---|
82 | byte[] jpegCommentSegment = segmentReader.readSegment(JpegSegmentReader.SEGMENT_COM);
|
---|
83 | new JpegCommentReader(jpegCommentSegment).extract(metadata);
|
---|
84 | } catch (JpegProcessingException e) {
|
---|
85 | // TODO lodge error message within jpegcomment directory?
|
---|
86 | }
|
---|
87 |
|
---|
88 | return metadata;
|
---|
89 | }
|
---|
90 |
|
---|
91 | private JpegMetadataReader()
|
---|
92 | {
|
---|
93 | }
|
---|
94 |
|
---|
95 | public static void main(String[] args) throws MetadataException, IOException
|
---|
96 | {
|
---|
97 | Metadata metadata = null;
|
---|
98 | try {
|
---|
99 | metadata = JpegMetadataReader.readMetadata(new File(args[0]));
|
---|
100 | } catch (Exception e) {
|
---|
101 | e.printStackTrace(System.err);
|
---|
102 | System.exit(1);
|
---|
103 | }
|
---|
104 |
|
---|
105 | // iterate over the exif data and print to System.out
|
---|
106 | Iterator directories = metadata.getDirectoryIterator();
|
---|
107 | while (directories.hasNext()) {
|
---|
108 | Directory directory = (Directory)directories.next();
|
---|
109 | Iterator tags = directory.getTagIterator();
|
---|
110 | while (tags.hasNext()) {
|
---|
111 | Tag tag = (Tag)tags.next();
|
---|
112 | try {
|
---|
113 | System.out.println("[" + directory.getName() + "] " + tag.getTagName() + " = " + tag.getDescription());
|
---|
114 | } catch (MetadataException e) {
|
---|
115 | System.err.println(e.getMessage());
|
---|
116 | System.err.println(tag.getDirectoryName() + " " + tag.getTagName() + " (error)");
|
---|
117 | }
|
---|
118 | }
|
---|
119 | if (directory.hasErrors()) {
|
---|
120 | Iterator errors = directory.getErrors();
|
---|
121 | while (errors.hasNext()) {
|
---|
122 | System.out.println("ERROR: " + errors.next());
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | if (args.length>1 && args[1].trim().equals("/thumb"))
|
---|
128 | {
|
---|
129 | ExifDirectory directory = (ExifDirectory)metadata.getDirectory(ExifDirectory.class);
|
---|
130 | if (directory.containsThumbnail())
|
---|
131 | {
|
---|
132 | System.out.println("Writing thumbnail...");
|
---|
133 | directory.writeThumbnail(args[0].trim() + ".thumb.jpg");
|
---|
134 | }
|
---|
135 | else
|
---|
136 | {
|
---|
137 | System.out.println("No thumbnail data exists in this image");
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|
141 | }
|
---|