[8132] | 1 | /*
|
---|
[10862] | 2 | * Copyright 2002-2016 Drew Noakes
|
---|
[8132] | 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;
|
---|
[8243] | 28 | import com.drew.lang.RandomAccessReader;
|
---|
[8132] | 29 | import com.drew.lang.annotations.NotNull;
|
---|
[10862] | 30 | import com.drew.lang.annotations.Nullable;
|
---|
| 31 | import com.drew.metadata.Directory;
|
---|
[8132] | 32 | import com.drew.metadata.Metadata;
|
---|
| 33 |
|
---|
| 34 | import java.io.IOException;
|
---|
[10862] | 35 | import java.util.Collections;
|
---|
[8132] | 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 | public class ExifReader implements JpegSegmentMetadataReader
|
---|
| 45 | {
|
---|
[8243] | 46 | /** Exif data stored in JPEG files' APP1 segment are preceded by this six character preamble. */
|
---|
| 47 | public static final String JPEG_SEGMENT_PREAMBLE = "Exif\0\0";
|
---|
[8132] | 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 | {
|
---|
[10862] | 64 | return Collections.singletonList(JpegSegmentType.APP1);
|
---|
[8132] | 65 | }
|
---|
| 66 |
|
---|
[8243] | 67 | public void readJpegSegments(@NotNull final Iterable<byte[]> segments, @NotNull final Metadata metadata, @NotNull final JpegSegmentType segmentType)
|
---|
[8132] | 68 | {
|
---|
[8243] | 69 | assert(segmentType == JpegSegmentType.APP1);
|
---|
| 70 |
|
---|
| 71 | for (byte[] segmentBytes : segments) {
|
---|
| 72 | // Filter any segments containing unexpected preambles
|
---|
| 73 | if (segmentBytes.length < JPEG_SEGMENT_PREAMBLE.length() || !new String(segmentBytes, 0, JPEG_SEGMENT_PREAMBLE.length()).equals(JPEG_SEGMENT_PREAMBLE))
|
---|
| 74 | continue;
|
---|
| 75 | extract(new ByteArrayReader(segmentBytes), metadata, JPEG_SEGMENT_PREAMBLE.length());
|
---|
| 76 | }
|
---|
[8132] | 77 | }
|
---|
| 78 |
|
---|
[8243] | 79 | /** Reads TIFF formatted Exif data from start of the specified {@link RandomAccessReader}. */
|
---|
| 80 | public void extract(@NotNull final RandomAccessReader reader, @NotNull final Metadata metadata)
|
---|
[8132] | 81 | {
|
---|
[8243] | 82 | extract(reader, metadata, 0);
|
---|
| 83 | }
|
---|
[8132] | 84 |
|
---|
[8243] | 85 | /** Reads TIFF formatted Exif data a specified offset within a {@link RandomAccessReader}. */
|
---|
| 86 | public void extract(@NotNull final RandomAccessReader reader, @NotNull final Metadata metadata, int readerOffset)
|
---|
| 87 | {
|
---|
[10862] | 88 | extract(reader, metadata, readerOffset, null);
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | /** Reads TIFF formatted Exif data a specified offset within a {@link RandomAccessReader}. */
|
---|
| 92 | public void extract(@NotNull final RandomAccessReader reader, @NotNull final Metadata metadata, int readerOffset, @Nullable Directory parentDirectory)
|
---|
| 93 | {
|
---|
[8132] | 94 | try {
|
---|
| 95 | // Read the TIFF-formatted Exif data
|
---|
| 96 | new TiffReader().processTiff(
|
---|
| 97 | reader,
|
---|
[10862] | 98 | new ExifTiffHandler(metadata, _storeThumbnailBytes, parentDirectory),
|
---|
[8243] | 99 | readerOffset
|
---|
[8132] | 100 | );
|
---|
| 101 | } catch (TiffProcessingException e) {
|
---|
| 102 | // TODO what do to with this error state?
|
---|
| 103 | e.printStackTrace(System.err);
|
---|
| 104 | } catch (IOException e) {
|
---|
| 105 | // TODO what do to with this error state?
|
---|
| 106 | e.printStackTrace(System.err);
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 | }
|
---|