001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.tools; 003 004import java.awt.geom.AffineTransform; 005import java.io.File; 006import java.io.IOException; 007import java.time.DateTimeException; 008import java.time.Instant; 009import java.util.List; 010import java.util.concurrent.TimeUnit; 011 012import org.openstreetmap.josm.data.SystemOfMeasurement; 013import org.openstreetmap.josm.data.coor.LatLon; 014import org.openstreetmap.josm.tools.date.DateUtils; 015 016import com.drew.imaging.jpeg.JpegMetadataReader; 017import com.drew.imaging.jpeg.JpegProcessingException; 018import com.drew.lang.Rational; 019import com.drew.metadata.Directory; 020import com.drew.metadata.Metadata; 021import com.drew.metadata.MetadataException; 022import com.drew.metadata.Tag; 023import com.drew.metadata.exif.ExifDirectoryBase; 024import com.drew.metadata.exif.ExifIFD0Directory; 025import com.drew.metadata.exif.GpsDirectory; 026import com.drew.metadata.iptc.IptcDirectory; 027 028/** 029 * Read out EXIF information from a JPEG file 030 * @author Imi 031 * @since 99 032 */ 033public final class ExifReader { 034 035 private ExifReader() { 036 // Hide default constructor for utils classes 037 } 038 039 /** 040 * Returns the date/time from the given JPEG file. 041 * @param filename The JPEG file to read 042 * @return The date/time read in the EXIF section, or {@code null} if not found 043 */ 044 public static Instant readInstant(File filename) { 045 try { 046 final Metadata metadata = JpegMetadataReader.readMetadata(filename); 047 return readInstant(metadata); 048 } catch (JpegProcessingException | IOException e) { 049 Logging.error(e); 050 } 051 return null; 052 } 053 054 /** 055 * Returns the date/time from the given JPEG file. 056 * @param metadata The EXIF metadata 057 * @return The date/time read in the EXIF section, or {@code null} if not found 058 */ 059 public static Instant readInstant(Metadata metadata) { 060 try { 061 String dateTimeOrig = null; 062 String dateTime = null; 063 String dateTimeDig = null; 064 String subSecOrig = null; 065 String subSec = null; 066 String subSecDig = null; 067 // The date fields are preferred in this order: DATETIME_ORIGINAL 068 // (0x9003), DATETIME (0x0132), DATETIME_DIGITIZED (0x9004). Some 069 // cameras store the fields in the wrong directory, so all 070 // directories are searched. Assume that the order of the fields 071 // in the directories is random. 072 for (Directory dirIt : metadata.getDirectories()) { 073 if (!(dirIt instanceof ExifDirectoryBase)) { 074 continue; 075 } 076 for (Tag tag : dirIt.getTags()) { 077 if (tag.getTagType() == ExifDirectoryBase.TAG_DATETIME_ORIGINAL /* 0x9003 */ && 078 !tag.getDescription().matches("\\[\\d+ .+]")) { 079 dateTimeOrig = tag.getDescription(); 080 } else if (tag.getTagType() == ExifDirectoryBase.TAG_DATETIME /* 0x0132 */) { 081 dateTime = tag.getDescription(); 082 } else if (tag.getTagType() == ExifDirectoryBase.TAG_DATETIME_DIGITIZED /* 0x9004 */) { 083 dateTimeDig = tag.getDescription(); 084 } else if (tag.getTagType() == ExifDirectoryBase.TAG_SUBSECOND_TIME_ORIGINAL /* 0x9291 */) { 085 subSecOrig = tag.getDescription(); 086 } else if (tag.getTagType() == ExifDirectoryBase.TAG_SUBSECOND_TIME /* 0x9290 */) { 087 subSec = tag.getDescription(); 088 } else if (tag.getTagType() == ExifDirectoryBase.TAG_SUBSECOND_TIME_DIGITIZED /* 0x9292 */) { 089 subSecDig = tag.getDescription(); 090 } 091 } 092 } 093 String dateStr = null; 094 String subSeconds = null; 095 if (dateTimeOrig != null) { 096 // prefer TAG_DATETIME_ORIGINAL 097 dateStr = dateTimeOrig; 098 subSeconds = subSecOrig; 099 } else if (dateTime != null) { 100 // TAG_DATETIME is second choice, see #14209 101 dateStr = dateTime; 102 subSeconds = subSec; 103 } else if (dateTimeDig != null) { 104 dateStr = dateTimeDig; 105 subSeconds = subSecDig; 106 } 107 if (dateStr != null) { 108 dateStr = dateStr.replace('/', ':'); // workaround for HTC Sensation bug, see #7228 109 Instant date = DateUtils.parseInstant(dateStr); 110 if (subSeconds != null) { 111 try { 112 date = date.plusMillis((long) (TimeUnit.SECONDS.toMillis(1) * Double.parseDouble("0." + subSeconds))); 113 } catch (NumberFormatException e) { 114 Logging.warn("Failed parsing sub seconds from [{0}]", subSeconds); 115 Logging.warn(e); 116 } 117 } 118 return date; 119 } 120 } catch (UncheckedParseException | DateTimeException e) { 121 Logging.error(e); 122 } 123 return null; 124 } 125 126 /** 127 * Returns the GPS date/time from the given JPEG file. 128 * @param filename The JPEG file to read 129 * @return The GPS date/time read in the EXIF section, or {@code null} if not found 130 * @since 19387 131 */ 132 public static Instant readGpsInstant(File filename) { 133 try { 134 final Metadata metadata = JpegMetadataReader.readMetadata(filename); 135 final GpsDirectory dirGps = metadata.getFirstDirectoryOfType(GpsDirectory.class); 136 return readGpsInstant(dirGps); 137 } catch (JpegProcessingException | IOException e) { 138 Logging.error(e); 139 } 140 return null; 141 } 142 143 /** 144 * Returns the GPS date/time from the given JPEG file. 145 * @param dirGps The EXIF GPS directory 146 * @return The GPS date/time read in the EXIF section, or {@code null} if not found 147 * @since 19387 148 */ 149 public static Instant readGpsInstant(GpsDirectory dirGps) { 150 if (dirGps != null) { 151 try { 152 return dirGps.getGpsDate().toInstant(); 153 } catch (UncheckedParseException | DateTimeException e) { 154 Logging.error(e); 155 } 156 } 157 return null; 158 } 159 160 /** 161 * Returns the image orientation of the given JPEG file. 162 * @param filename The JPEG file to read 163 * @return The image orientation as an {@code int}. Default value is 1. Possible values are listed in EXIF spec as follows:<br><ol> 164 * <li>The 0th row is at the visual top of the image, and the 0th column is the visual left-hand side.</li> 165 * <li>The 0th row is at the visual top of the image, and the 0th column is the visual right-hand side.</li> 166 * <li>The 0th row is at the visual bottom of the image, and the 0th column is the visual right-hand side.</li> 167 * <li>The 0th row is at the visual bottom of the image, and the 0th column is the visual left-hand side.</li> 168 * <li>The 0th row is the visual left-hand side of the image, and the 0th column is the visual top.</li> 169 * <li>The 0th row is the visual right-hand side of the image, and the 0th column is the visual top.</li> 170 * <li>The 0th row is the visual right-hand side of the image, and the 0th column is the visual bottom.</li> 171 * <li>The 0th row is the visual left-hand side of the image, and the 0th column is the visual bottom.</li></ol> 172 * @see <a href="http://www.impulseadventure.com/photo/exif-orientation.html">http://www.impulseadventure.com/photo/exif-orientation.html</a> 173 * @see <a href="http://www.daveperrett.com/articles/2012/07/28/exif-orientation-handling-is-a-ghetto"> 174 * http://www.daveperrett.com/articles/2012/07/28/exif-orientation-handling-is-a-ghetto</a> 175 */ 176 public static Integer readOrientation(File filename) { 177 try { 178 final Metadata metadata = JpegMetadataReader.readMetadata(filename); 179 final Directory dir = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class); 180 return dir == null ? null : dir.getInteger(ExifDirectoryBase.TAG_ORIENTATION); 181 } catch (JpegProcessingException | IOException e) { 182 Logging.error(e); 183 } 184 return null; 185 } 186 187 /** 188 * Returns the geolocation of the given JPEG file. 189 * @param filename The JPEG file to read 190 * @return The lat/lon read in the EXIF section, or {@code null} if not found 191 * @since 6209 192 */ 193 public static LatLon readLatLon(File filename) { 194 try { 195 final Metadata metadata = JpegMetadataReader.readMetadata(filename); 196 final GpsDirectory dirGps = metadata.getFirstDirectoryOfType(GpsDirectory.class); 197 return readLatLon(dirGps); 198 } catch (JpegProcessingException | IOException | MetadataException e) { 199 Logging.error(e); 200 } 201 return null; 202 } 203 204 /** 205 * Returns the geolocation of the given EXIF GPS directory. 206 * @param dirGps The EXIF GPS directory 207 * @return The lat/lon read in the EXIF section, or {@code null} if {@code dirGps} is null 208 * @throws MetadataException if invalid metadata is given 209 * @since 6209 210 */ 211 public static LatLon readLatLon(GpsDirectory dirGps) throws MetadataException { 212 if (dirGps != null && dirGps.containsTag(GpsDirectory.TAG_LATITUDE) && dirGps.containsTag(GpsDirectory.TAG_LONGITUDE)) { 213 double lat = readAxis(dirGps, GpsDirectory.TAG_LATITUDE, GpsDirectory.TAG_LATITUDE_REF, 'S'); 214 double lon = readAxis(dirGps, GpsDirectory.TAG_LONGITUDE, GpsDirectory.TAG_LONGITUDE_REF, 'W'); 215 return new LatLon(lat, lon); 216 } 217 return null; 218 } 219 220 /** 221 * Returns the direction of the given JPEG file. 222 * @param filename The JPEG file to read 223 * @return The direction of the image when it was captures (in degrees between 0.0 and 359.99), 224 * or {@code null} if not found 225 * @since 6209 226 */ 227 public static Double readDirection(File filename) { 228 try { 229 final Metadata metadata = JpegMetadataReader.readMetadata(filename); 230 final GpsDirectory dirGps = metadata.getFirstDirectoryOfType(GpsDirectory.class); 231 return readDirection(dirGps); 232 } catch (JpegProcessingException | IOException e) { 233 Logging.error(e); 234 } 235 return null; 236 } 237 238 /** 239 * Returns the direction of the given EXIF GPS directory. 240 * @param dirGps The EXIF GPS directory 241 * @return The direction of the image when it was captured (in degrees between 0.0 and 359.99), 242 * or {@code null} if missing or if {@code dirGps} is null 243 * @since 6209 244 */ 245 public static Double readDirection(GpsDirectory dirGps) { 246 if (dirGps != null) { 247 Rational direction = dirGps.getRational(GpsDirectory.TAG_IMG_DIRECTION); 248 if (direction != null) { 249 return direction.doubleValue(); 250 } 251 } 252 return null; 253 } 254 255 /** 256 * Returns the GPS track direction of the given JPEG file. 257 * @param filename The JPEG file to read 258 * @return The GPS track direction of the image when it was captures (in degrees between 0.0 and 359.99), 259 * or {@code null} if not found 260 * @since 19387 261 */ 262 public static Double readGpsTrackDirection(File filename) { 263 try { 264 final Metadata metadata = JpegMetadataReader.readMetadata(filename); 265 final GpsDirectory dirGps = metadata.getFirstDirectoryOfType(GpsDirectory.class); 266 return readGpsTrackDirection(dirGps); 267 } catch (JpegProcessingException | IOException e) { 268 Logging.error(e); 269 } 270 return null; 271 } 272 273 /** 274 * Returns the GPS track direction of the given EXIF GPS directory. 275 * @param dirGps The EXIF GPS directory 276 * @return The GPS track direction of the image when it was captured (in degrees between 0.0 and 359.99), 277 * or {@code null} if missing or if {@code dirGps} is null 278 * @since 19387 279 */ 280 public static Double readGpsTrackDirection(GpsDirectory dirGps) { 281 if (dirGps != null) { 282 Rational trackDirection = dirGps.getRational(GpsDirectory.TAG_TRACK); 283 if (trackDirection != null) { 284 return trackDirection.doubleValue(); 285 } 286 } 287 return null; 288 } 289 290 private static double readAxis(GpsDirectory dirGps, int gpsTag, int gpsTagRef, char cRef) throws MetadataException { 291 double value; 292 Rational[] components = dirGps.getRationalArray(gpsTag); 293 if (components != null) { 294 double deg = components[0].doubleValue(); 295 double min = components[1].doubleValue(); 296 double sec = components[2].doubleValue(); 297 298 if (Double.isNaN(deg) && Double.isNaN(min) && Double.isNaN(sec)) 299 throw new IllegalArgumentException("deg, min and sec are NaN"); 300 301 value = Double.isNaN(deg) ? 0 : deg + (Double.isNaN(min) ? 0 : (min / 60)) + (Double.isNaN(sec) ? 0 : (sec / 3600)); 302 303 String s = dirGps.getString(gpsTagRef); 304 if (s != null && s.charAt(0) == cRef) { 305 value = -value; 306 } 307 } else { 308 // Try to read lon/lat as double value (Nonstandard, created by some cameras -> #5220) 309 value = dirGps.getDouble(gpsTag); 310 } 311 return value; 312 } 313 314 /** 315 * Returns the speed of the given JPEG file. 316 * @param filename The JPEG file to read 317 * @return The speed of the camera when the image was captured (in km/h), 318 * or {@code null} if not found 319 * @since 11745 320 */ 321 public static Double readSpeed(File filename) { 322 try { 323 final Metadata metadata = JpegMetadataReader.readMetadata(filename); 324 final GpsDirectory dirGps = metadata.getFirstDirectoryOfType(GpsDirectory.class); 325 return readSpeed(dirGps); 326 } catch (JpegProcessingException | IOException e) { 327 Logging.error(e); 328 } 329 return null; 330 } 331 332 /** 333 * Returns the speed of the given EXIF GPS directory. 334 * @param dirGps The EXIF GPS directory 335 * @return The speed of the camera when the image was captured (in km/h), 336 * or {@code null} if missing or if {@code dirGps} is null 337 * @since 11745 338 */ 339 public static Double readSpeed(GpsDirectory dirGps) { 340 if (dirGps != null) { 341 Double speed = dirGps.getDoubleObject(GpsDirectory.TAG_SPEED); 342 if (speed != null) { 343 final String speedRef = dirGps.getString(GpsDirectory.TAG_SPEED_REF); 344 if ("M".equalsIgnoreCase(speedRef)) { 345 // miles per hour 346 speed *= SystemOfMeasurement.IMPERIAL.bValue / 1000; 347 } else if ("N".equalsIgnoreCase(speedRef)) { 348 // knots == nautical miles per hour 349 speed *= SystemOfMeasurement.NAUTICAL_MILE.bValue / 1000; 350 } 351 // default is K (km/h) 352 return speed; 353 } 354 } 355 return null; 356 } 357 358 /** 359 * Returns the elevation of the given JPEG file. 360 * @param filename The JPEG file to read 361 * @return The elevation of the camera when the image was captured (in m), 362 * or {@code null} if not found 363 * @since 11745 364 */ 365 public static Double readElevation(File filename) { 366 try { 367 return readElevation(JpegMetadataReader.readMetadata(filename).getFirstDirectoryOfType(GpsDirectory.class)); 368 } catch (JpegProcessingException | IOException e) { 369 Logging.error(e); 370 return null; 371 } 372 } 373 374 /** 375 * Returns the elevation of the given EXIF GPS directory. 376 * @param dirGps The EXIF GPS directory 377 * @return The elevation of the camera when the image was captured (in m), 378 * or {@code null} if missing or if {@code dirGps} is null 379 * @since 11745 380 */ 381 public static Double readElevation(GpsDirectory dirGps) { 382 if (dirGps != null) { 383 Double ele = dirGps.getDoubleObject(GpsDirectory.TAG_ALTITUDE); 384 if (ele != null) { 385 final Integer d = dirGps.getInteger(GpsDirectory.TAG_ALTITUDE_REF); 386 if (d != null && d == 1) { 387 ele *= -1; 388 } 389 return ele; 390 } 391 } 392 return null; 393 } 394 395 /** 396 * Returns the GPS horizontal positionning error of the given JPEG file. 397 * @param filename The JPEG file to read 398 * @return The GPS horizontal positionning error of the camera when the image was captured (in m), 399 * or {@code null} if not found 400 * @since 19387 401 */ 402 public static Double readHpositioningError(File filename) { 403 try { 404 final Metadata metadata = JpegMetadataReader.readMetadata(filename); 405 final GpsDirectory dirGps = metadata.getFirstDirectoryOfType(GpsDirectory.class); 406 return readHpositioningError(dirGps); 407 } catch (JpegProcessingException | IOException e) { 408 Logging.error(e); 409 } 410 return null; 411 } 412 413 /** 414 * Returns the GPS horizontal positionning error of the given EXIF GPS directory. 415 * @param dirGps The EXIF GPS directory 416 * @return The GPS horizontal positionning error of the camera when the image was captured (in m), 417 * or {@code null} if missing or if {@code dirGps} is null 418 * @since 19387 419 */ 420 public static Double readHpositioningError(GpsDirectory dirGps) { 421 if (dirGps != null) { 422 Double hposerr = dirGps.getDoubleObject(GpsDirectory.TAG_H_POSITIONING_ERROR); 423 if (hposerr != null) 424 return hposerr; 425 } 426 return null; 427 } 428 429 /** 430 * Returns the GPS differential mode of the given JPEG file. 431 * @param filename The JPEG file to read 432 * @return The GPS differential mode of the camera when the image was captured, 433 * <ul> 434 * <li>0 : no differential correction</li> 435 * <li>1 : differential correction</li> 436 * <li>or {@code null} if not found</li> 437 * </ul> 438 * @since 19387 439 */ 440 public static Integer readGpsDiffMode(File filename) { 441 try { 442 final Metadata metadata = JpegMetadataReader.readMetadata(filename); 443 final GpsDirectory dirGps = metadata.getFirstDirectoryOfType(GpsDirectory.class); 444 return readGpsDiffMode(dirGps); 445 } catch (JpegProcessingException | IOException e) { 446 Logging.error(e); 447 } 448 return null; 449 } 450 451 /** 452 * Returns the GPS differential mode of the given EXIF GPS directory. 453 * @param dirGps The EXIF GPS directory 454 * @return The GPS differential mode of the camera when the image was captured, 455 * <ul> 456 * <li>0 : no differential correction</li> 457 * <li>1 : differential correction</li> 458 * <li>or {@code null} if missing or if {@code dirGps} is null</li> 459 * </ul> 460 * @since 19387 461 */ 462 public static Integer readGpsDiffMode(GpsDirectory dirGps) { 463 if (dirGps != null) { 464 Integer gpsDiffMode = dirGps.getInteger(GpsDirectory.TAG_DIFFERENTIAL); 465 if (gpsDiffMode != null) 466 return gpsDiffMode; 467 } 468 return null; 469 } 470 471 /** 472 * Returns the GPS 2d/3d mode of the given JPEG file. 473 * @param filename The JPEG file to read 474 * @return The GPS 2d/3d mode of the camera when the image was captured, 475 * <ul> 476 * <li>2 : 2d mode</li> 477 * <li>2 : 3d mode</li> 478 * <li>or {@code null} if not found</li> 479 * </ul> 480 * @since 19387 481 */ 482 public static Integer readGpsMeasureMode(File filename) { 483 try { 484 final Metadata metadata = JpegMetadataReader.readMetadata(filename); 485 final GpsDirectory dirGps = metadata.getFirstDirectoryOfType(GpsDirectory.class); 486 return readGpsMeasureMode(dirGps); 487 } catch (JpegProcessingException | IOException e) { 488 Logging.error(e); 489 } 490 return null; 491 } 492 493 /** 494 * Returns the GPS 2d/3d mode of the given EXIF GPS directory. 495 * @param dirGps The EXIF GPS directory 496 * @return The 2d/3d mode of the camera when the image was captured, 497 * <ul> 498 * <li>2 : 2d mode</li> 499 * <li>3 : 3d mode</li> 500 * <li>or {@code null} if missing or if {@code dirGps} is null</li> 501 * </ul> 502 * @since 19387 503 */ 504 public static Integer readGpsMeasureMode(GpsDirectory dirGps) { 505 if (dirGps != null) { 506 Integer gps2d3dMode = dirGps.getInteger(GpsDirectory.TAG_MEASURE_MODE); 507 if (gps2d3dMode != null) 508 return gps2d3dMode; 509 } 510 return null; 511 } 512 513 /** 514 * Returns the GPS DOP value of the given JPEG file. 515 * @param filename The JPEG file to read 516 * @return The GPS DOP value of the camera when the image was captured, 517 * or {@code null} if not found 518 * @since 19387 519 */ 520 public static Double readGpsDop(File filename) { 521 try { 522 final Metadata metadata = JpegMetadataReader.readMetadata(filename); 523 final GpsDirectory dirGps = metadata.getFirstDirectoryOfType(GpsDirectory.class); 524 return readGpsDop(dirGps); 525 } catch (JpegProcessingException | IOException e) { 526 Logging.error(e); 527 } 528 return null; 529 } 530 531 /** 532 * Returns the GPS DOP value of the given EXIF GPS directory. 533 * @param dirGps The EXIF GPS directory 534 * @return The GPS DOP value of the camera when the image was captured, 535 * or {@code null} if missing or if {@code dirGps} is null 536 * @since 19387 537 */ 538 public static Double readGpsDop(GpsDirectory dirGps) { 539 if (dirGps != null) { 540 Double gpsDop = dirGps.getDoubleObject(GpsDirectory.TAG_DOP); 541 if (gpsDop != null) { 542 return gpsDop; 543 } 544 } 545 return null; 546 } 547 548 /** 549 * Returns the GPS datum value of the given JPEG file. 550 * @param filename The JPEG file to read 551 * @return The GPS datum value of the camera when the image was captured, 552 * or {@code null} if not found 553 * @since 19387 554 */ 555 public static String readGpsDatum(File filename) { 556 try { 557 final Metadata metadata = JpegMetadataReader.readMetadata(filename); 558 final GpsDirectory dirGps = metadata.getFirstDirectoryOfType(GpsDirectory.class); 559 return readGpsDatum(dirGps); 560 } catch (JpegProcessingException | IOException e) { 561 Logging.error(e); 562 } 563 return null; 564 } 565 566 /** 567 * Returns the GPS datum value of the given EXIF GPS directory. 568 * @param dirGps The EXIF GPS directory 569 * @return The GPS datum value of the camera when the image was captured, 570 * or {@code null} if missing or if {@code dirGps} is null 571 * @since 19387 572 */ 573 public static String readGpsDatum(GpsDirectory dirGps) { 574 if (dirGps != null) { 575 String gpsDatum = dirGps.getString(GpsDirectory.TAG_MAP_DATUM); 576 if (gpsDatum != null) 577 return gpsDatum; 578 } 579 return null; 580 } 581 582 /** 583 * Return the GPS processing method of the given JPEG file. 584 * @param filename The JPEG file to read 585 * @return The GPS processing method. Possible values from the EXIF specs are: 586 * <ul> 587 * <li>GPS</li> 588 * <li>QZSS</li> 589 * <li>GALILEO</li> 590 * <li>GLONASS</li> 591 * <li>BEIDOU</li> 592 * <li>NAVIC</li> 593 * <li>CELLID</li> 594 * <li>WLAN</li> 595 * <li>MANUAL</li> 596 * </ul> 597 * Other values, and combined space separated values are possible too. 598 * or {@code null} if missing 599 * @since 19387 600 */ 601 public static String readGpsProcessingMethod(File filename) { 602 try { 603 final Metadata metadata = JpegMetadataReader.readMetadata(filename); 604 final GpsDirectory dirGps = metadata.getFirstDirectoryOfType(GpsDirectory.class); 605 return readGpsProcessingMethod(dirGps); 606 } catch (JpegProcessingException | IOException e) { 607 Logging.error(e); 608 } 609 return null; 610 } 611 612 /** 613 * Return the GPS processing method of the given EXIF GPS directory. 614 * @param dirGps The EXIF GPS directory 615 * @return The GPS processing method. Possible values from the EXIF specs are: 616 * <ul> 617 * <li>GPS</li> 618 * <li>QZSS</li> 619 * <li>GALILEO</li> 620 * <li>GLONASS</li> 621 * <li>BEIDOU</li> 622 * <li>NAVIC</li> 623 * <li>CELLID</li> 624 * <li>WLAN</li> 625 * <li>MANUAL</li> 626 * </ul> 627 * Other values, and combined space separated values are possible too. 628 * or {@code null} if missing or if {@code dirGps} is null 629 * @since 19387 630 */ 631 public static String readGpsProcessingMethod(GpsDirectory dirGps) { 632 if (dirGps != null) { 633 String gpsProcessingMethod = dirGps.getDescription(GpsDirectory.TAG_PROCESSING_METHOD); 634 if (gpsProcessingMethod != null) 635 return gpsProcessingMethod; 636 } 637 return null; 638 } 639 640 /** 641 * Returns the caption of the given IPTC directory. 642 * @param dirIptc The IPTC directory 643 * @return The caption entered, or {@code null} if missing or if {@code dirIptc} is null 644 * @since 15219 645 */ 646 public static String readCaption(IptcDirectory dirIptc) { 647 return dirIptc == null ? null : dirIptc.getDescription(IptcDirectory.TAG_CAPTION); 648 } 649 650 /** 651 * Returns the headline of the given IPTC directory. 652 * @param dirIptc The IPTC directory 653 * @return The headline entered, or {@code null} if missing or if {@code dirIptc} is null 654 * @since 15219 655 */ 656 public static String readHeadline(IptcDirectory dirIptc) { 657 return dirIptc == null ? null : dirIptc.getDescription(IptcDirectory.TAG_HEADLINE); 658 } 659 660 /** 661 * Returns the keywords of the given IPTC directory. 662 * @param dirIptc The IPTC directory 663 * @return The keywords entered, or {@code null} if missing or if {@code dirIptc} is null 664 * @since 15219 665 */ 666 public static List<String> readKeywords(IptcDirectory dirIptc) { 667 return dirIptc == null ? null : dirIptc.getKeywords(); 668 } 669 670 /** 671 * Returns the object name of the given IPTC directory. 672 * @param dirIptc The IPTC directory 673 * @return The object name entered, or {@code null} if missing or if {@code dirIptc} is null 674 * @since 15219 675 */ 676 public static String readObjectName(IptcDirectory dirIptc) { 677 return dirIptc == null ? null : dirIptc.getDescription(IptcDirectory.TAG_OBJECT_NAME); 678 } 679 680 /** 681 * Returns a Transform that fixes the image orientation. 682 * <p> 683 * Only orientation 1, 3, 6 and 8 are supported. Everything else is treated as 1. 684 * @param orientation the exif-orientation of the image 685 * @param width the original width of the image 686 * @param height the original height of the image 687 * @return a transform that rotates the image, so it is upright 688 */ 689 public static AffineTransform getRestoreOrientationTransform(final int orientation, final int width, final int height) { 690 final int q; 691 final double ax; 692 final double ay; 693 switch (orientation) { 694 case 8: 695 q = -1; 696 ax = width / 2d; 697 ay = width / 2d; 698 break; 699 case 3: 700 q = 2; 701 ax = width / 2d; 702 ay = height / 2d; 703 break; 704 case 6: 705 q = 1; 706 ax = height / 2d; 707 ay = height / 2d; 708 break; 709 default: 710 q = 0; 711 ax = 0; 712 ay = 0; 713 } 714 return AffineTransform.getQuadrantRotateInstance(q, ax, ay); 715 } 716 717 /** 718 * Check, if the given orientation switches width and height of the image. 719 * E.g. 90 degree rotation 720 * <p> 721 * Only orientation 1, 3, 6 and 8 are supported. Everything else is treated 722 * as 1. 723 * @param orientation the exif-orientation of the image 724 * @return true, if it switches width and height 725 */ 726 public static boolean orientationSwitchesDimensions(int orientation) { 727 return orientation == 6 || orientation == 8; 728 } 729 730 /** 731 * Check, if the given orientation requires any correction to the image. 732 * <p> 733 * Only orientation 1, 3, 6 and 8 are supported. Everything else is treated 734 * as 1. 735 * @param orientation the exif-orientation of the image 736 * @return true, unless the orientation value is 1 or unsupported. 737 */ 738 public static boolean orientationNeedsCorrection(int orientation) { 739 return orientation == 3 || orientation == 6 || orientation == 8; 740 } 741}