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

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

see #17848 - add ICC/Photoshop metadata support, otherwise IPTC does not work

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