diff --git a/src/org/forkalsrud/album/exif/EntryDao.java b/src/org/forkalsrud/album/exif/EntryDao.java index 93326dc..c69e14a 100644 --- a/src/org/forkalsrud/album/exif/EntryDao.java +++ b/src/org/forkalsrud/album/exif/EntryDao.java @@ -30,6 +30,7 @@ import com.drew.metadata.Directory; import com.drew.metadata.Metadata; import com.drew.metadata.MetadataException; import com.drew.metadata.exif.ExifDirectory; +import com.drew.metadata.jpeg.JpegDirectory; /** @@ -122,12 +123,15 @@ public class EntryDao { for (File f : files) { String name = f.getName(); String base = "file." + name + "."; + boolean hasDate = false; + boolean hasOrientation = false; Metadata metadata = JpegMetadataReader.readMetadata(f); Directory exifDirectory = metadata.getDirectory(ExifDirectory.class); if (exifDirectory.containsTag(ExifDirectory.TAG_ORIENTATION)) { int orientation = exifDirectory.getInt(ExifDirectory.TAG_ORIENTATION); cachedProps.setProperty(base + "orientation", nf.format(orientation)); + hasOrientation = true; } if (exifDirectory.containsTag(ExifDirectory.TAG_EXIF_IMAGE_WIDTH) && exifDirectory.containsTag(ExifDirectory.TAG_EXIF_IMAGE_HEIGHT)) { @@ -138,11 +142,25 @@ public class EntryDao { if (exifDirectory.containsTag(ExifDirectory.TAG_DATETIME_ORIGINAL)) { Date captureDate = exifDirectory.getDate(ExifDirectory.TAG_DATETIME_ORIGINAL); cachedProps.setProperty(base + "captureDate", sdf.format(captureDate)); + hasDate = true; } if (exifDirectory.containsTag(ExifDirectory.TAG_USER_COMMENT)) { String comment = exifDirectory.getString(ExifDirectory.TAG_USER_COMMENT); cachedProps.setProperty(base + "comment", comment); } + Directory jpegDirectory = metadata.getDirectory(JpegDirectory.class); + if (jpegDirectory.containsTag(JpegDirectory.TAG_JPEG_IMAGE_WIDTH) && + jpegDirectory.containsTag(JpegDirectory.TAG_JPEG_IMAGE_HEIGHT)) { + int width = jpegDirectory.getInt(JpegDirectory.TAG_JPEG_IMAGE_WIDTH); + int height = jpegDirectory.getInt(JpegDirectory.TAG_JPEG_IMAGE_HEIGHT); + cachedProps.setProperty(base + "dimensions", new Dimension(width, height).toString()); + } + if (!hasDate) { + cachedProps.setProperty(base + "captureDate", sdf.format(new Date(f.lastModified()))); + } + if (!hasOrientation) { + cachedProps.setProperty(base + "orientation", "1"); + } } File dst = new File(directory, CACHE_FILE); if (directory.canWrite()) { diff --git a/src/org/forkalsrud/album/web/AlbumServlet.java b/src/org/forkalsrud/album/web/AlbumServlet.java index a9bcdb5..618eb9e 100644 --- a/src/org/forkalsrud/album/web/AlbumServlet.java +++ b/src/org/forkalsrud/album/web/AlbumServlet.java @@ -3,19 +3,23 @@ package org.forkalsrud.album.web; import java.awt.Graphics2D; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; -import java.io.*; +import java.io.File; +import java.io.IOException; import java.text.ParseException; -import java.util.*; +import java.util.Iterator; +import java.util.List; import javax.imageio.ImageIO; import javax.imageio.ImageReadParam; import javax.imageio.ImageReader; -import javax.imageio.ImageWriteParam; import javax.imageio.ImageWriter; import javax.imageio.stream.ImageInputStream; import javax.imageio.stream.ImageOutputStream; -import javax.servlet.*; -import javax.servlet.http.*; +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import org.forkalsrud.album.exif.Dimension; import org.forkalsrud.album.exif.Entry; @@ -48,8 +52,15 @@ public class AlbumServlet System.out.println("requestUri: " + req.getRequestURI()); System.out.println("servletPath: " + req.getServletPath()); */ - String path = req.getPathTranslated(); +/* useless + Set paths = getServletContext().getResourcePaths(req.getPathTranslated()); + for (Object path2 : paths) { + System.out.println(" " + String.valueOf(path2)); + } + System.out.println(); +*/ + String path = req.getPathTranslated(); // System.out.println("file: " + path); File file = new File(path); @@ -79,7 +90,7 @@ public class AlbumServlet rd.forward(req, res); } catch (Exception e) { - throw new ServletException("sadness", e); + throw new RuntimeException("sadness", e); } } else if (req.getParameter("size") != null) { @@ -108,9 +119,6 @@ public class AlbumServlet } Dimension orig = entry.getSize(); Dimension outd = orig.scaled(size); - if (entry.getOrientation() == 6) { - outd = outd.flip(); - } Iterator readers = ImageIO.getImageReadersByFormatName("jpg"); ImageReader reader = (ImageReader)readers.next(); @@ -120,7 +128,9 @@ public class AlbumServlet if (true /* subsampling */) { double subSampleScale = (double)outd.getWidth() / orig.getWidth(); int subSampling = (int)Math.floor(0.25d/subSampleScale); - param.setSourceSubsampling(subSampling, subSampling, 0, 0); + if (subSampling > 1) { + param.setSourceSubsampling(subSampling, subSampling, 0, 0); + } } BufferedImage img = reader.read(0, param);