Debian Codegraph @

src/com/drew/metadata/exif/ExifReader.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.metadata.exif;
22
23 import com.drew.imaging.jpeg.JpegSegmentMetadataReader;
24 import com.drew.imaging.jpeg.JpegSegmentType;
25 import com.drew.imaging.tiff.TiffProcessingException;
26 import com.drew.imaging.tiff.TiffReader;
27 import com.drew.lang.ByteArrayReader;
28 import com.drew.lang.RandomAccessReader;
29 import com.drew.lang.annotations.NotNull;
30 import com.drew.lang.annotations.Nullable;
31 import com.drew.metadata.Directory;
32 import com.drew.metadata.Metadata;
33
34 import java.io.IOException;
35 import java.util.Collections;
36
37 /**
38 * Decodes Exif binary data, populating a {@link Metadata} object with tag values in {@link ExifSubIFDDirectory},
39 * {@link ExifThumbnailDirectory}, {@link ExifInteropDirectory}, {@link GpsDirectory} and one of the many camera
40 * makernote directories.
41 *
42 * @author Drew Noakes https://drewnoakes.com
43 */
44 @SuppressWarnings("WeakerAccess")
45 public class ExifReader implements JpegSegmentMetadataReader
46 {
47 /** Exif data stored in JPEG files' APP1 segment are preceded by this six character preamble "Exif\0\0". */
48 public static final String JPEG_SEGMENT_PREAMBLE = "Exif\0\0";
49
50 @NotNull
51 public Iterable<JpegSegmentType> getSegmentTypes()
52 {
53 return Collections.singletonList(JpegSegmentType.APP1);
54 }
55
56 public void readJpegSegments(@NotNull final Iterable<byte[]> segments, @NotNull final Metadata metadata, @NotNull final JpegSegmentType segmentType)
57 {
58 assert(segmentType == JpegSegmentType.APP1);
59
60 for (byte[] segmentBytes : segments) {
61 // Segment must have the expected preamble
62 if (startsWithJpegExifPreamble(segmentBytes)) {
63 extract(new ByteArrayReader(segmentBytes), metadata, JPEG_SEGMENT_PREAMBLE.length());
64 }
65 }
66 }
67
68 /** Indicates whether 'bytes' starts with 'JpegSegmentPreamble'. */
69 public static boolean startsWithJpegExifPreamble(byte[] bytes)
70 {
71 return bytes.length >= JPEG_SEGMENT_PREAMBLE.length() &&
72 new String(bytes, 0, JPEG_SEGMENT_PREAMBLE.length()).equals(JPEG_SEGMENT_PREAMBLE);
73 }
74
75 /** Reads TIFF formatted Exif data from start of the specified {@link RandomAccessReader}. */
76 public void extract(@NotNull final RandomAccessReader reader, @NotNull final Metadata metadata)
77 {
78 extract(reader, metadata, 0);
79 }
80
81 /** Reads TIFF formatted Exif data a specified offset within a {@link RandomAccessReader}. */
82 public void extract(@NotNull final RandomAccessReader reader, @NotNull final Metadata metadata, int readerOffset)
83 {
84 extract(reader, metadata, readerOffset, null);
85 }
86
87 /** Reads TIFF formatted Exif data at a specified offset within a {@link RandomAccessReader}. */
88 public void extract(@NotNull final RandomAccessReader reader, @NotNull final Metadata metadata, int readerOffset, @Nullable Directory parentDirectory)
89 {
90 ExifTiffHandler exifTiffHandler = new ExifTiffHandler(metadata, parentDirectory, readerOffset);
91
92 try {
93 // Read the TIFF-formatted Exif data
94 new TiffReader().processTiff(
95 reader,
96 exifTiffHandler,
97 readerOffset
98 );
99 } catch (TiffProcessingException e) {
100 exifTiffHandler.error("Exception processing TIFF data: " + e.getMessage());
101 } catch (IOException e) {
102 exifTiffHandler.error("Exception processing TIFF data: " + e.getMessage());
103 }
104 }
105 }