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

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

update to metadata-extractor 2.9.1

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