Support for PDF (1st page)
This commit is contained in:
parent
c46cd2e3fe
commit
6f674599da
3 changed files with 124 additions and 36 deletions
11
pom.xml
11
pom.xml
|
|
@ -237,6 +237,17 @@
|
|||
<version>3.9.4</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.pdfbox</groupId>
|
||||
<artifactId>pdfbox</artifactId>
|
||||
<version>3.0.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.pdfbox</groupId>
|
||||
<artifactId>pdfbox-io</artifactId>
|
||||
<version>3.0.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.eclipse.jetty</groupId>
|
||||
<artifactId>jetty-server</artifactId>
|
||||
|
|
|
|||
|
|
@ -6,11 +6,7 @@ import java.io.IOException;
|
|||
import java.text.DecimalFormat;
|
||||
import java.text.NumberFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.*;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.imageio.ImageReadParam;
|
||||
|
|
@ -19,6 +15,11 @@ import javax.imageio.stream.ImageInputStream;
|
|||
|
||||
import com.drew.imaging.ImageMetadataReader;
|
||||
import com.drew.imaging.ImageProcessingException;
|
||||
import org.apache.pdfbox.Loader;
|
||||
import org.apache.pdfbox.io.RandomAccessReadBufferedFile;
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.pdmodel.PDPage;
|
||||
import org.apache.pdfbox.pdmodel.common.PDRectangle;
|
||||
import org.forkalsrud.album.db.DirectoryProps;
|
||||
import org.forkalsrud.album.video.MovieMetadataGenerator;
|
||||
import org.slf4j.Logger;
|
||||
|
|
@ -91,6 +92,9 @@ public class DirectoryMetadataGenerator {
|
|||
} else if (name.endsWith(".mov") || name.endsWith(".MOV") || name.endsWith(".mp4") || name.endsWith(".m4v") || name.endsWith(".avi")) {
|
||||
Map<String, String> p = movieMetadataGenerator.generateVideoProperties(f);
|
||||
addPropsForFile(props, f, p);
|
||||
} else if (name.endsWith(".pdf")) {
|
||||
Map<String, String> p = generatePdfProperties(f);
|
||||
addPropsForFile(props, f, p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -229,4 +233,34 @@ public class DirectoryMetadataGenerator {
|
|||
iis.close();
|
||||
return new Dimension(img.getWidth(), img.getHeight());
|
||||
}
|
||||
|
||||
|
||||
|
||||
private Map<String, String> generatePdfProperties(File file) throws IOException {
|
||||
|
||||
HashMap<String, String> props = new HashMap<>();
|
||||
|
||||
String name = file.getName();
|
||||
props.put("etag", Integer.toHexString(name.hashCode() + Long.valueOf(file.lastModified()).hashCode()));
|
||||
|
||||
try (PDDocument document = Loader.loadPDF(new RandomAccessReadBufferedFile(file))) {
|
||||
|
||||
PDPage page = document.getPage(0);
|
||||
PDRectangle rect = page.getMediaBox();
|
||||
|
||||
|
||||
int width = Math.round(rect.getWidth());
|
||||
int height = Math.round(rect.getHeight());
|
||||
props.put("dimensions", new Dimension(width, height).toString());
|
||||
props.put("orientation", "1");
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HHmmss");
|
||||
Calendar cal = document.getDocumentInformation().getCreationDate();
|
||||
Date date = cal != null ? cal.getTime() : new Date(file.lastModified());
|
||||
props.put("captureDate", sdf.format(date));
|
||||
|
||||
}
|
||||
return props;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,6 +25,15 @@ import javax.imageio.ImageWriteParam;
|
|||
import javax.imageio.ImageWriter;
|
||||
import javax.imageio.stream.ImageOutputStream;
|
||||
|
||||
import org.apache.pdfbox.Loader;
|
||||
import org.apache.pdfbox.cos.COSDocument;
|
||||
import org.apache.pdfbox.io.RandomAccessRead;
|
||||
import org.apache.pdfbox.io.RandomAccessReadBufferedFile;
|
||||
import org.apache.pdfbox.io.RandomAccessReadMemoryMappedFile;
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.pdmodel.PDPage;
|
||||
import org.apache.pdfbox.pdmodel.common.PDRectangle;
|
||||
import org.apache.pdfbox.rendering.PDFRenderer;
|
||||
import org.forkalsrud.album.exif.Dimension;
|
||||
import org.forkalsrud.album.exif.Thumbnail;
|
||||
|
||||
|
|
@ -86,6 +95,9 @@ public class PictureScaler {
|
|||
@Override
|
||||
public CachedImage call() throws Exception {
|
||||
try {
|
||||
if (file.getName().endsWith(".pdf")) {
|
||||
return renderPdf(file, thumbnail, size);
|
||||
}
|
||||
return scalePictureReally(file, thumbnail, size);
|
||||
} catch (Exception e) {
|
||||
log.error("sadness: " + file.getAbsolutePath(), e);
|
||||
|
|
@ -223,7 +235,11 @@ public class PictureScaler {
|
|||
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
||||
g2.drawImage(buf3, 0, 0, intermediate.getWidth(), intermediate.getHeight(), null);
|
||||
}
|
||||
return encoded(buf2, file.lastModified());
|
||||
}
|
||||
|
||||
|
||||
private CachedImage encoded(BufferedImage buf2, long lastModifiedTs) throws IOException {
|
||||
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
|
||||
ImageWriter writer = writers.next();
|
||||
ImageWriteParam writeParam = writer.getDefaultWriteParam();
|
||||
|
|
@ -241,10 +257,37 @@ public class PictureScaler {
|
|||
ios.flush();
|
||||
|
||||
CachedImage cimg = new CachedImage();
|
||||
cimg.lastModified = file.lastModified();
|
||||
cimg.lastModified = lastModifiedTs;
|
||||
cimg.mimeType = "image/jpeg";
|
||||
cimg.bits = bits.toByteArray();
|
||||
return cimg;
|
||||
}
|
||||
|
||||
|
||||
CachedImage renderPdf(File file, Thumbnail thumbnail, String size) throws IOException {
|
||||
|
||||
|
||||
Dimension orig = thumbnail.getSize();
|
||||
Dimension dim = orig.scale(size);
|
||||
|
||||
|
||||
|
||||
try (PDDocument document = Loader.loadPDF(new RandomAccessReadBufferedFile(file))) {
|
||||
|
||||
PDPage page = document.getPage(0);
|
||||
PDRectangle rect = page.getMediaBox();
|
||||
|
||||
|
||||
float width = rect.getWidth() / 72; // inches
|
||||
float targetWidth = dim.getWidth(); // pixels
|
||||
|
||||
PDFRenderer renderer = new PDFRenderer(document);
|
||||
BufferedImage image = renderer.renderImageWithDPI(0, targetWidth / width);
|
||||
|
||||
|
||||
return encoded(image, file.lastModified());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue