source: josm/trunk/src/com/drew/metadata/SampleUsage.java@ 6040

Last change on this file since 6040 was 4231, checked in by stoecker, 13 years ago

add signpost and metadata extractor code to repository directly

File size: 4.5 KB
Line 
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 05-Nov-2002 18:57:14 using IntelliJ IDEA.
16 */
17package com.drew.metadata;
18
19import com.drew.imaging.jpeg.JpegMetadataReader;
20import com.drew.imaging.jpeg.JpegProcessingException;
21import com.drew.imaging.jpeg.JpegSegmentReader;
22import com.drew.metadata.exif.ExifReader;
23import com.drew.metadata.iptc.IptcReader;
24
25import java.awt.image.BufferedImage;
26import java.io.File;
27import java.io.FileInputStream;
28import java.io.FileNotFoundException;
29import java.io.IOException;
30import java.util.Iterator;
31
32/**
33 *
34 */
35public class SampleUsage
36{
37 /**
38 * Constructor which executes multiple sample usages, each of which return the same output. This class showcases
39 * multiple usages of this metadata class library.
40 * @param fileName path to a jpeg file upon which to operate
41 */
42 public SampleUsage(String fileName)
43 {
44 File jpegFile = new File(fileName);
45
46 // There are multiple ways to get a Metadata object
47
48 // Approach 1
49 // This approach reads all types of known Jpeg metadata (at present,
50 // Exif and Iptc) in a single call. In most cases, this is the most
51 // appropriate usage.
52 try {
53 Metadata metadata = JpegMetadataReader.readMetadata(jpegFile);
54 printImageTags(1, metadata);
55 } catch (JpegProcessingException e) {
56 System.err.println("error 1a");
57 }
58
59 // Approach 2
60 // This approach shows using individual MetadataReader implementations
61 // to read a file. This is less efficient than approach 1, as the file
62 // is opened and read twice.
63 try {
64 Metadata metadata = new Metadata();
65 new ExifReader(jpegFile).extract(metadata);
66 new IptcReader(jpegFile).extract(metadata);
67 printImageTags(2, metadata);
68 } catch (JpegProcessingException jpe) {
69 System.err.println("error 2a");
70 }
71
72 // Approach 3
73 // As fast as approach 1 (this is what goes on inside the JpegMetadataReader's
74 // readMetadata() method), this code is handy if you want to look into other
75 // Jpeg segments too.
76 try {
77 JpegSegmentReader segmentReader = new JpegSegmentReader(jpegFile);
78 byte[] exifSegment = segmentReader.readSegment(JpegSegmentReader.SEGMENT_APP1);
79 byte[] iptcSegment = segmentReader.readSegment(JpegSegmentReader.SEGMENT_APPD);
80 Metadata metadata = new Metadata();
81 new ExifReader(exifSegment).extract(metadata);
82 new IptcReader(iptcSegment).extract(metadata);
83 printImageTags(3, metadata);
84 } catch (JpegProcessingException jpe) {
85 System.err.println("error 3a");
86 }
87 }
88
89 private void printImageTags(int approachCount, Metadata metadata)
90 {
91 System.out.println();
92 System.out.println("*** APPROACH " + approachCount + " ***");
93 System.out.println();
94 // iterate over the exif data and print to System.out
95 Iterator directories = metadata.getDirectoryIterator();
96 while (directories.hasNext()) {
97 Directory directory = (Directory)directories.next();
98 Iterator tags = directory.getTagIterator();
99 while (tags.hasNext()) {
100 Tag tag = (Tag)tags.next();
101 System.out.println(tag);
102 }
103 if (directory.hasErrors()) {
104 Iterator errors = directory.getErrors();
105 while (errors.hasNext()) {
106 System.out.println("ERROR: " + errors.next());
107 }
108 }
109 }
110 }
111
112 /**
113 * Executes the sample usage program.
114 * @param args command line parameters
115 */
116 public static void main(String[] args)
117 {
118 new SampleUsage("src/com/drew/metadata/test/withIptcExifGps.jpg");
119 }
120}
Note: See TracBrowser for help on using the repository browser.