Exif reader for directories
This commit is contained in:
parent
a91ac55163
commit
642399cd0b
2 changed files with 76 additions and 2 deletions
72
src/album/exif/DirectoryTest.java
Normal file
72
src/album/exif/DirectoryTest.java
Normal file
|
|
@ -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");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -51,8 +51,10 @@ public class ExifDataTest {
|
||||||
assertEquals("Top, left side (Horizontal / normal)", readOrientation2("photos/IMG_0129.JPG"));
|
assertEquals("Top, left side (Horizontal / normal)", readOrientation2("photos/IMG_0129.JPG"));
|
||||||
assertEquals("Right side, top (Rotate 90 CW)", readOrientation2("photos/IMG_0139.JPG"));
|
assertEquals("Right side, top (Rotate 90 CW)", readOrientation2("photos/IMG_0139.JPG"));
|
||||||
|
|
||||||
framePicture("photos/IMG_0129.JPG");
|
framePicture("photos/l1000729.jpg");
|
||||||
framePicture("photos/IMG_0139.JPG");
|
framePicture("photos/l1000802.jpg");
|
||||||
|
// framePicture("photos/IMG_0129.JPG");
|
||||||
|
// framePicture("photos/IMG_0139.JPG");
|
||||||
Thread.sleep(50000);
|
Thread.sleep(50000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue