| 1 | /*
|
|---|
| 2 | * Copyright 2002-2012 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 | * http://drewnoakes.com/code/exif/
|
|---|
| 19 | * http://code.google.com/p/metadata-extractor/
|
|---|
| 20 | */
|
|---|
| 21 |
|
|---|
| 22 | package com.drew.metadata.exif;
|
|---|
| 23 |
|
|---|
| 24 | import com.drew.lang.Rational;
|
|---|
| 25 | import com.drew.lang.annotations.NotNull;
|
|---|
| 26 | import com.drew.lang.annotations.Nullable;
|
|---|
| 27 | import com.drew.metadata.TagDescriptor;
|
|---|
| 28 |
|
|---|
| 29 | import java.io.UnsupportedEncodingException;
|
|---|
| 30 |
|
|---|
| 31 | /**
|
|---|
| 32 | * Provides human-readable string representations of tag values stored in a <code>ExifIFD0Directory</code>.
|
|---|
| 33 | *
|
|---|
| 34 | * @author Drew Noakes http://drewnoakes.com
|
|---|
| 35 | */
|
|---|
| 36 | public class ExifIFD0Descriptor extends TagDescriptor<ExifIFD0Directory>
|
|---|
| 37 | {
|
|---|
| 38 | /**
|
|---|
| 39 | * Dictates whether rational values will be represented in decimal format in instances
|
|---|
| 40 | * where decimal notation is elegant (such as 1/2 -> 0.5, but not 1/3).
|
|---|
| 41 | */
|
|---|
| 42 | private final boolean _allowDecimalRepresentationOfRationals = true;
|
|---|
| 43 |
|
|---|
| 44 | public ExifIFD0Descriptor(@NotNull ExifIFD0Directory directory)
|
|---|
| 45 | {
|
|---|
| 46 | super(directory);
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | // Note for the potential addition of brightness presentation in eV:
|
|---|
| 50 | // Brightness of taken subject. To calculate Exposure(Ev) from BrightnessValue(Bv),
|
|---|
| 51 | // you must add SensitivityValue(Sv).
|
|---|
| 52 | // Ev=BV+Sv Sv=log2(ISOSpeedRating/3.125)
|
|---|
| 53 | // ISO100:Sv=5, ISO200:Sv=6, ISO400:Sv=7, ISO125:Sv=5.32.
|
|---|
| 54 |
|
|---|
| 55 | /**
|
|---|
| 56 | * Returns a descriptive value of the the specified tag for this image.
|
|---|
| 57 | * Where possible, known values will be substituted here in place of the raw
|
|---|
| 58 | * tokens actually kept in the Exif segment. If no substitution is
|
|---|
| 59 | * available, the value provided by getString(int) will be returned.
|
|---|
| 60 | * @param tagType the tag to find a description for
|
|---|
| 61 | * @return a description of the image's value for the specified tag, or
|
|---|
| 62 | * <code>null</code> if the tag hasn't been defined.
|
|---|
| 63 | */
|
|---|
| 64 | @Nullable
|
|---|
| 65 | public String getDescription(int tagType)
|
|---|
| 66 | {
|
|---|
| 67 | switch (tagType) {
|
|---|
| 68 | case ExifIFD0Directory.TAG_RESOLUTION_UNIT:
|
|---|
| 69 | return getResolutionDescription();
|
|---|
| 70 | case ExifIFD0Directory.TAG_YCBCR_POSITIONING:
|
|---|
| 71 | return getYCbCrPositioningDescription();
|
|---|
| 72 | case ExifIFD0Directory.TAG_X_RESOLUTION:
|
|---|
| 73 | return getXResolutionDescription();
|
|---|
| 74 | case ExifIFD0Directory.TAG_Y_RESOLUTION:
|
|---|
| 75 | return getYResolutionDescription();
|
|---|
| 76 | case ExifIFD0Directory.TAG_REFERENCE_BLACK_WHITE:
|
|---|
| 77 | return getReferenceBlackWhiteDescription();
|
|---|
| 78 | case ExifIFD0Directory.TAG_ORIENTATION:
|
|---|
| 79 | return getOrientationDescription();
|
|---|
| 80 |
|
|---|
| 81 | case ExifIFD0Directory.TAG_WIN_AUTHOR:
|
|---|
| 82 | return getWindowsAuthorDescription();
|
|---|
| 83 | case ExifIFD0Directory.TAG_WIN_COMMENT:
|
|---|
| 84 | return getWindowsCommentDescription();
|
|---|
| 85 | case ExifIFD0Directory.TAG_WIN_KEYWORDS:
|
|---|
| 86 | return getWindowsKeywordsDescription();
|
|---|
| 87 | case ExifIFD0Directory.TAG_WIN_SUBJECT:
|
|---|
| 88 | return getWindowsSubjectDescription();
|
|---|
| 89 | case ExifIFD0Directory.TAG_WIN_TITLE:
|
|---|
| 90 | return getWindowsTitleDescription();
|
|---|
| 91 |
|
|---|
| 92 | default:
|
|---|
| 93 | return super.getDescription(tagType);
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | @Nullable
|
|---|
| 98 | public String getReferenceBlackWhiteDescription()
|
|---|
| 99 | {
|
|---|
| 100 | int[] ints = _directory.getIntArray(ExifIFD0Directory.TAG_REFERENCE_BLACK_WHITE);
|
|---|
| 101 | if (ints==null)
|
|---|
| 102 | return null;
|
|---|
| 103 | int blackR = ints[0];
|
|---|
| 104 | int whiteR = ints[1];
|
|---|
| 105 | int blackG = ints[2];
|
|---|
| 106 | int whiteG = ints[3];
|
|---|
| 107 | int blackB = ints[4];
|
|---|
| 108 | int whiteB = ints[5];
|
|---|
| 109 | return "[" + blackR + "," + blackG + "," + blackB + "] " +
|
|---|
| 110 | "[" + whiteR + "," + whiteG + "," + whiteB + "]";
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | @Nullable
|
|---|
| 114 | public String getYResolutionDescription()
|
|---|
| 115 | {
|
|---|
| 116 | Rational value = _directory.getRational(ExifIFD0Directory.TAG_Y_RESOLUTION);
|
|---|
| 117 | if (value==null)
|
|---|
| 118 | return null;
|
|---|
| 119 | final String unit = getResolutionDescription();
|
|---|
| 120 | return value.toSimpleString(_allowDecimalRepresentationOfRationals) +
|
|---|
| 121 | " dots per " +
|
|---|
| 122 | (unit==null ? "unit" : unit.toLowerCase());
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | @Nullable
|
|---|
| 126 | public String getXResolutionDescription()
|
|---|
| 127 | {
|
|---|
| 128 | Rational value = _directory.getRational(ExifIFD0Directory.TAG_X_RESOLUTION);
|
|---|
| 129 | if (value==null)
|
|---|
| 130 | return null;
|
|---|
| 131 | final String unit = getResolutionDescription();
|
|---|
| 132 | return value.toSimpleString(_allowDecimalRepresentationOfRationals) +
|
|---|
| 133 | " dots per " +
|
|---|
| 134 | (unit==null ? "unit" : unit.toLowerCase());
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | @Nullable
|
|---|
| 138 | public String getYCbCrPositioningDescription()
|
|---|
| 139 | {
|
|---|
| 140 | Integer value = _directory.getInteger(ExifIFD0Directory.TAG_YCBCR_POSITIONING);
|
|---|
| 141 | if (value==null)
|
|---|
| 142 | return null;
|
|---|
| 143 | switch (value) {
|
|---|
| 144 | case 1: return "Center of pixel array";
|
|---|
| 145 | case 2: return "Datum point";
|
|---|
| 146 | default:
|
|---|
| 147 | return String.valueOf(value);
|
|---|
| 148 | }
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | @Nullable
|
|---|
| 152 | public String getOrientationDescription()
|
|---|
| 153 | {
|
|---|
| 154 | Integer value = _directory.getInteger(ExifIFD0Directory.TAG_ORIENTATION);
|
|---|
| 155 | if (value==null)
|
|---|
| 156 | return null;
|
|---|
| 157 | switch (value) {
|
|---|
| 158 | case 1: return "Top, left side (Horizontal / normal)";
|
|---|
| 159 | case 2: return "Top, right side (Mirror horizontal)";
|
|---|
| 160 | case 3: return "Bottom, right side (Rotate 180)";
|
|---|
| 161 | case 4: return "Bottom, left side (Mirror vertical)";
|
|---|
| 162 | case 5: return "Left side, top (Mirror horizontal and rotate 270 CW)";
|
|---|
| 163 | case 6: return "Right side, top (Rotate 90 CW)";
|
|---|
| 164 | case 7: return "Right side, bottom (Mirror horizontal and rotate 90 CW)";
|
|---|
| 165 | case 8: return "Left side, bottom (Rotate 270 CW)";
|
|---|
| 166 | default:
|
|---|
| 167 | return String.valueOf(value);
|
|---|
| 168 | }
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | @Nullable
|
|---|
| 172 | public String getResolutionDescription()
|
|---|
| 173 | {
|
|---|
| 174 | // '1' means no-unit, '2' means inch, '3' means centimeter. Default value is '2'(inch)
|
|---|
| 175 | Integer value = _directory.getInteger(ExifIFD0Directory.TAG_RESOLUTION_UNIT);
|
|---|
| 176 | if (value==null)
|
|---|
| 177 | return null;
|
|---|
| 178 | switch (value) {
|
|---|
| 179 | case 1: return "(No unit)";
|
|---|
| 180 | case 2: return "Inch";
|
|---|
| 181 | case 3: return "cm";
|
|---|
| 182 | default:
|
|---|
| 183 | return "";
|
|---|
| 184 | }
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | /** The Windows specific tags uses plain Unicode. */
|
|---|
| 188 | @Nullable
|
|---|
| 189 | private String getUnicodeDescription(int tag)
|
|---|
| 190 | {
|
|---|
| 191 | byte[] commentBytes = _directory.getByteArray(tag);
|
|---|
| 192 | if (commentBytes==null)
|
|---|
| 193 | return null;
|
|---|
| 194 | try {
|
|---|
| 195 | // Decode the unicode string and trim the unicode zero "\0" from the end.
|
|---|
| 196 | return new String(commentBytes, "UTF-16LE").trim();
|
|---|
| 197 | }
|
|---|
| 198 | catch (UnsupportedEncodingException ex) {
|
|---|
| 199 | return null;
|
|---|
| 200 | }
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | @Nullable
|
|---|
| 204 | public String getWindowsAuthorDescription()
|
|---|
| 205 | {
|
|---|
| 206 | return getUnicodeDescription(ExifIFD0Directory.TAG_WIN_AUTHOR);
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | @Nullable
|
|---|
| 210 | public String getWindowsCommentDescription()
|
|---|
| 211 | {
|
|---|
| 212 | return getUnicodeDescription(ExifIFD0Directory.TAG_WIN_COMMENT);
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | @Nullable
|
|---|
| 216 | public String getWindowsKeywordsDescription()
|
|---|
| 217 | {
|
|---|
| 218 | return getUnicodeDescription(ExifIFD0Directory.TAG_WIN_KEYWORDS);
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | @Nullable
|
|---|
| 222 | public String getWindowsTitleDescription()
|
|---|
| 223 | {
|
|---|
| 224 | return getUnicodeDescription(ExifIFD0Directory.TAG_WIN_TITLE);
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | @Nullable
|
|---|
| 228 | public String getWindowsSubjectDescription()
|
|---|
| 229 | {
|
|---|
| 230 | return getUnicodeDescription(ExifIFD0Directory.TAG_WIN_SUBJECT);
|
|---|
| 231 | }
|
|---|
| 232 | }
|
|---|