Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/drew/metadata/SampleUsage.java


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