source: josm/trunk/src/com/drew/imaging/jpeg/JpegMetadataReader.java@ 8043

Last change on this file since 8043 was 6127, checked in by bastiK, 13 years ago

applied #8895 - Upgrade metadata-extractor to v. 2.6.4 (patch by ebourg)

File size: 5.3 KB
Line 
1/*
2 * Copyright 2002-2012 Drew Noakes
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * More information about this project is available at:
17 *
18 * http://drewnoakes.com/code/exif/
19 * http://code.google.com/p/metadata-extractor/
20 */
21package com.drew.imaging.jpeg;
22
23import com.drew.lang.ByteArrayReader;
24import com.drew.lang.annotations.NotNull;
25import com.drew.metadata.Metadata;
26import com.drew.metadata.exif.ExifReader;
27import com.drew.metadata.iptc.IptcReader;
28import com.drew.metadata.jpeg.JpegCommentReader;
29import com.drew.metadata.jpeg.JpegDirectory;
30import com.drew.metadata.jpeg.JpegReader;
31
32import java.io.File;
33import java.io.IOException;
34import java.io.InputStream;
35
36/**
37 * Obtains all available metadata from Jpeg formatted files.
38 *
39 * @author Drew Noakes http://drewnoakes.com
40 */
41public class JpegMetadataReader
42{
43 // TODO investigate supporting javax.imageio
44// public static Metadata readMetadata(IIOMetadata metadata) throws JpegProcessingException {}
45// public static Metadata readMetadata(ImageInputStream in) throws JpegProcessingException{}
46// public static Metadata readMetadata(IIOImage image) throws JpegProcessingException{}
47// public static Metadata readMetadata(ImageReader reader) throws JpegProcessingException{}
48
49 @NotNull
50 public static Metadata readMetadata(@NotNull InputStream inputStream) throws JpegProcessingException
51 {
52 return readMetadata(inputStream, true);
53 }
54
55 @NotNull
56 public static Metadata readMetadata(@NotNull InputStream inputStream, final boolean waitForBytes) throws JpegProcessingException
57 {
58 JpegSegmentReader segmentReader = new JpegSegmentReader(inputStream, waitForBytes);
59 return extractMetadataFromJpegSegmentReader(segmentReader.getSegmentData());
60 }
61
62 @NotNull
63 public static Metadata readMetadata(@NotNull File file) throws JpegProcessingException, IOException
64 {
65 JpegSegmentReader segmentReader = new JpegSegmentReader(file);
66 return extractMetadataFromJpegSegmentReader(segmentReader.getSegmentData());
67 }
68
69 @NotNull
70 public static Metadata extractMetadataFromJpegSegmentReader(@NotNull JpegSegmentData segmentReader)
71 {
72 final Metadata metadata = new Metadata();
73
74 // Loop through looking for all SOFn segments. When we find one, we know what type of compression
75 // was used for the JPEG, and we can process the JPEG metadata in the segment too.
76 for (byte i = 0; i < 16; i++) {
77 // There are no SOF4 or SOF12 segments, so don't bother
78 if (i == 4 || i == 12)
79 continue;
80 // Should never have more than one SOFn for a given 'n'.
81 byte[] jpegSegment = segmentReader.getSegment((byte)(JpegSegmentReader.SEGMENT_SOF0 + i));
82 if (jpegSegment == null)
83 continue;
84 JpegDirectory directory = metadata.getOrCreateDirectory(JpegDirectory.class);
85 directory.setInt(JpegDirectory.TAG_JPEG_COMPRESSION_TYPE, i);
86 new JpegReader().extract(new ByteArrayReader(jpegSegment), metadata);
87 break;
88 }
89
90 // There should never be more than one COM segment.
91 byte[] comSegment = segmentReader.getSegment(JpegSegmentReader.SEGMENT_COM);
92 if (comSegment != null)
93 new JpegCommentReader().extract(new ByteArrayReader(comSegment), metadata);
94
95 // Loop through all APP1 segments, checking the leading bytes to identify the format of each.
96 for (byte[] app1Segment : segmentReader.getSegments(JpegSegmentReader.SEGMENT_APP1)) {
97 if (app1Segment.length > 3 && "EXIF".equalsIgnoreCase(new String(app1Segment, 0, 4)))
98 new ExifReader().extract(new ByteArrayReader(app1Segment), metadata);
99
100 //if (app1Segment.length > 27 && "http://ns.adobe.com/xap/1.0/".equalsIgnoreCase(new String(app1Segment, 0, 28)))
101 // new XmpReader().extract(new ByteArrayReader(app1Segment), metadata);
102 }
103
104 // Loop through all APPD segments, checking the leading bytes to identify the format of each.
105 for (byte[] appdSegment : segmentReader.getSegments(JpegSegmentReader.SEGMENT_APPD)) {
106 if (appdSegment.length > 12 && "Photoshop 3.0".compareTo(new String(appdSegment, 0, 13))==0) {
107 //new PhotoshopReader().extract(new ByteArrayReader(appdSegment), metadata);
108 } else {
109 // TODO might be able to check for a leading 0x1c02 for IPTC data...
110 new IptcReader().extract(new ByteArrayReader(appdSegment), metadata);
111 }
112 }
113
114 return metadata;
115 }
116
117 private JpegMetadataReader() throws Exception
118 {
119 throw new Exception("Not intended for instantiation");
120 }
121}
122
Note: See TracBrowser for help on using the repository browser.