From f32b98480c0c1678acd251fd88adb112d0118711 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Fri, 24 Mar 2017 02:32:49 +0800 Subject: [PATCH] Refactor ImageMetadata --- .../net/filebot/mediainfo/ImageMetadata.java | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/source/net/filebot/mediainfo/ImageMetadata.java b/source/net/filebot/mediainfo/ImageMetadata.java index 93c571c5..b0944ea3 100644 --- a/source/net/filebot/mediainfo/ImageMetadata.java +++ b/source/net/filebot/mediainfo/ImageMetadata.java @@ -16,6 +16,7 @@ import java.util.Map; import java.util.Optional; import java.util.function.Function; import java.util.function.Predicate; +import java.util.stream.Stream; import com.drew.imaging.ImageMetadataReader; import com.drew.imaging.ImageProcessingException; @@ -48,7 +49,7 @@ public class ImageMetadata { } public Map snapshot(Function key) { - return snapshot(key, d -> !d.getName().matches("JPEG|JFIF|Interoperability|Huffman|File")); + return snapshot(key, d -> Stream.of("JPEG", "JFIF", "Interoperability", "Huffman", "File").noneMatch(d.getName()::equals)); } public Map snapshot(Function key, Predicate accept) { @@ -69,16 +70,15 @@ public class ImageMetadata { } public Optional getDateTaken() { - return extract(m -> m.getFirstDirectoryOfType(ExifIFD0Directory.class).getDate(ExifSubIFDDirectory.TAG_DATETIME)).map(d -> { + return extract(m -> m.getFirstDirectoryOfType(ExifIFD0Directory.class)).map(d -> d.getDate(ExifSubIFDDirectory.TAG_DATETIME)).map(d -> { return d.toInstant().atZone(ZoneOffset.UTC); }); } public Optional> getCameraModel() { - return extract(m -> { - ExifIFD0Directory directory = m.getFirstDirectoryOfType(ExifIFD0Directory.class); - String maker = directory.getDescription(ExifIFD0Directory.TAG_MAKE); - String model = directory.getDescription(ExifIFD0Directory.TAG_MODEL); + return extract(m -> m.getFirstDirectoryOfType(ExifIFD0Directory.class)).map(d -> { + String maker = d.getDescription(ExifIFD0Directory.TAG_MAKE); + String model = d.getDescription(ExifIFD0Directory.TAG_MODEL); Map camera = new EnumMap(CameraProperty.class); if (maker != null) { @@ -87,8 +87,9 @@ public class ImageMetadata { if (model != null) { camera.put(CameraProperty.model, model); } + return camera; - }).filter(m -> m.size() > 0); + }).filter(m -> !m.isEmpty()); } public enum CameraProperty { @@ -96,14 +97,15 @@ public class ImageMetadata { } public Optional> getLocationTaken() { - return extract(m -> m.getFirstDirectoryOfType(GpsDirectory.class).getGeoLocation()).map(this::locate); + return extract(m -> m.getFirstDirectoryOfType(GpsDirectory.class)).map(GpsDirectory::getGeoLocation).map(this::locate); } protected Map locate(GeoLocation location) { try { // e.g. https://maps.googleapis.com/maps/api/geocode/json?latlng=40.7470444,-073.9411611 - Cache cache = Cache.getCache("geocode", CacheType.Monthly); - Object json = cache.json(location.getLatitude() + "," + location.getLongitude(), pos -> new URL("https://maps.googleapis.com/maps/api/geocode/json?latlng=" + pos)).get(); + Cache cache = Cache.getCache("geocode", CacheType.Persistent); + + Object json = cache.json(location.getLatitude() + "," + location.getLongitude(), p -> new URL("https://maps.googleapis.com/maps/api/geocode/json?latlng=" + p)).get(); Map address = new EnumMap(AddressComponent.class);