From 642399cd0b4b31dcfe0cfefd537aa6af3e41936e Mon Sep 17 00:00:00 2001 From: knut Date: Wed, 12 Mar 2008 09:01:47 +0000 Subject: [PATCH] Exif reader for directories --- src/album/exif/DirectoryTest.java | 72 +++++++++++++++++++++++++++++++ src/album/exif/ExifDataTest.java | 6 ++- 2 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 src/album/exif/DirectoryTest.java diff --git a/src/album/exif/DirectoryTest.java b/src/album/exif/DirectoryTest.java new file mode 100644 index 0000000..6d43fcd --- /dev/null +++ b/src/album/exif/DirectoryTest.java @@ -0,0 +1,72 @@ +package album.exif; + +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.FileFilter; +import java.text.DecimalFormat; +import java.text.NumberFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Properties; + +import org.junit.Test; + +import com.drew.imaging.jpeg.JpegMetadataReader; +import com.drew.metadata.Directory; +import com.drew.metadata.Metadata; +import com.drew.metadata.exif.ExifDirectory; + + + +public class DirectoryTest { + + + + + @Test + public void testDirectoryReader() throws Exception { + + File myDir = new File("photos"); + assertTrue(myDir.exists()); + assertTrue(myDir.isDirectory()); + File[] files = myDir.listFiles(new FileFilter() { + + public boolean accept(File file) { + + return !file.isHidden() && !file.isDirectory(); + } + + }); + SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HHmmss"); + NumberFormat nf = new DecimalFormat("0"); + Properties imgProps = new Properties(); + for (File f : files) { + String name = f.getName(); + String base = "file." + name + "."; + + Metadata metadata = JpegMetadataReader.readMetadata(f); + Directory exifDirectory = metadata.getDirectory(ExifDirectory.class); + if (exifDirectory.containsTag(ExifDirectory.TAG_ORIENTATION)) { + int orientation = exifDirectory.getInt(ExifDirectory.TAG_ORIENTATION); + imgProps.setProperty(base + "orientation", nf.format(orientation)); + } + if (exifDirectory.containsTag(ExifDirectory.TAG_EXIF_IMAGE_WIDTH) && + exifDirectory.containsTag(ExifDirectory.TAG_EXIF_IMAGE_HEIGHT)) { + int width = exifDirectory.getInt(ExifDirectory.TAG_EXIF_IMAGE_WIDTH); + int height = exifDirectory.getInt(ExifDirectory.TAG_EXIF_IMAGE_HEIGHT); + imgProps.setProperty(base + "dimensions", nf.format(width) + "x" + nf.format(height)); + } + if (exifDirectory.containsTag(ExifDirectory.TAG_DATETIME_ORIGINAL)) { + Date captureDate = exifDirectory.getDate(ExifDirectory.TAG_DATETIME_ORIGINAL); + imgProps.setProperty(base + "captureDate", sdf.format(captureDate)); + } + if (exifDirectory.containsTag(ExifDirectory.TAG_USER_COMMENT)) { + String comment = exifDirectory.getString(ExifDirectory.TAG_USER_COMMENT); + imgProps.setProperty(base + "comment", comment); + } + } + imgProps.store(System.out, "Extra Comments"); + } + +} diff --git a/src/album/exif/ExifDataTest.java b/src/album/exif/ExifDataTest.java index 9a196c1..cfe67ff 100644 --- a/src/album/exif/ExifDataTest.java +++ b/src/album/exif/ExifDataTest.java @@ -51,8 +51,10 @@ public class ExifDataTest { assertEquals("Top, left side (Horizontal / normal)", readOrientation2("photos/IMG_0129.JPG")); assertEquals("Right side, top (Rotate 90 CW)", readOrientation2("photos/IMG_0139.JPG")); - framePicture("photos/IMG_0129.JPG"); - framePicture("photos/IMG_0139.JPG"); + framePicture("photos/l1000729.jpg"); + framePicture("photos/l1000802.jpg"); +// framePicture("photos/IMG_0129.JPG"); +// framePicture("photos/IMG_0139.JPG"); Thread.sleep(50000); }