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

Last change on this file since 8132 was 8132, checked in by Don-vip, 9 years ago

fix #11162 - update to metadata-extractor 2.7.2

File size: 4.8 KB
Line 
1/*
2 * Copyright 2002-2015 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 * https://drewnoakes.com/code/exif/
19 * https://github.com/drewnoakes/metadata-extractor
20 */
21package com.drew.imaging.jpeg;
22
23import java.io.File;
24import java.io.FileInputStream;
25import java.io.IOException;
26import java.io.InputStream;
27import java.util.Arrays;
28import java.util.HashSet;
29import java.util.Set;
30
31import com.drew.lang.StreamReader;
32import com.drew.lang.annotations.NotNull;
33import com.drew.lang.annotations.Nullable;
34import com.drew.metadata.Metadata;
35//import com.drew.metadata.adobe.AdobeJpegReader;
36import com.drew.metadata.exif.ExifReader;
37//import com.drew.metadata.icc.IccReader;
38import com.drew.metadata.iptc.IptcReader;
39//import com.drew.metadata.jfif.JfifReader;
40import com.drew.metadata.jpeg.JpegCommentReader;
41import com.drew.metadata.jpeg.JpegReader;
42//import com.drew.metadata.photoshop.PhotoshopReader;
43//import com.drew.metadata.xmp.XmpReader;
44
45/**
46 * Obtains all available metadata from JPEG formatted files.
47 *
48 * @author Drew Noakes https://drewnoakes.com
49 */
50public class JpegMetadataReader
51{
52 public static final Iterable<JpegSegmentMetadataReader> ALL_READERS = Arrays.asList(
53 new JpegReader(),
54 new JpegCommentReader(),
55 //new JfifReader(),
56 new ExifReader(),
57 //new XmpReader(),
58 //new IccReader(),
59 //new PhotoshopReader(),
60 new IptcReader()//,
61 //new AdobeJpegReader()
62 );
63
64 @NotNull
65 public static Metadata readMetadata(@NotNull InputStream inputStream, @Nullable Iterable<JpegSegmentMetadataReader> readers) throws JpegProcessingException, IOException
66 {
67 Metadata metadata = new Metadata();
68 process(metadata, inputStream, readers);
69 return metadata;
70 }
71
72 @NotNull
73 public static Metadata readMetadata(@NotNull InputStream inputStream) throws JpegProcessingException, IOException
74 {
75 return readMetadata(inputStream, null);
76 }
77
78 @NotNull
79 public static Metadata readMetadata(@NotNull File file, @Nullable Iterable<JpegSegmentMetadataReader> readers) throws JpegProcessingException, IOException
80 {
81 InputStream inputStream = null;
82 try
83 {
84 inputStream = new FileInputStream(file);
85 return readMetadata(inputStream, readers);
86 } finally {
87 if (inputStream != null)
88 inputStream.close();
89 }
90 }
91
92 @NotNull
93 public static Metadata readMetadata(@NotNull File file) throws JpegProcessingException, IOException
94 {
95 return readMetadata(file, null);
96 }
97
98 public static void process(@NotNull Metadata metadata, @NotNull InputStream inputStream) throws JpegProcessingException, IOException
99 {
100 process(metadata, inputStream, null);
101 }
102
103 public static void process(@NotNull Metadata metadata, @NotNull InputStream inputStream, @Nullable Iterable<JpegSegmentMetadataReader> readers) throws JpegProcessingException, IOException
104 {
105 if (readers == null)
106 readers = ALL_READERS;
107
108 Set<JpegSegmentType> segmentTypes = new HashSet<JpegSegmentType>();
109 for (JpegSegmentMetadataReader reader : readers) {
110 for (JpegSegmentType type : reader.getSegmentTypes()) {
111 segmentTypes.add(type);
112 }
113 }
114
115 JpegSegmentData segmentData = JpegSegmentReader.readSegments(new StreamReader(inputStream), segmentTypes);
116
117 processJpegSegmentData(metadata, readers, segmentData);
118 }
119
120 public static void processJpegSegmentData(Metadata metadata, Iterable<JpegSegmentMetadataReader> readers, JpegSegmentData segmentData)
121 {
122 // Pass the appropriate byte arrays to each reader.
123 for (JpegSegmentMetadataReader reader : readers) {
124 for (JpegSegmentType segmentType : reader.getSegmentTypes()) {
125 for (byte[] segmentBytes : segmentData.getSegments(segmentType)) {
126 if (reader.canProcess(segmentBytes, segmentType)) {
127 reader.extract(segmentBytes, metadata, segmentType);
128 }
129 }
130 }
131 }
132 }
133
134 private JpegMetadataReader() throws Exception
135 {
136 throw new Exception("Not intended for instantiation");
137 }
138}
Note: See TracBrowser for help on using the repository browser.