summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Heher <jakob.heher@iaik.tugraz.at>2022-08-04 16:27:31 +0200
committerJakob Heher <jakob.heher@iaik.tugraz.at>2022-08-04 16:27:31 +0200
commit8e6acd9237c2458c66f8c747a64a1feb68172b35 (patch)
tree00ba6a0a4df079836520e5a942c5f55ac75c27ee
parent256a1b2cfbc72c33eb08d361591bdb3f52a34b64 (diff)
downloadpdf-over-8e6acd9237c2458c66f8c747a64a1feb68172b35.tar.gz
pdf-over-8e6acd9237c2458c66f8c747a64a1feb68172b35.tar.bz2
pdf-over-8e6acd9237c2458c66f8c747a64a1feb68172b35.zip
emblem image parsing for width/height cf. #113
-rw-r--r--pdf-over-signator/src/main/java/at/asit/pdfover/signator/Emblem.java39
-rw-r--r--pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SignatureParameter.java2
-rw-r--r--pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4Signer.java2
3 files changed, 29 insertions, 14 deletions
diff --git a/pdf-over-signator/src/main/java/at/asit/pdfover/signator/Emblem.java b/pdf-over-signator/src/main/java/at/asit/pdfover/signator/Emblem.java
index 4c093d29..26f05f08 100644
--- a/pdf-over-signator/src/main/java/at/asit/pdfover/signator/Emblem.java
+++ b/pdf-over-signator/src/main/java/at/asit/pdfover/signator/Emblem.java
@@ -20,6 +20,7 @@ import at.asit.pdfover.commons.Constants;
import at.asit.pdfover.commons.utils.ImageUtil;
import java.awt.Graphics2D;
+import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
@@ -38,7 +39,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
- *
+ * TODO all of this caching business is a bit of a mess
*/
public class Emblem {
/**
@@ -56,18 +57,31 @@ public class Emblem {
private static final int MAX_EMBLEM_WIDTH = 480;
private static final int MAX_EMBLEM_HEIGHT = 600;
- private String fileName = null;
+ private String originalFileName = null;
+ private Image image = null; /* image data, if we have it */
+
+ private void lazyLoadImage() {
+ if (this.image != null) return;
+
+ String filename = getCachedFileName();
+ if (this.image != null) return; /* getCachedFileName may have re-generated the cache and populated this.image */
+
+ try {
+ image = ImageUtil.readImageWithEXIFRotation(new File(filename));
+ } catch (IOException e) {
+ log.warn("Failed to load Emblem image");
+ }
+ }
- // TODO actual values
- public int getWidth() { return 190; }
- public int getHeight() { return 96; }
+ public int getWidth() { if (image == null) lazyLoadImage(); return (image != null) ? image.getWidth(null) : 0; }
+ public int getHeight() { if (image == null) lazyLoadImage(); return (image != null) ? image.getHeight(null) : 0; }
/**
* Constructor
* @param filename
*/
public Emblem(String filename) {
- this.fileName = filename;
+ this.originalFileName = filename;
}
private String getFileHash(String filename) throws IOException {
@@ -106,8 +120,8 @@ public class Emblem {
/* (non-Javadoc)
* @see at.asit.pdfover.signator.Emblem#getFileName()
*/
- public String getFileName() {
- String emblemImg = this.fileName;
+ public String getCachedFileName() {
+ String emblemImg = this.originalFileName;
String emblemHsh = null;
String cachedEmblemFileName = CACHE_DIR + File.separator + CACHE_IMG_FILENAME;
@@ -144,11 +158,12 @@ public class Emblem {
File file = new File(CACHE_DIR, CACHE_IMG_FILENAME);
ImageIO.write(img, CACHE_IMG_FORMAT, file); // ignore returned boolean
+ this.image = img;
OutputStream out = new FileOutputStream(new File(CACHE_DIR, CACHE_PROPS_FILENAME));
emblemProps.store(out, null);
} catch (IOException e) {
log.error("Can't save emblem cache", e);
- return this.fileName;
+ return this.originalFileName;
}
return cachedEmblemFileName;
}
@@ -158,7 +173,7 @@ public class Emblem {
* @return the original filename
*/
public String getOriginalFileName() {
- return this.fileName;
+ return this.originalFileName;
}
/**
@@ -166,10 +181,10 @@ public class Emblem {
* @return the original filename
*/
public String getOriginalFileHash() {
- if (this.fileName == null || !(new File(this.fileName).exists()))
+ if (this.originalFileName == null || !(new File(this.originalFileName).exists()))
return "";
try {
- return getFileHash(this.fileName);
+ return getFileHash(this.originalFileName);
} catch (IOException e) {
log.debug("Error getting file hash", e);
return "";
diff --git a/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SignatureParameter.java b/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SignatureParameter.java
index cea41bdd..c52625dd 100644
--- a/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SignatureParameter.java
+++ b/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SignatureParameter.java
@@ -85,7 +85,7 @@ public class PdfAs4SignatureParameter {
public Image getPlaceholder() {
String sigProfile = getPdfAsSignatureProfileId();
- String sigEmblem = (this.emblem == null ? null : this.emblem.getFileName());
+ String sigEmblem = (this.emblem == null ? null : this.emblem.getCachedFileName());
String sigNote = getProperty("SIG_NOTE");
try {
diff --git a/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4Signer.java b/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4Signer.java
index 952f1971..7a67a35a 100644
--- a/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4Signer.java
+++ b/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4Signer.java
@@ -46,7 +46,7 @@ public class PdfAs4Signer {
}
String sigProfile = parameter.getPdfAsSignatureProfileId();
- String sigEmblem = (parameter.emblem == null ? null : parameter.emblem.getFileName());
+ String sigEmblem = (parameter.emblem == null ? null : parameter.emblem.getCachedFileName());
String sigNote = parameter.getProperty("SIG_NOTE");
String sigPos = null;
if (parameter.signaturePosition != null) {