Debian Codegraph @

src/com/drew/imaging/jpeg/JpegMetadataReader.java

raw symbols SCIP
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 */
21 package com.drew.imaging.jpeg;
22
23 import com.drew.lang.StreamReader;
24 import com.drew.lang.annotations.NotNull;
25 import com.drew.lang.annotations.Nullable;
26 import com.drew.metadata.Metadata;
27 import com.drew.metadata.adobe.AdobeJpegReader;
28 import com.drew.metadata.exif.ExifReader;
29 import com.drew.metadata.file.FileSystemMetadataReader;
30 import com.drew.metadata.icc.IccReader;
31 import com.drew.metadata.iptc.IptcReader;
32 import com.drew.metadata.jfif.JfifReader;
33 import com.drew.metadata.jfxx.JfxxReader;
34 import com.drew.metadata.jpeg.JpegCommentReader;
35 import com.drew.metadata.jpeg.JpegDhtReader;
36 import com.drew.metadata.jpeg.JpegDnlReader;
37 import com.drew.metadata.jpeg.JpegReader;
38 import com.drew.metadata.photoshop.DuckyReader;
39 import com.drew.metadata.photoshop.PhotoshopReader;
40 import com.drew.metadata.xmp.XmpReader;
41
42 import java.io.File;
43 import java.io.FileInputStream;
44 import java.io.IOException;
45 import java.io.InputStream;
46 import java.util.Arrays;
47 import java.util.HashSet;
48 import java.util.Set;
49
50 /**
51 * Obtains all available metadata from JPEG formatted files.
52 *
53 * @author Drew Noakes https://drewnoakes.com
54 */
55 public 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 }