Refactor ImageMetadata

This commit is contained in:
Reinhard Pointner 2017-03-24 02:32:49 +08:00
parent aa3c9b24b7
commit f32b98480c
1 changed files with 12 additions and 10 deletions

View File

@ -16,6 +16,7 @@ import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.stream.Stream;
import com.drew.imaging.ImageMetadataReader; import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException; import com.drew.imaging.ImageProcessingException;
@ -48,7 +49,7 @@ public class ImageMetadata {
} }
public Map<String, String> snapshot(Function<Tag, String> key) { public Map<String, String> snapshot(Function<Tag, String> 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<String, String> snapshot(Function<Tag, String> key, Predicate<Directory> accept) { public Map<String, String> snapshot(Function<Tag, String> key, Predicate<Directory> accept) {
@ -69,16 +70,15 @@ public class ImageMetadata {
} }
public Optional<ZonedDateTime> getDateTaken() { public Optional<ZonedDateTime> 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); return d.toInstant().atZone(ZoneOffset.UTC);
}); });
} }
public Optional<Map<CameraProperty, String>> getCameraModel() { public Optional<Map<CameraProperty, String>> getCameraModel() {
return extract(m -> { return extract(m -> m.getFirstDirectoryOfType(ExifIFD0Directory.class)).map(d -> {
ExifIFD0Directory directory = m.getFirstDirectoryOfType(ExifIFD0Directory.class); String maker = d.getDescription(ExifIFD0Directory.TAG_MAKE);
String maker = directory.getDescription(ExifIFD0Directory.TAG_MAKE); String model = d.getDescription(ExifIFD0Directory.TAG_MODEL);
String model = directory.getDescription(ExifIFD0Directory.TAG_MODEL);
Map<CameraProperty, String> camera = new EnumMap<CameraProperty, String>(CameraProperty.class); Map<CameraProperty, String> camera = new EnumMap<CameraProperty, String>(CameraProperty.class);
if (maker != null) { if (maker != null) {
@ -87,8 +87,9 @@ public class ImageMetadata {
if (model != null) { if (model != null) {
camera.put(CameraProperty.model, model); camera.put(CameraProperty.model, model);
} }
return camera; return camera;
}).filter(m -> m.size() > 0); }).filter(m -> !m.isEmpty());
} }
public enum CameraProperty { public enum CameraProperty {
@ -96,14 +97,15 @@ public class ImageMetadata {
} }
public Optional<Map<AddressComponent, String>> getLocationTaken() { public Optional<Map<AddressComponent, String>> 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<AddressComponent, String> locate(GeoLocation location) { protected Map<AddressComponent, String> locate(GeoLocation location) {
try { try {
// e.g. https://maps.googleapis.com/maps/api/geocode/json?latlng=40.7470444,-073.9411611 // e.g. https://maps.googleapis.com/maps/api/geocode/json?latlng=40.7470444,-073.9411611
Cache cache = Cache.getCache("geocode", CacheType.Monthly); Cache cache = Cache.getCache("geocode", CacheType.Persistent);
Object json = cache.json(location.getLatitude() + "," + location.getLongitude(), pos -> new URL("https://maps.googleapis.com/maps/api/geocode/json?latlng=" + pos)).get();
Object json = cache.json(location.getLatitude() + "," + location.getLongitude(), p -> new URL("https://maps.googleapis.com/maps/api/geocode/json?latlng=" + p)).get();
Map<AddressComponent, String> address = new EnumMap<AddressComponent, String>(AddressComponent.class); Map<AddressComponent, String> address = new EnumMap<AddressComponent, String>(AddressComponent.class);