| 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.lang.annotations.NotNull; |
| 24 | import com.drew.lang.annotations.Nullable; |
| 25 | import com.drew.metadata.Directory; |
| 26 | |
| 27 | import java.util.Date; |
| 28 | import java.util.HashMap; |
| 29 | import java.util.TimeZone; |
| 30 | |
| 31 | /** |
| 32 | * Describes Exif tags from the SubIFD directory. |
| 33 | * |
| 34 | * @author Drew Noakes https://drewnoakes.com |
| 35 | */ |
| 36 | @SuppressWarnings("WeakerAccess") |
| 37 | public class ExifSubIFDDirectory extends ExifDirectoryBase |
| 38 | { |
| 39 | /** This tag is a pointer to the Exif Interop IFD. */ |
| 40 | public static final int TAG_INTEROP_OFFSET = 0xA005; |
| 41 | |
| 42 | public ExifSubIFDDirectory() |
| 43 | { |
| 44 | this.setDescriptor(new ExifSubIFDDescriptor(this)); |
| 45 | } |
| 46 | |
| 47 | @NotNull |
| 48 | private static final HashMap<Integer, String> _tagNameMap = new HashMap<Integer, String>(); |
| 49 | |
| 50 | static |
| 51 | { |
| 52 | addExifTagNames(_tagNameMap); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | @NotNull |
| 57 | public String getName() |
| 58 | { |
| 59 | return "Exif SubIFD"; |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | @NotNull |
| 64 | protected HashMap<Integer, String> getTagNameMap() |
| 65 | { |
| 66 | return _tagNameMap; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Parses the date/time tag, the subsecond tag and the time offset tag to obtain a single Date |
| 71 | * object with milliseconds representing the date and time when this image was modified. If |
| 72 | * the time offset tag does not exist, attempts will be made to parse the values as though it is |
| 73 | * in the GMT {@link TimeZone}. |
| 74 | * |
| 75 | * @return A Date object representing when this image was modified, if possible, otherwise null |
| 76 | */ |
| 77 | @Nullable |
| 78 | public Date getDateModified() |
| 79 | { |
| 80 | return getDateModified(null); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Parses the date/time tag, the subsecond tag and the time offset tag to obtain a single Date |
| 85 | * object with milliseconds representing the date and time when this image was modified. If |
| 86 | * the time offset tag does not exist, attempts will be made to parse the values as though it is |
| 87 | * in the {@link TimeZone} represented by the {@code timeZone} parameter (if it is non-null). |
| 88 | * |
| 89 | * @param timeZone the time zone to use |
| 90 | * @return A Date object representing when this image was modified, if possible, otherwise null |
| 91 | */ |
| 92 | @Nullable |
| 93 | public Date getDateModified(@Nullable TimeZone timeZone) |
| 94 | { |
| 95 | Directory parent = getParent(); |
| 96 | if (parent instanceof ExifIFD0Directory) { |
| 97 | TimeZone timeZoneModified = getTimeZone(TAG_TIME_ZONE); |
| 98 | return parent.getDate(TAG_DATETIME, getString(TAG_SUBSECOND_TIME), |
| 99 | (timeZoneModified != null) ? timeZoneModified : timeZone); |
| 100 | } else { |
| 101 | return null; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Parses the date/time tag, the subsecond tag and the time offset tag to obtain a single Date |
| 107 | * object with milliseconds representing the date and time when this image was captured. If |
| 108 | * the time offset tag does not exist, attempts will be made to parse the values as though it is |
| 109 | * in the GMT {@link TimeZone}. |
| 110 | * |
| 111 | * @return A Date object representing when this image was captured, if possible, otherwise null |
| 112 | */ |
| 113 | @Nullable |
| 114 | public Date getDateOriginal() |
| 115 | { |
| 116 | return getDateOriginal(null); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Parses the date/time tag, the subsecond tag and the time offset tag to obtain a single Date |
| 121 | * object with milliseconds representing the date and time when this image was captured. If |
| 122 | * the time offset tag does not exist, attempts will be made to parse the values as though it is |
| 123 | * in the {@link TimeZone} represented by the {@code timeZone} parameter (if it is non-null). |
| 124 | * |
| 125 | * @param timeZone the time zone to use |
| 126 | * @return A Date object representing when this image was captured, if possible, otherwise null |
| 127 | */ |
| 128 | @Nullable |
| 129 | public Date getDateOriginal(@Nullable TimeZone timeZone) |
| 130 | { |
| 131 | TimeZone timeZoneOriginal = getTimeZone(TAG_TIME_ZONE_ORIGINAL); |
| 132 | return getDate(TAG_DATETIME_ORIGINAL, getString(TAG_SUBSECOND_TIME_ORIGINAL), |
| 133 | (timeZoneOriginal != null) ? timeZoneOriginal : timeZone); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Parses the date/time tag, the subsecond tag and the time offset tag to obtain a single Date |
| 138 | * object with milliseconds representing the date and time when this image was digitized. If |
| 139 | * the time offset tag does not exist, attempts will be made to parse the values as though it is |
| 140 | * in the GMT {@link TimeZone}. |
| 141 | * |
| 142 | * @return A Date object representing when this image was digitized, if possible, otherwise null |
| 143 | */ |
| 144 | @Nullable |
| 145 | public Date getDateDigitized() |
| 146 | { |
| 147 | return getDateDigitized(null); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Parses the date/time tag, the subsecond tag and the time offset tag to obtain a single Date |
| 152 | * object with milliseconds representing the date and time when this image was digitized. If |
| 153 | * the time offset tag does not exist, attempts will be made to parse the values as though it is |
| 154 | * in the {@link TimeZone} represented by the {@code timeZone} parameter (if it is non-null). |
| 155 | * |
| 156 | * @param timeZone the time zone to use |
| 157 | * @return A Date object representing when this image was digitized, if possible, otherwise null |
| 158 | */ |
| 159 | @Nullable |
| 160 | public Date getDateDigitized(@Nullable TimeZone timeZone) |
| 161 | { |
| 162 | TimeZone timeZoneDigitized = getTimeZone(TAG_TIME_ZONE_DIGITIZED); |
| 163 | return getDate(TAG_DATETIME_DIGITIZED, getString(TAG_SUBSECOND_TIME_DIGITIZED), |
| 164 | (timeZoneDigitized != null) ? timeZoneDigitized : timeZone); |
| 165 | } |
| 166 | |
| 167 | @Nullable |
| 168 | private TimeZone getTimeZone(int tagType) |
| 169 | { |
| 170 | String timeOffset = getString(tagType); |
| 171 | if (timeOffset != null && timeOffset.matches("[\\+\\-]\\d\\d:\\d\\d")) { |
| 172 | return TimeZone.getTimeZone("GMT" + timeOffset); |
| 173 | } else { |
| 174 | return null; |
| 175 | } |
| 176 | } |
| 177 | } |