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

Last change on this file since 6331 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
RevLine 
[4231]1/*
[6127]2 * Copyright 2002-2012 Drew Noakes
[4231]3 *
[6127]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
[4231]7 *
[6127]8 * http://www.apache.org/licenses/LICENSE-2.0
[4231]9 *
[6127]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/
[4231]20 */
21package com.drew.imaging.jpeg;
22
[6127]23import com.drew.lang.ByteArrayReader;
24import com.drew.lang.annotations.NotNull;
[4231]25import com.drew.metadata.Metadata;
26import com.drew.metadata.exif.ExifReader;
27import com.drew.metadata.iptc.IptcReader;
28import com.drew.metadata.jpeg.JpegCommentReader;
[6127]29import com.drew.metadata.jpeg.JpegDirectory;
[4231]30import com.drew.metadata.jpeg.JpegReader;
31
32import java.io.File;
33import java.io.IOException;
34import java.io.InputStream;
35
36/**
[6127]37 * Obtains all available metadata from Jpeg formatted files.
[4231]38 *
[6127]39 * @author Drew Noakes http://drewnoakes.com
[4231]40 */
41public class JpegMetadataReader
42{
[6127]43 // TODO investigate supporting javax.imageio
[4231]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
[6127]49 @NotNull
50 public static Metadata readMetadata(@NotNull InputStream inputStream) throws JpegProcessingException
[4231]51 {
[6127]52 return readMetadata(inputStream, true);
[4231]53 }
54
[6127]55 @NotNull
56 public static Metadata readMetadata(@NotNull InputStream inputStream, final boolean waitForBytes) throws JpegProcessingException
[4231]57 {
[6127]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 {
[4231]65 JpegSegmentReader segmentReader = new JpegSegmentReader(file);
[6127]66 return extractMetadataFromJpegSegmentReader(segmentReader.getSegmentData());
[4231]67 }
68
[6127]69 @NotNull
70 public static Metadata extractMetadataFromJpegSegmentReader(@NotNull JpegSegmentData segmentReader)
[4231]71 {
72 final Metadata metadata = new Metadata();
73
[6127]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;
[4231]88 }
89
[6127]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);
[4231]94
[6127]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);
[4231]99
[6127]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);
[4231]102 }
103
[6127]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);
[4231]111 }
112 }
113
[6127]114 return metadata;
[4231]115 }
[6127]116
117 private JpegMetadataReader() throws Exception
118 {
119 throw new Exception("Not intended for instantiation");
120 }
[4231]121}
[6127]122
Note: See TracBrowser for help on using the repository browser.