Added JPEG information to image prperty parsing
This commit is contained in:
parent
344cafa908
commit
03f58b7b5e
2 changed files with 39 additions and 11 deletions
|
|
@ -30,6 +30,7 @@ import com.drew.metadata.Directory;
|
||||||
import com.drew.metadata.Metadata;
|
import com.drew.metadata.Metadata;
|
||||||
import com.drew.metadata.MetadataException;
|
import com.drew.metadata.MetadataException;
|
||||||
import com.drew.metadata.exif.ExifDirectory;
|
import com.drew.metadata.exif.ExifDirectory;
|
||||||
|
import com.drew.metadata.jpeg.JpegDirectory;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -122,12 +123,15 @@ public class EntryDao {
|
||||||
for (File f : files) {
|
for (File f : files) {
|
||||||
String name = f.getName();
|
String name = f.getName();
|
||||||
String base = "file." + name + ".";
|
String base = "file." + name + ".";
|
||||||
|
boolean hasDate = false;
|
||||||
|
boolean hasOrientation = false;
|
||||||
|
|
||||||
Metadata metadata = JpegMetadataReader.readMetadata(f);
|
Metadata metadata = JpegMetadataReader.readMetadata(f);
|
||||||
Directory exifDirectory = metadata.getDirectory(ExifDirectory.class);
|
Directory exifDirectory = metadata.getDirectory(ExifDirectory.class);
|
||||||
if (exifDirectory.containsTag(ExifDirectory.TAG_ORIENTATION)) {
|
if (exifDirectory.containsTag(ExifDirectory.TAG_ORIENTATION)) {
|
||||||
int orientation = exifDirectory.getInt(ExifDirectory.TAG_ORIENTATION);
|
int orientation = exifDirectory.getInt(ExifDirectory.TAG_ORIENTATION);
|
||||||
cachedProps.setProperty(base + "orientation", nf.format(orientation));
|
cachedProps.setProperty(base + "orientation", nf.format(orientation));
|
||||||
|
hasOrientation = true;
|
||||||
}
|
}
|
||||||
if (exifDirectory.containsTag(ExifDirectory.TAG_EXIF_IMAGE_WIDTH) &&
|
if (exifDirectory.containsTag(ExifDirectory.TAG_EXIF_IMAGE_WIDTH) &&
|
||||||
exifDirectory.containsTag(ExifDirectory.TAG_EXIF_IMAGE_HEIGHT)) {
|
exifDirectory.containsTag(ExifDirectory.TAG_EXIF_IMAGE_HEIGHT)) {
|
||||||
|
|
@ -138,11 +142,25 @@ public class EntryDao {
|
||||||
if (exifDirectory.containsTag(ExifDirectory.TAG_DATETIME_ORIGINAL)) {
|
if (exifDirectory.containsTag(ExifDirectory.TAG_DATETIME_ORIGINAL)) {
|
||||||
Date captureDate = exifDirectory.getDate(ExifDirectory.TAG_DATETIME_ORIGINAL);
|
Date captureDate = exifDirectory.getDate(ExifDirectory.TAG_DATETIME_ORIGINAL);
|
||||||
cachedProps.setProperty(base + "captureDate", sdf.format(captureDate));
|
cachedProps.setProperty(base + "captureDate", sdf.format(captureDate));
|
||||||
|
hasDate = true;
|
||||||
}
|
}
|
||||||
if (exifDirectory.containsTag(ExifDirectory.TAG_USER_COMMENT)) {
|
if (exifDirectory.containsTag(ExifDirectory.TAG_USER_COMMENT)) {
|
||||||
String comment = exifDirectory.getString(ExifDirectory.TAG_USER_COMMENT);
|
String comment = exifDirectory.getString(ExifDirectory.TAG_USER_COMMENT);
|
||||||
cachedProps.setProperty(base + "comment", 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);
|
File dst = new File(directory, CACHE_FILE);
|
||||||
if (directory.canWrite()) {
|
if (directory.canWrite()) {
|
||||||
|
|
|
||||||
|
|
@ -3,19 +3,23 @@ package org.forkalsrud.album.web;
|
||||||
import java.awt.Graphics2D;
|
import java.awt.Graphics2D;
|
||||||
import java.awt.geom.AffineTransform;
|
import java.awt.geom.AffineTransform;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.*;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.util.*;
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
import javax.imageio.ImageReadParam;
|
import javax.imageio.ImageReadParam;
|
||||||
import javax.imageio.ImageReader;
|
import javax.imageio.ImageReader;
|
||||||
import javax.imageio.ImageWriteParam;
|
|
||||||
import javax.imageio.ImageWriter;
|
import javax.imageio.ImageWriter;
|
||||||
import javax.imageio.stream.ImageInputStream;
|
import javax.imageio.stream.ImageInputStream;
|
||||||
import javax.imageio.stream.ImageOutputStream;
|
import javax.imageio.stream.ImageOutputStream;
|
||||||
import javax.servlet.*;
|
import javax.servlet.RequestDispatcher;
|
||||||
import javax.servlet.http.*;
|
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.Dimension;
|
||||||
import org.forkalsrud.album.exif.Entry;
|
import org.forkalsrud.album.exif.Entry;
|
||||||
|
|
@ -48,8 +52,15 @@ public class AlbumServlet
|
||||||
System.out.println("requestUri: " + req.getRequestURI());
|
System.out.println("requestUri: " + req.getRequestURI());
|
||||||
System.out.println("servletPath: " + req.getServletPath());
|
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);
|
// System.out.println("file: " + path);
|
||||||
|
|
||||||
File file = new File(path);
|
File file = new File(path);
|
||||||
|
|
@ -79,7 +90,7 @@ public class AlbumServlet
|
||||||
rd.forward(req, res);
|
rd.forward(req, res);
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new ServletException("sadness", e);
|
throw new RuntimeException("sadness", e);
|
||||||
}
|
}
|
||||||
} else if (req.getParameter("size") != null) {
|
} else if (req.getParameter("size") != null) {
|
||||||
|
|
||||||
|
|
@ -108,9 +119,6 @@ public class AlbumServlet
|
||||||
}
|
}
|
||||||
Dimension orig = entry.getSize();
|
Dimension orig = entry.getSize();
|
||||||
Dimension outd = orig.scaled(size);
|
Dimension outd = orig.scaled(size);
|
||||||
if (entry.getOrientation() == 6) {
|
|
||||||
outd = outd.flip();
|
|
||||||
}
|
|
||||||
|
|
||||||
Iterator readers = ImageIO.getImageReadersByFormatName("jpg");
|
Iterator readers = ImageIO.getImageReadersByFormatName("jpg");
|
||||||
ImageReader reader = (ImageReader)readers.next();
|
ImageReader reader = (ImageReader)readers.next();
|
||||||
|
|
@ -120,7 +128,9 @@ public class AlbumServlet
|
||||||
if (true /* subsampling */) {
|
if (true /* subsampling */) {
|
||||||
double subSampleScale = (double)outd.getWidth() / orig.getWidth();
|
double subSampleScale = (double)outd.getWidth() / orig.getWidth();
|
||||||
int subSampling = (int)Math.floor(0.25d/subSampleScale);
|
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);
|
BufferedImage img = reader.read(0, param);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue