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

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

fix #11359 - update to metadata-extractor 2.8.1

File size: 3.7 KB
Line 
1/*
2 * Copyright 2002-2015 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.metadata.Metadata;
31
32import java.io.IOException;
33import java.util.Arrays;
34
35/**
36 * Decodes Exif binary data, populating a {@link Metadata} object with tag values in {@link ExifSubIFDDirectory},
37 * {@link ExifThumbnailDirectory}, {@link ExifInteropDirectory}, {@link GpsDirectory} and one of the many camera
38 * makernote directories.
39 *
40 * @author Drew Noakes https://drewnoakes.com
41 */
42public class ExifReader implements JpegSegmentMetadataReader
43{
44 /** Exif data stored in JPEG files' APP1 segment are preceded by this six character preamble. */
45 public static final String JPEG_SEGMENT_PREAMBLE = "Exif\0\0";
46
47 private boolean _storeThumbnailBytes = true;
48
49 public boolean isStoreThumbnailBytes()
50 {
51 return _storeThumbnailBytes;
52 }
53
54 public void setStoreThumbnailBytes(boolean storeThumbnailBytes)
55 {
56 _storeThumbnailBytes = storeThumbnailBytes;
57 }
58
59 @NotNull
60 public Iterable<JpegSegmentType> getSegmentTypes()
61 {
62 return Arrays.asList(JpegSegmentType.APP1);
63 }
64
65 public void readJpegSegments(@NotNull final Iterable<byte[]> segments, @NotNull final Metadata metadata, @NotNull final JpegSegmentType segmentType)
66 {
67 assert(segmentType == JpegSegmentType.APP1);
68
69 for (byte[] segmentBytes : segments) {
70 // Filter any segments containing unexpected preambles
71 if (segmentBytes.length < JPEG_SEGMENT_PREAMBLE.length() || !new String(segmentBytes, 0, JPEG_SEGMENT_PREAMBLE.length()).equals(JPEG_SEGMENT_PREAMBLE))
72 continue;
73 extract(new ByteArrayReader(segmentBytes), metadata, JPEG_SEGMENT_PREAMBLE.length());
74 }
75 }
76
77 /** Reads TIFF formatted Exif data from start of the specified {@link RandomAccessReader}. */
78 public void extract(@NotNull final RandomAccessReader reader, @NotNull final Metadata metadata)
79 {
80 extract(reader, metadata, 0);
81 }
82
83 /** Reads TIFF formatted Exif data a specified offset within a {@link RandomAccessReader}. */
84 public void extract(@NotNull final RandomAccessReader reader, @NotNull final Metadata metadata, int readerOffset)
85 {
86 try {
87 // Read the TIFF-formatted Exif data
88 new TiffReader().processTiff(
89 reader,
90 new ExifTiffHandler(metadata, _storeThumbnailBytes),
91 readerOffset
92 );
93 } catch (TiffProcessingException e) {
94 // TODO what do to with this error state?
95 e.printStackTrace(System.err);
96 } catch (IOException e) {
97 // TODO what do to with this error state?
98 e.printStackTrace(System.err);
99 }
100 }
101}
Note: See TracBrowser for help on using the repository browser.