source: josm/trunk/src/com/drew/metadata/exif/ExifReader.java@ 13500

Last change on this file since 13500 was 13061, checked in by Don-vip, 6 years ago

fix #15505 - update to metadata-extractor 2.10.1

File size: 4.1 KB
Line 
1/*
2 * Copyright 2002-2017 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.metadata.exif;
22
23import com.drew.imaging.jpeg.JpegSegmentMetadataReader;
24import com.drew.imaging.jpeg.JpegSegmentType;
25import com.drew.imaging.tiff.TiffProcessingException;
26import com.drew.imaging.tiff.TiffReader;
27import com.drew.lang.ByteArrayReader;
28import com.drew.lang.RandomAccessReader;
29import com.drew.lang.annotations.NotNull;
30import com.drew.lang.annotations.Nullable;
31import com.drew.metadata.Directory;
32import com.drew.metadata.Metadata;
33
34import java.io.IOException;
35import 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")
45public class ExifReader implements JpegSegmentMetadataReader
46{
47 /** Exif data stored in JPEG files' APP1 segment are preceded by this six character preamble. */
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 // Filter any segments containing unexpected preambles
62 if (segmentBytes.length < JPEG_SEGMENT_PREAMBLE.length() || !new String(segmentBytes, 0, JPEG_SEGMENT_PREAMBLE.length()).equals(JPEG_SEGMENT_PREAMBLE))
63 continue;
64 extract(new ByteArrayReader(segmentBytes), metadata, JPEG_SEGMENT_PREAMBLE.length());
65 }
66 }
67
68 /** Reads TIFF formatted Exif data from start of the specified {@link RandomAccessReader}. */
69 public void extract(@NotNull final RandomAccessReader reader, @NotNull final Metadata metadata)
70 {
71 extract(reader, metadata, 0);
72 }
73
74 /** Reads TIFF formatted Exif data a specified offset within a {@link RandomAccessReader}. */
75 public void extract(@NotNull final RandomAccessReader reader, @NotNull final Metadata metadata, int readerOffset)
76 {
77 extract(reader, metadata, readerOffset, null);
78 }
79
80 /** Reads TIFF formatted Exif data at a specified offset within a {@link RandomAccessReader}. */
81 public void extract(@NotNull final RandomAccessReader reader, @NotNull final Metadata metadata, int readerOffset, @Nullable Directory parentDirectory)
82 {
83 ExifTiffHandler exifTiffHandler = new ExifTiffHandler(metadata, parentDirectory);
84
85 try {
86 // Read the TIFF-formatted Exif data
87 new TiffReader().processTiff(
88 reader,
89 exifTiffHandler,
90 readerOffset
91 );
92 } catch (TiffProcessingException e) {
93 exifTiffHandler.error("Exception processing TIFF data: " + e.getMessage());
94 // TODO what do to with this error state?
95 e.printStackTrace(System.err);
96 } catch (IOException e) {
97 exifTiffHandler.error("Exception processing TIFF data: " + e.getMessage());
98 // TODO what do to with this error state?
99 e.printStackTrace(System.err);
100 }
101 }
102}
Note: See TracBrowser for help on using the repository browser.