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

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

fix #11162 - update to metadata-extractor 2.7.2

File size: 4.2 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.annotations.NotNull;
29import com.drew.metadata.Metadata;
30
31import java.io.IOException;
32import java.util.Arrays;
33
34/**
35 * Decodes Exif binary data, populating a {@link Metadata} object with tag values in {@link ExifSubIFDDirectory},
36 * {@link ExifThumbnailDirectory}, {@link ExifInteropDirectory}, {@link GpsDirectory} and one of the many camera
37 * makernote directories.
38 *
39 * @author Drew Noakes https://drewnoakes.com
40 */
41public class ExifReader implements JpegSegmentMetadataReader
42{
43 /**
44 * The offset at which the TIFF data actually starts. This may be necessary when, for example, processing
45 * JPEG Exif data from APP0 which has a 6-byte preamble before starting the TIFF data.
46 */
47 private static final String JPEG_EXIF_SEGMENT_PREAMBLE = "Exif\0\0";
48
49 private boolean _storeThumbnailBytes = true;
50
51 public boolean isStoreThumbnailBytes()
52 {
53 return _storeThumbnailBytes;
54 }
55
56 public void setStoreThumbnailBytes(boolean storeThumbnailBytes)
57 {
58 _storeThumbnailBytes = storeThumbnailBytes;
59 }
60
61 @NotNull
62 public Iterable<JpegSegmentType> getSegmentTypes()
63 {
64 return Arrays.asList(JpegSegmentType.APP1);
65 }
66
67 public boolean canProcess(@NotNull final byte[] segmentBytes, @NotNull final JpegSegmentType segmentType)
68 {
69 return segmentBytes.length >= JPEG_EXIF_SEGMENT_PREAMBLE.length() && new String(segmentBytes, 0, JPEG_EXIF_SEGMENT_PREAMBLE.length()).equalsIgnoreCase(JPEG_EXIF_SEGMENT_PREAMBLE);
70 }
71
72 public void extract(@NotNull final byte[] segmentBytes, @NotNull final Metadata metadata, @NotNull final JpegSegmentType segmentType)
73 {
74 if (segmentBytes == null)
75 throw new NullPointerException("segmentBytes cannot be null");
76 if (metadata == null)
77 throw new NullPointerException("metadata cannot be null");
78 if (segmentType == null)
79 throw new NullPointerException("segmentType cannot be null");
80
81 try {
82 ByteArrayReader reader = new ByteArrayReader(segmentBytes);
83
84 //
85 // Check for the header preamble
86 //
87 try {
88 if (!reader.getString(0, JPEG_EXIF_SEGMENT_PREAMBLE.length()).equals(JPEG_EXIF_SEGMENT_PREAMBLE)) {
89 // TODO what do to with this error state?
90 System.err.println("Invalid JPEG Exif segment preamble");
91 return;
92 }
93 } catch (IOException e) {
94 // TODO what do to with this error state?
95 e.printStackTrace(System.err);
96 return;
97 }
98
99 //
100 // Read the TIFF-formatted Exif data
101 //
102 new TiffReader().processTiff(
103 reader,
104 new ExifTiffHandler(metadata, _storeThumbnailBytes),
105 JPEG_EXIF_SEGMENT_PREAMBLE.length()
106 );
107
108 } catch (TiffProcessingException e) {
109 // TODO what do to with this error state?
110 e.printStackTrace(System.err);
111 } catch (IOException e) {
112 // TODO what do to with this error state?
113 e.printStackTrace(System.err);
114 }
115 }
116}
Note: See TracBrowser for help on using the repository browser.