| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
package com.drew.tools; |
| 23 |
|
| 24 |
import com.adobe.internal.xmp.XMPException; |
| 25 |
import com.adobe.internal.xmp.XMPIterator; |
| 26 |
import com.adobe.internal.xmp.XMPMeta; |
| 27 |
import com.adobe.internal.xmp.options.IteratorOptions; |
| 28 |
import com.adobe.internal.xmp.properties.XMPPropertyInfo; |
| 29 |
import com.drew.imaging.FileType; |
| 30 |
import com.drew.imaging.FileTypeDetector; |
| 31 |
import com.drew.imaging.ImageMetadataReader; |
| 32 |
import com.drew.lang.StringUtil; |
| 33 |
import com.drew.lang.annotations.NotNull; |
| 34 |
import com.drew.lang.annotations.Nullable; |
| 35 |
import com.drew.metadata.Directory; |
| 36 |
import com.drew.metadata.Metadata; |
| 37 |
import com.drew.metadata.Tag; |
| 38 |
import com.drew.metadata.exif.ExifIFD0Directory; |
| 39 |
import com.drew.metadata.exif.ExifSubIFDDirectory; |
| 40 |
import com.drew.metadata.exif.ExifThumbnailDirectory; |
| 41 |
import com.drew.metadata.file.FileSystemDirectory; |
| 42 |
import com.drew.metadata.xmp.XmpDirectory; |
| 43 |
|
| 44 |
import java.io.*; |
| 45 |
import java.util.*; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public class ProcessAllImagesInFolderUtility |
| 51 |
{ |
| 52 |
public static void main(String[] args) throws IOException |
| 53 |
{ |
| 54 |
Locale.setDefault(new Locale("en", "US")); |
| 55 |
System.setProperty("user.timezone", "Australia/Sydney"); |
| 56 |
|
| 57 |
List<String> directories = new ArrayList<String>(); |
| 58 |
|
| 59 |
FileHandler handler = null; |
| 60 |
PrintStream log = System.out; |
| 61 |
|
| 62 |
for (int i = 0; i < args.length; i++) { |
| 63 |
String arg = args[i]; |
| 64 |
if (arg.equalsIgnoreCase("--text")) { |
| 65 |
|
| 66 |
handler = new TextFileOutputHandler(); |
| 67 |
} else if (arg.equalsIgnoreCase("--markdown")) { |
| 68 |
|
| 69 |
handler = new MarkdownTableOutputHandler(); |
| 70 |
} else if (arg.equalsIgnoreCase("--unknown")) { |
| 71 |
|
| 72 |
handler = new UnknownTagHandler(); |
| 73 |
} else if (arg.equalsIgnoreCase("--log-file")) { |
| 74 |
if (i == args.length - 1) { |
| 75 |
printUsage(); |
| 76 |
System.exit(1); |
| 77 |
} |
| 78 |
log = new PrintStream(new FileOutputStream(args[++i], false), true); |
| 79 |
} else { |
| 80 |
|
| 81 |
directories.add(arg); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
if (directories.isEmpty()) { |
| 86 |
System.err.println("Expects one or more directories as arguments."); |
| 87 |
printUsage(); |
| 88 |
System.exit(1); |
| 89 |
} |
| 90 |
|
| 91 |
if (handler == null) { |
| 92 |
handler = new BasicFileHandler(); |
| 93 |
} |
| 94 |
|
| 95 |
long start = System.nanoTime(); |
| 96 |
|
| 97 |
for (String directory : directories) { |
| 98 |
processDirectory(new File(directory), handler, "", log); |
| 99 |
} |
| 100 |
|
| 101 |
handler.onScanCompleted(log); |
| 102 |
|
| 103 |
System.out.println(String.format("Completed in %d ms", (System.nanoTime() - start) / 1000000)); |
| 104 |
|
| 105 |
if (log != System.out) { |
| 106 |
log.close(); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
private static void printUsage() |
| 111 |
{ |
| 112 |
System.out.println("Usage:"); |
| 113 |
System.out.println(); |
| 114 |
System.out.println(" java com.drew.tools.ProcessAllImagesInFolderUtility [--text|--markdown|--unknown] [--log-file <file-name>]"); |
| 115 |
} |
| 116 |
|
| 117 |
private static void processDirectory(@NotNull File path, @NotNull FileHandler handler, @NotNull String relativePath, PrintStream log) |
| 118 |
{ |
| 119 |
handler.onStartingDirectory(path); |
| 120 |
|
| 121 |
String[] pathItems = path.list(); |
| 122 |
|
| 123 |
if (pathItems == null) { |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
Arrays.sort(pathItems); |
| 129 |
|
| 130 |
for (String pathItem : pathItems) { |
| 131 |
File file = new File(path, pathItem); |
| 132 |
|
| 133 |
if (file.isDirectory()) { |
| 134 |
processDirectory(file, handler, relativePath.length() == 0 ? pathItem : relativePath + "/" + pathItem, log); |
| 135 |
} else if (handler.shouldProcess(file)) { |
| 136 |
|
| 137 |
handler.onBeforeExtraction(file, log, relativePath); |
| 138 |
|
| 139 |
|
| 140 |
final Metadata metadata; |
| 141 |
try { |
| 142 |
metadata = ImageMetadataReader.readMetadata(file); |
| 143 |
} catch (Throwable t) { |
| 144 |
handler.onExtractionError(file, t, log); |
| 145 |
continue; |
| 146 |
} |
| 147 |
|
| 148 |
handler.onExtractionSuccess(file, metadata, relativePath, log); |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
interface FileHandler |
| 154 |
{ |
| 155 |
|
| 156 |
void onStartingDirectory(@NotNull File directoryPath); |
| 157 |
|
| 158 |
|
| 159 |
boolean shouldProcess(@NotNull File file); |
| 160 |
|
| 161 |
|
| 162 |
void onBeforeExtraction(@NotNull File file, @NotNull PrintStream log, @NotNull String relativePath); |
| 163 |
|
| 164 |
|
| 165 |
void onExtractionSuccess(@NotNull File file, @NotNull Metadata metadata, @NotNull String relativePath, @NotNull PrintStream log); |
| 166 |
|
| 167 |
|
| 168 |
void onExtractionError(@NotNull File file, @NotNull Throwable throwable, @NotNull PrintStream log); |
| 169 |
|
| 170 |
|
| 171 |
void onScanCompleted(@NotNull PrintStream log); |
| 172 |
} |
| 173 |
|
| 174 |
abstract static class FileHandlerBase implements FileHandler |
| 175 |
{ |
| 176 |
|
| 177 |
private final Set<String> _supportedExtensions = new HashSet<String>( |
| 178 |
Arrays.asList( |
| 179 |
"3fr", |
| 180 |
"3g2", |
| 181 |
"3gp", |
| 182 |
"ai", |
| 183 |
"arw", |
| 184 |
"avi", |
| 185 |
"bmp", |
| 186 |
"cam", |
| 187 |
"cr2", |
| 188 |
"cr3", |
| 189 |
"crw", |
| 190 |
"dcr", |
| 191 |
"dng", |
| 192 |
"eps", |
| 193 |
"fuzzed", |
| 194 |
"gif", |
| 195 |
"gpr", |
| 196 |
"heic", |
| 197 |
"heif", |
| 198 |
"ico", |
| 199 |
"j2c", |
| 200 |
"jp2", |
| 201 |
"jpeg", |
| 202 |
"jpf", |
| 203 |
"jpg", |
| 204 |
"jpm", |
| 205 |
"kdc", |
| 206 |
"m2ts", |
| 207 |
"m2v", |
| 208 |
"m4a", |
| 209 |
"m4v", |
| 210 |
"mj2", |
| 211 |
"mov", |
| 212 |
"mp3", |
| 213 |
"mp4", |
| 214 |
"mpg", |
| 215 |
"mts", |
| 216 |
"nef", |
| 217 |
"orf", |
| 218 |
"pbm", |
| 219 |
"pcx", |
| 220 |
"pef", |
| 221 |
"pgm", |
| 222 |
"png", |
| 223 |
"pnm", |
| 224 |
"ppm", |
| 225 |
"psd", |
| 226 |
"raf", |
| 227 |
"rw2", |
| 228 |
"rwl", |
| 229 |
"srw", |
| 230 |
"tif", |
| 231 |
"tiff", |
| 232 |
"wav", |
| 233 |
"webp", |
| 234 |
"x3f")); |
| 235 |
|
| 236 |
private int _processedFileCount = 0; |
| 237 |
private int _exceptionCount = 0; |
| 238 |
private int _errorCount = 0; |
| 239 |
private long _processedByteCount = 0; |
| 240 |
|
| 241 |
public void onStartingDirectory(@NotNull File directoryPath) |
| 242 |
{} |
| 243 |
|
| 244 |
public boolean shouldProcess(@NotNull File file) |
| 245 |
{ |
| 246 |
String extension = getExtension(file); |
| 247 |
return extension != null && _supportedExtensions.contains(extension.toLowerCase()); |
| 248 |
} |
| 249 |
|
| 250 |
public void onBeforeExtraction(@NotNull File file, @NotNull PrintStream log, @NotNull String relativePath) |
| 251 |
{ |
| 252 |
_processedFileCount++; |
| 253 |
_processedByteCount += file.length(); |
| 254 |
} |
| 255 |
|
| 256 |
public void onExtractionError(@NotNull File file, @NotNull Throwable throwable, @NotNull PrintStream log) |
| 257 |
{ |
| 258 |
_exceptionCount++; |
| 259 |
log.printf("\t[%s] %s\n", throwable.getClass().getName(), throwable.getMessage()); |
| 260 |
} |
| 261 |
|
| 262 |
public void onExtractionSuccess(@NotNull File file, @NotNull Metadata metadata, @NotNull String relativePath, @NotNull PrintStream log) |
| 263 |
{ |
| 264 |
if (metadata.hasErrors()) { |
| 265 |
log.print(file); |
| 266 |
log.print('\n'); |
| 267 |
for (Directory directory : metadata.getDirectories()) { |
| 268 |
if (!directory.hasErrors()) |
| 269 |
continue; |
| 270 |
for (String error : directory.getErrors()) { |
| 271 |
log.printf("\t[%s] %s\n", directory.getName(), error); |
| 272 |
_errorCount++; |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
public void onScanCompleted(@NotNull PrintStream log) |
| 279 |
{ |
| 280 |
if (_processedFileCount > 0) { |
| 281 |
log.print(String.format( |
| 282 |
"Processed %,d files (%,d bytes) with %,d exceptions and %,d file errors\n", |
| 283 |
_processedFileCount, _processedByteCount, _exceptionCount, _errorCount |
| 284 |
)); |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
@Nullable |
| 289 |
protected String getExtension(@NotNull File file) |
| 290 |
{ |
| 291 |
String fileName = file.getName(); |
| 292 |
int i = fileName.lastIndexOf('.'); |
| 293 |
if (i == -1) |
| 294 |
return null; |
| 295 |
if (i == fileName.length() - 1) |
| 296 |
return null; |
| 297 |
return fileName.substring(i + 1); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
static class TextFileOutputHandler extends FileHandlerBase |
| 305 |
{ |
| 306 |
|
| 307 |
private static final String NEW_LINE = "\n"; |
| 308 |
|
| 309 |
@Override |
| 310 |
public void onStartingDirectory(@NotNull File directoryPath) |
| 311 |
{ |
| 312 |
super.onStartingDirectory(directoryPath); |
| 313 |
|
| 314 |
|
| 315 |
File metadataDirectory = new File(directoryPath + "/metadata/java"); |
| 316 |
if (metadataDirectory.exists()) |
| 317 |
deleteRecursively(metadataDirectory); |
| 318 |
} |
| 319 |
|
| 320 |
private static void deleteRecursively(@NotNull File directory) |
| 321 |
{ |
| 322 |
if (!directory.isDirectory()) |
| 323 |
throw new IllegalArgumentException("Must be a directory."); |
| 324 |
|
| 325 |
if (directory.exists()) { |
| 326 |
String[] list = directory.list(); |
| 327 |
if (list != null) { |
| 328 |
for (String item : list) { |
| 329 |
File file = new File(item); |
| 330 |
if (file.isDirectory()) |
| 331 |
deleteRecursively(file); |
| 332 |
else |
| 333 |
file.delete(); |
| 334 |
} |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
directory.delete(); |
| 339 |
} |
| 340 |
|
| 341 |
@Override |
| 342 |
public void onBeforeExtraction(@NotNull File file, @NotNull PrintStream log, @NotNull String relativePath) |
| 343 |
{ |
| 344 |
super.onBeforeExtraction(file, log, relativePath); |
| 345 |
log.print(file.getAbsoluteFile()); |
| 346 |
log.print(NEW_LINE); |
| 347 |
} |
| 348 |
|
| 349 |
@Override |
| 350 |
public void onExtractionSuccess(@NotNull File file, @NotNull Metadata metadata, @NotNull String relativePath, @NotNull PrintStream log) |
| 351 |
{ |
| 352 |
super.onExtractionSuccess(file, metadata, relativePath, log); |
| 353 |
|
| 354 |
try { |
| 355 |
PrintWriter writer = null; |
| 356 |
try |
| 357 |
{ |
| 358 |
writer = openWriter(file); |
| 359 |
|
| 360 |
|
| 361 |
if (metadata.hasErrors()) { |
| 362 |
for (Directory directory : metadata.getDirectories()) { |
| 363 |
if (!directory.hasErrors()) |
| 364 |
continue; |
| 365 |
for (String error : directory.getErrors()) |
| 366 |
writer.format("[ERROR: %s] %s%s", directory.getName(), error, NEW_LINE); |
| 367 |
} |
| 368 |
writer.write(NEW_LINE); |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
for (Directory directory : metadata.getDirectories()) { |
| 373 |
String directoryName = directory.getName(); |
| 374 |
|
| 375 |
for (Tag tag : directory.getTags()) { |
| 376 |
String tagName = tag.getTagName(); |
| 377 |
String description; |
| 378 |
try { |
| 379 |
description = tag.getDescription(); |
| 380 |
} catch (Exception ex) { |
| 381 |
description = "ERROR: " + ex.getMessage(); |
| 382 |
} |
| 383 |
if (description == null) |
| 384 |
description = ""; |
| 385 |
|
| 386 |
if (directory instanceof FileSystemDirectory && tag.getTagType() == FileSystemDirectory.TAG_FILE_MODIFIED_DATE) |
| 387 |
description = "<omitted for regression testing as checkout dependent>"; |
| 388 |
writer.format("[%s - %s] %s = %s%s", directoryName, tag.getTagTypeHex(), tagName, description, NEW_LINE); |
| 389 |
} |
| 390 |
if (directory.getTagCount() != 0) |
| 391 |
writer.write(NEW_LINE); |
| 392 |
|
| 393 |
if (directory instanceof XmpDirectory) { |
| 394 |
boolean wrote = false; |
| 395 |
XmpDirectory xmpDirectory = (XmpDirectory)directory; |
| 396 |
XMPMeta xmpMeta = xmpDirectory.getXMPMeta(); |
| 397 |
try { |
| 398 |
IteratorOptions options = new IteratorOptions().setJustLeafnodes(true); |
| 399 |
XMPIterator iterator = xmpMeta.iterator(options); |
| 400 |
while (iterator.hasNext()) { |
| 401 |
XMPPropertyInfo prop = (XMPPropertyInfo)iterator.next(); |
| 402 |
String ns = prop.getNamespace(); |
| 403 |
String path = prop.getPath(); |
| 404 |
String value = prop.getValue(); |
| 405 |
|
| 406 |
if (path == null) |
| 407 |
continue; |
| 408 |
if (ns == null) |
| 409 |
ns = ""; |
| 410 |
|
| 411 |
final int MAX_XMP_VALUE_LENGTH = 512; |
| 412 |
if (value == null) |
| 413 |
value = ""; |
| 414 |
else if (value.length() > MAX_XMP_VALUE_LENGTH) |
| 415 |
value = String.format("%s <truncated from %d characters>", value.substring(0, MAX_XMP_VALUE_LENGTH), value.length()); |
| 416 |
|
| 417 |
writer.format("[XMPMeta - %s] %s = %s%s", ns, path, value, NEW_LINE); |
| 418 |
wrote = true; |
| 419 |
} |
| 420 |
} catch (XMPException e) { |
| 421 |
e.printStackTrace(); |
| 422 |
} |
| 423 |
if (wrote) |
| 424 |
writer.write(NEW_LINE); |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
writeHierarchyLevel(metadata, writer, null, 0); |
| 430 |
|
| 431 |
writer.write(NEW_LINE); |
| 432 |
} finally { |
| 433 |
closeWriter(writer); |
| 434 |
} |
| 435 |
} catch (IOException e) { |
| 436 |
e.printStackTrace(); |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
private static void writeHierarchyLevel(@NotNull Metadata metadata, @NotNull PrintWriter writer, @Nullable Directory parent, int level) |
| 441 |
{ |
| 442 |
final int indent = 4; |
| 443 |
|
| 444 |
for (Directory child : metadata.getDirectories()) { |
| 445 |
if (parent == null) { |
| 446 |
if (child.getParent() != null) |
| 447 |
continue; |
| 448 |
} else if (!parent.equals(child.getParent())) { |
| 449 |
continue; |
| 450 |
} |
| 451 |
|
| 452 |
for (int i = 0; i < level*indent; i++) { |
| 453 |
writer.write(' '); |
| 454 |
} |
| 455 |
writer.write("- "); |
| 456 |
writer.write(child.getName()); |
| 457 |
writer.write(NEW_LINE); |
| 458 |
writeHierarchyLevel(metadata, writer, child, level + 1); |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
@Override |
| 463 |
public void onExtractionError(@NotNull File file, @NotNull Throwable throwable, @NotNull PrintStream log) |
| 464 |
{ |
| 465 |
super.onExtractionError(file, throwable, log); |
| 466 |
|
| 467 |
try { |
| 468 |
PrintWriter writer = null; |
| 469 |
try { |
| 470 |
writer = openWriter(file); |
| 471 |
writer.write("EXCEPTION: " + throwable.getMessage() + NEW_LINE); |
| 472 |
writer.write(NEW_LINE); |
| 473 |
} finally { |
| 474 |
closeWriter(writer); |
| 475 |
} |
| 476 |
} catch (IOException e) { |
| 477 |
log.printf("IO exception writing metadata file: %s%s", e.getMessage(), NEW_LINE); |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
@NotNull |
| 482 |
private static PrintWriter openWriter(@NotNull File file) throws IOException |
| 483 |
{ |
| 484 |
|
| 485 |
File metadataDir = new File(String.format("%s/metadata", file.getParent())); |
| 486 |
if (!metadataDir.exists()) |
| 487 |
metadataDir.mkdir(); |
| 488 |
|
| 489 |
File javaDir = new File(String.format("%s/metadata/java", file.getParent())); |
| 490 |
if (!javaDir.exists()) |
| 491 |
javaDir.mkdir(); |
| 492 |
|
| 493 |
String outputPath = String.format("%s/metadata/java/%s.txt", file.getParent(), file.getName()); |
| 494 |
Writer writer = new OutputStreamWriter( |
| 495 |
new FileOutputStream(outputPath), |
| 496 |
"UTF-8" |
| 497 |
); |
| 498 |
writer.write("FILE: " + file.getName() + NEW_LINE); |
| 499 |
|
| 500 |
|
| 501 |
BufferedInputStream stream = null; |
| 502 |
try { |
| 503 |
stream = new BufferedInputStream(new FileInputStream(file)); |
| 504 |
FileType fileType = FileTypeDetector.detectFileType(stream); |
| 505 |
writer.write(String.format("TYPE: %s" + NEW_LINE, fileType.toString().toUpperCase())); |
| 506 |
writer.write(NEW_LINE); |
| 507 |
} finally { |
| 508 |
if (stream != null) { |
| 509 |
stream.close(); |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
return new PrintWriter(writer); |
| 514 |
} |
| 515 |
|
| 516 |
private static void closeWriter(@Nullable Writer writer) throws IOException |
| 517 |
{ |
| 518 |
if (writer != null) { |
| 519 |
writer.write("Generated using metadata-extractor" + NEW_LINE); |
| 520 |
writer.write("https://drewnoakes.com/code/exif/" + NEW_LINE); |
| 521 |
writer.flush(); |
| 522 |
writer.close(); |
| 523 |
} |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
static class MarkdownTableOutputHandler extends FileHandlerBase |
| 531 |
{ |
| 532 |
private final Map<String, String> _extensionEquivalence = new HashMap<String, String>(); |
| 533 |
private final Map<String, List<Row>> _rowListByExtension = new HashMap<String, List<Row>>(); |
| 534 |
|
| 535 |
static class Row |
| 536 |
{ |
| 537 |
final File file; |
| 538 |
final Metadata metadata; |
| 539 |
@NotNull final String relativePath; |
| 540 |
@Nullable private String manufacturer; |
| 541 |
@Nullable private String model; |
| 542 |
@Nullable private String exifVersion; |
| 543 |
@Nullable private String thumbnail; |
| 544 |
@Nullable private String makernote; |
| 545 |
|
| 546 |
Row(@NotNull File file, @NotNull Metadata metadata, @NotNull String relativePath) |
| 547 |
{ |
| 548 |
this.file = file; |
| 549 |
this.metadata = metadata; |
| 550 |
this.relativePath = relativePath; |
| 551 |
|
| 552 |
ExifIFD0Directory ifd0Dir = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class); |
| 553 |
ExifSubIFDDirectory subIfdDir = metadata.getFirstDirectoryOfType(ExifSubIFDDirectory.class); |
| 554 |
ExifThumbnailDirectory thumbDir = metadata.getFirstDirectoryOfType(ExifThumbnailDirectory.class); |
| 555 |
if (ifd0Dir != null) { |
| 556 |
manufacturer = ifd0Dir.getDescription(ExifIFD0Directory.TAG_MAKE); |
| 557 |
model = ifd0Dir.getDescription(ExifIFD0Directory.TAG_MODEL); |
| 558 |
} |
| 559 |
boolean hasMakernoteData = false; |
| 560 |
if (subIfdDir != null) { |
| 561 |
exifVersion = subIfdDir.getDescription(ExifSubIFDDirectory.TAG_EXIF_VERSION); |
| 562 |
hasMakernoteData = subIfdDir.containsTag(ExifSubIFDDirectory.TAG_MAKERNOTE); |
| 563 |
} |
| 564 |
if (thumbDir != null) { |
| 565 |
Integer width = thumbDir.getInteger(ExifThumbnailDirectory.TAG_IMAGE_WIDTH); |
| 566 |
Integer height = thumbDir.getInteger(ExifThumbnailDirectory.TAG_IMAGE_HEIGHT); |
| 567 |
thumbnail = width != null && height != null |
| 568 |
? String.format("Yes (%s x %s)", width, height) |
| 569 |
: "Yes"; |
| 570 |
} |
| 571 |
for (Directory directory : metadata.getDirectories()) { |
| 572 |
if (directory.getClass().getName().contains("Makernote")) { |
| 573 |
makernote = directory.getName().replace("Makernote", "").trim(); |
| 574 |
break; |
| 575 |
} |
| 576 |
} |
| 577 |
if (makernote == null) { |
| 578 |
makernote = hasMakernoteData ? "(Unknown)" : "N/A"; |
| 579 |
} |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
public MarkdownTableOutputHandler() |
| 584 |
{ |
| 585 |
_extensionEquivalence.put("jpeg", "jpg"); |
| 586 |
} |
| 587 |
|
| 588 |
@Override |
| 589 |
public void onExtractionSuccess(@NotNull File file, @NotNull Metadata metadata, @NotNull String relativePath, @NotNull PrintStream log) |
| 590 |
{ |
| 591 |
super.onExtractionSuccess(file, metadata, relativePath, log); |
| 592 |
|
| 593 |
String extension = getExtension(file); |
| 594 |
|
| 595 |
if (extension == null) { |
| 596 |
return; |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
extension = extension.toLowerCase(); |
| 601 |
if (_extensionEquivalence.containsKey(extension)) |
| 602 |
extension = _extensionEquivalence.get(extension); |
| 603 |
|
| 604 |
List<Row> list = _rowListByExtension.get(extension); |
| 605 |
if (list == null) { |
| 606 |
list = new ArrayList<Row>(); |
| 607 |
_rowListByExtension.put(extension, list); |
| 608 |
} |
| 609 |
list.add(new Row(file, metadata, relativePath)); |
| 610 |
} |
| 611 |
|
| 612 |
@Override |
| 613 |
public void onScanCompleted(@NotNull PrintStream log) |
| 614 |
{ |
| 615 |
super.onScanCompleted(log); |
| 616 |
|
| 617 |
OutputStream outputStream = null; |
| 618 |
PrintStream stream = null; |
| 619 |
try { |
| 620 |
outputStream = new FileOutputStream("../wiki/ImageDatabaseSummary.md", false); |
| 621 |
stream = new PrintStream(outputStream, false); |
| 622 |
writeOutput(stream); |
| 623 |
stream.flush(); |
| 624 |
} catch (IOException e) { |
| 625 |
e.printStackTrace(); |
| 626 |
} finally { |
| 627 |
if (stream != null) |
| 628 |
stream.close(); |
| 629 |
if (outputStream != null) |
| 630 |
try { |
| 631 |
outputStream.close(); |
| 632 |
} catch (IOException e) { |
| 633 |
e.printStackTrace(); |
| 634 |
} |
| 635 |
} |
| 636 |
} |
| 637 |
|
| 638 |
private void writeOutput(@NotNull PrintStream stream) throws IOException |
| 639 |
{ |
| 640 |
Writer writer = new OutputStreamWriter(stream); |
| 641 |
writer.write("# Image Database Summary\n\n"); |
| 642 |
|
| 643 |
for (Map.Entry<String, List<Row>> entry : _rowListByExtension.entrySet()) { |
| 644 |
String extension = entry.getKey(); |
| 645 |
writer.write("## " + extension.toUpperCase() + " Files\n\n"); |
| 646 |
|
| 647 |
writer.write("File|Manufacturer|Model|Dir Count|Exif?|Makernote|Thumbnail|All Data\n"); |
| 648 |
writer.write("----|------------|-----|---------|-----|---------|---------|--------\n"); |
| 649 |
|
| 650 |
List<Row> rows = entry.getValue(); |
| 651 |
|
| 652 |
|
| 653 |
Collections.sort(rows, new Comparator<Row>() { |
| 654 |
public int compare(Row o1, Row o2) |
| 655 |
{ |
| 656 |
int c1 = StringUtil.compare(o1.manufacturer, o2.manufacturer); |
| 657 |
return c1 != 0 ? c1 : StringUtil.compare(o1.model, o2.model); |
| 658 |
} |
| 659 |
}); |
| 660 |
|
| 661 |
for (Row row : rows) { |
| 662 |
writer.write(String.format("[%s](https://raw.githubusercontent.com/drewnoakes/metadata-extractor-images/master/%s/%s)|%s|%s|%d|%s|%s|%s|[metadata](https://raw.githubusercontent.com/drewnoakes/metadata-extractor-images/master/%s/metadata/%s.txt)\n", |
| 663 |
row.file.getName(), |
| 664 |
row.relativePath, |
| 665 |
StringUtil.urlEncode(row.file.getName()), |
| 666 |
row.manufacturer == null ? "" : row.manufacturer, |
| 667 |
row.model == null ? "" : row.model, |
| 668 |
row.metadata.getDirectoryCount(), |
| 669 |
row.exifVersion == null ? "" : row.exifVersion, |
| 670 |
row.makernote == null ? "" : row.makernote, |
| 671 |
row.thumbnail == null ? "" : row.thumbnail, |
| 672 |
row.relativePath, |
| 673 |
StringUtil.urlEncode(row.file.getName()).toLowerCase() |
| 674 |
)); |
| 675 |
} |
| 676 |
|
| 677 |
writer.write('\n'); |
| 678 |
} |
| 679 |
writer.flush(); |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
static class UnknownTagHandler extends FileHandlerBase |
| 687 |
{ |
| 688 |
private HashMap<String, HashMap<Integer, Integer>> _occurrenceCountByTagByDirectory = new HashMap<String, HashMap<Integer, Integer>>(); |
| 689 |
|
| 690 |
@Override |
| 691 |
public void onExtractionSuccess(@NotNull File file, @NotNull Metadata metadata, @NotNull String relativePath, @NotNull PrintStream log) |
| 692 |
{ |
| 693 |
super.onExtractionSuccess(file, metadata, relativePath, log); |
| 694 |
|
| 695 |
for (Directory directory : metadata.getDirectories()) { |
| 696 |
for (Tag tag : directory.getTags()) { |
| 697 |
|
| 698 |
|
| 699 |
if (tag.hasTagName()) |
| 700 |
continue; |
| 701 |
|
| 702 |
HashMap<Integer, Integer> occurrenceCountByTag = _occurrenceCountByTagByDirectory.get(directory.getName()); |
| 703 |
if (occurrenceCountByTag == null) { |
| 704 |
occurrenceCountByTag = new HashMap<Integer, Integer>(); |
| 705 |
_occurrenceCountByTagByDirectory.put(directory.getName(), occurrenceCountByTag); |
| 706 |
} |
| 707 |
|
| 708 |
Integer count = occurrenceCountByTag.get(tag.getTagType()); |
| 709 |
if (count == null) { |
| 710 |
count = 0; |
| 711 |
occurrenceCountByTag.put(tag.getTagType(), 0); |
| 712 |
} |
| 713 |
|
| 714 |
occurrenceCountByTag.put(tag.getTagType(), count + 1); |
| 715 |
} |
| 716 |
} |
| 717 |
} |
| 718 |
|
| 719 |
@Override |
| 720 |
public void onScanCompleted(@NotNull PrintStream log) |
| 721 |
{ |
| 722 |
super.onScanCompleted(log); |
| 723 |
|
| 724 |
for (Map.Entry<String, HashMap<Integer, Integer>> pair1 : _occurrenceCountByTagByDirectory.entrySet()) { |
| 725 |
String directoryName = pair1.getKey(); |
| 726 |
List<Map.Entry<Integer, Integer>> counts = new ArrayList<Map.Entry<Integer, Integer>>(pair1.getValue().entrySet()); |
| 727 |
Collections.sort(counts, new Comparator<Map.Entry<Integer, Integer>>() |
| 728 |
{ |
| 729 |
public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) |
| 730 |
{ |
| 731 |
return o2.getValue().compareTo(o1.getValue()); |
| 732 |
} |
| 733 |
}); |
| 734 |
for (Map.Entry<Integer, Integer> pair2 : counts) { |
| 735 |
Integer tagType = pair2.getKey(); |
| 736 |
Integer count = pair2.getValue(); |
| 737 |
log.format("%s, 0x%04X, %d\n", directoryName, tagType, count); |
| 738 |
} |
| 739 |
} |
| 740 |
} |
| 741 |
} |
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
static class BasicFileHandler extends FileHandlerBase |
| 748 |
{ |
| 749 |
@Override |
| 750 |
public void onExtractionSuccess(@NotNull File file, @NotNull Metadata metadata, @NotNull String relativePath, @NotNull PrintStream log) |
| 751 |
{ |
| 752 |
super.onExtractionSuccess(file, metadata, relativePath, log); |
| 753 |
|
| 754 |
|
| 755 |
for (Directory directory : metadata.getDirectories()) { |
| 756 |
directory.getName(); |
| 757 |
for (Tag tag : directory.getTags()) { |
| 758 |
tag.getTagName(); |
| 759 |
tag.getDescription(); |
| 760 |
} |
| 761 |
} |
| 762 |
} |
| 763 |
} |
| 764 |
} |