Refactor ImageMetadata API

This commit is contained in:
Reinhard Pointner 2017-02-26 22:55:44 +08:00
parent c16dbc3741
commit 40bab4a1fb
3 changed files with 15 additions and 10 deletions

View File

@ -844,7 +844,7 @@ public class MediaBindingBean {
@Define("exif")
public AssociativeScriptObject getImageMetadata() throws Exception {
return new AssociativeScriptObject(new ImageMetadata(getMediaFile()).snapshot(t -> t.getTagName()));
return new AssociativeScriptObject(new ImageMetadata(getMediaFile()).snapshot());
}
@Define("camera")

View File

@ -15,6 +15,7 @@ import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
@ -35,22 +36,26 @@ public class ImageMetadata {
private final Metadata metadata;
public ImageMetadata(File file) throws ImageProcessingException, IOException {
if (SUPPORTED_FILE_TYPES.accept(file)) {
if (!SUPPORTED_FILE_TYPES.accept(file)) {
throw new IllegalArgumentException("Image type not supported: " + file);
}
metadata = ImageMetadataReader.readMetadata(file);
}
protected boolean accept(Directory directory) {
return !directory.getName().matches("JPEG|JFIF|Interoperability|Huffman|File");
public Map<String, String> snapshot() {
return snapshot(Tag::getTagName);
}
public Map<String, String> snapshot(Function<Tag, String> key) {
return snapshot(key, d -> !d.getName().matches("JPEG|JFIF|Interoperability|Huffman|File"));
}
public Map<String, String> snapshot(Function<Tag, String> key, Predicate<Directory> accept) {
Map<String, String> values = new LinkedHashMap<String, String>();
for (Directory directory : metadata.getDirectories()) {
if (accept(directory)) {
if (accept.test(directory)) {
for (Tag tag : directory.getTags()) {
String v = tag.getDescription();
if (v != null && v.length() > 0) {

View File

@ -1,10 +1,9 @@
package net.filebot.mediainfo;
import static java.nio.charset.StandardCharsets.*;
import static java.util.regex.Pattern.*;
import static java.util.stream.Collectors.*;
import static net.filebot.Logging.*;
import static net.filebot.util.StringUtilities.*;
import static net.filebot.util.RegularExpressions.*;
import java.io.Closeable;
import java.io.File;
@ -158,9 +157,10 @@ public class MediaInfo implements Closeable {
if (streamKind == StreamKind.Image && streamNumber == 0) {
String path = get(StreamKind.General, 0, "CompleteName");
try {
streamInfo.putAll(new ImageMetadata(new File(path)).snapshot(t -> {
return Stream.of(t.getDirectoryName(), t.getTagName()).flatMap(s -> tokenize(s, compile("\\W+"))).distinct().collect(joining("_"));
}));
Map<String, String> values = new ImageMetadata(new File(path)).snapshot(t -> {
return Stream.of(t.getDirectoryName(), t.getTagName()).flatMap(NON_WORD::splitAsStream).distinct().collect(joining("_"));
});
streamInfo.putAll(values);
} catch (Throwable e) {
debug.warning(format("%s: %s", e, path));
}