summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Heher <jakob.heher@iaik.tugraz.at>2022-08-02 16:04:49 +0200
committerJakob Heher <jakob.heher@iaik.tugraz.at>2022-08-02 16:04:49 +0200
commita3a190f41b5b509f2af1c1565ad9599169cf7f82 (patch)
treef617c0ef11ea37178a29089db41a2c4bcb376c30
parent84a57e47b435e5b44e827536b3eb29fae7ec3501 (diff)
downloadpdf-over-a3a190f41b5b509f2af1c1565ad9599169cf7f82.tar.gz
pdf-over-a3a190f41b5b509f2af1c1565ad9599169cf7f82.tar.bz2
pdf-over-a3a190f41b5b509f2af1c1565ad9599169cf7f82.zip
YAGNI: Emblem interface
-rw-r--r--pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/configuration/SimpleConfigurationComposite.java16
-rw-r--r--pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/SignaturePlaceholderCache.java9
-rw-r--r--pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/config/ConfigurationDataInMemory.java2
-rw-r--r--pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/config/ConfigurationManager.java6
-rw-r--r--pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PositioningState.java3
-rw-r--r--pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PrepareSigningState.java6
-rw-r--r--pdf-over-signator/src/main/java/at/asit/pdfover/signator/CachedFileNameEmblem.java271
-rw-r--r--pdf-over-signator/src/main/java/at/asit/pdfover/signator/Emblem.java253
8 files changed, 266 insertions, 300 deletions
diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/configuration/SimpleConfigurationComposite.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/configuration/SimpleConfigurationComposite.java
index e821d78f..4fb4874e 100644
--- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/configuration/SimpleConfigurationComposite.java
+++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/configuration/SimpleConfigurationComposite.java
@@ -66,7 +66,7 @@ import at.asit.pdfover.gui.utils.ImageConverter;
import at.asit.pdfover.gui.workflow.config.ConfigurationManager;
import at.asit.pdfover.gui.workflow.config.ConfigurationDataInMemory;
import at.asit.pdfover.gui.workflow.states.State;
-import at.asit.pdfover.signator.CachedFileNameEmblem;
+import at.asit.pdfover.signator.Emblem;
import at.asit.pdfover.signer.pdfas.PdfAs4SignatureParameter;
/**
@@ -423,8 +423,8 @@ public class SimpleConfigurationComposite extends ConfigurationCompositeBase {
private void setEmblemFileInternal(final String filename, boolean force)
throws Exception {
- if (!force && this.configurationContainer.getEmblem() != null) {
- if (this.configurationContainer.getEmblem().equals(filename)) {
+ if (!force && this.configurationContainer.getEmblemPath() != null) {
+ if (this.configurationContainer.getEmblemPath().equals(filename)) {
return; // Ignore ...
}
}
@@ -435,7 +435,7 @@ public class SimpleConfigurationComposite extends ConfigurationCompositeBase {
}
void updateSignatureBlockPreview() {
- String image = this.configurationContainer.getEmblem();
+ String image = this.configurationContainer.getEmblemPath();
ImageData img = null;
ImageData logo = null;
@@ -449,7 +449,7 @@ public class SimpleConfigurationComposite extends ConfigurationCompositeBase {
param.enablePDFACompat = this.configurationContainer.signaturePDFACompat;
if (image != null && !image.trim().isEmpty()) {
logo = new ImageData(image);
- param.emblem = new CachedFileNameEmblem(image);
+ param.emblem = new Emblem(image);
}
//TODO deactivated the placeholder preview
//TODO display accurate placeholder preview -> now its only standard placeholder shown
@@ -468,7 +468,7 @@ public class SimpleConfigurationComposite extends ConfigurationCompositeBase {
try {
File imgFile = new File(image);
this.logo = new Image(this.getDisplay(),
- ImageConverter.convertToSWT(CachedFileNameEmblem.fixImage(
+ ImageConverter.convertToSWT(Emblem.fixImage(
ImageIO.read(imgFile), imgFile)));
} catch (IOException e) {
log.error("Error reading image", e);
@@ -604,7 +604,7 @@ public class SimpleConfigurationComposite extends ConfigurationCompositeBase {
this.txtMobileNumber.setText(number);
}
- String emblemFile = this.configurationContainer.getEmblem();
+ String emblemFile = this.configurationContainer.getEmblemPath();
if (emblemFile != null && !emblemFile.trim().isEmpty()) {
this.logoFile = emblemFile;
try {
@@ -636,7 +636,7 @@ public class SimpleConfigurationComposite extends ConfigurationCompositeBase {
@Override
public void storeConfiguration(ConfigurationManager store) {
store.setDefaultMobileNumberPersistent(this.configurationContainer.getMobileNumber());
- store.setDefaultEmblemPersistent(this.configurationContainer.getEmblem());
+ store.setDefaultEmblemPersistent(this.configurationContainer.getEmblemPath());
store.setSignatureLocalePersistent(this.configurationContainer.signatureLocale);
store.setSignatureNotePersistent(this.configurationContainer.signatureNote);
store.setSignatureProfilePersistent(this.configurationContainer.getSignatureProfile().name());
diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/SignaturePlaceholderCache.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/SignaturePlaceholderCache.java
index 80b27183..18009895 100644
--- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/SignaturePlaceholderCache.java
+++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/SignaturePlaceholderCache.java
@@ -34,7 +34,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import at.asit.pdfover.commons.Constants;
-import at.asit.pdfover.signator.CachedFileNameEmblem;
import at.asit.pdfover.signator.Emblem;
import at.asit.pdfover.signer.pdfas.PdfAs4SignatureParameter;
@@ -79,12 +78,8 @@ public class SignaturePlaceholderCache {
String sigEHsh = "";
if (param.emblem != null) {
Emblem embl = param.emblem;
- if (embl instanceof CachedFileNameEmblem) {
- sigEmbl = ((CachedFileNameEmblem) embl).getOriginalFileName();
- sigEHsh = ((CachedFileNameEmblem) embl).getOriginalFileHash();
- } else {
- sigEmbl = embl.getFileName();
- }
+ sigEmbl = embl.getOriginalFileName();
+ sigEHsh = embl.getOriginalFileHash();
}
String sigPdfA = param.enablePDFACompat ? Constants.TRUE : Constants.FALSE;
String sigNote = param.getProperty("SIG_NOTE");
diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/config/ConfigurationDataInMemory.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/config/ConfigurationDataInMemory.java
index 7fb08602..39d557c5 100644
--- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/config/ConfigurationDataInMemory.java
+++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/config/ConfigurationDataInMemory.java
@@ -47,7 +47,7 @@ public class ConfigurationDataInMemory {
/** the emblem File */
protected String emblemFile = null;
- public String getEmblem() { return this.emblemFile; }
+ public String getEmblemPath() { return this.emblemFile; }
public void setEmblem(String emblemFile) throws InvalidEmblemFile {
if (emblemFile == null || emblemFile.trim().isEmpty()) {
// Ok to set no file ...
diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/config/ConfigurationManager.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/config/ConfigurationManager.java
index d467178a..df059e68 100644
--- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/config/ConfigurationManager.java
+++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/config/ConfigurationManager.java
@@ -521,15 +521,15 @@ public class ConfigurationManager {
}
}
- public String getDefaultEmblem() {
- String emblem = this.configurationOverlay.getEmblem();
+ public String getDefaultEmblemPath() {
+ String emblem = this.configurationOverlay.getEmblemPath();
if (emblem == null)
emblem = getDefaultEmblemPersistent();
return emblem;
}
public String getDefaultEmblemPersistent() {
- String emblem = this.configuration.getEmblem();
+ String emblem = this.configuration.getEmblemPath();
if (emblem == null)
emblem = STRING_EMPTY;
return emblem;
diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PositioningState.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PositioningState.java
index 5ed63e0b..a09a7a6b 100644
--- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PositioningState.java
+++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PositioningState.java
@@ -35,7 +35,6 @@ import at.asit.pdfover.gui.utils.SignaturePlaceholderCache;
import at.asit.pdfover.gui.workflow.StateMachine;
import at.asit.pdfover.gui.workflow.Status;
import at.asit.pdfover.gui.workflow.config.ConfigurationManager;
-import at.asit.pdfover.signator.CachedFileNameEmblem;
import at.asit.pdfover.signator.Emblem;
import at.asit.pdfover.signator.SignaturePosition;
import at.asit.pdfover.signer.pdfas.PdfAs4SignatureParameter;
@@ -108,7 +107,7 @@ public class PositioningState extends State {
// Update possibly changed values
ConfigurationManager config = stateMachine.configProvider;
PdfAs4SignatureParameter param = new PdfAs4SignatureParameter();
- Emblem emblem = new CachedFileNameEmblem(config.getDefaultEmblem());
+ Emblem emblem = new Emblem(config.getDefaultEmblemPath());
param.emblem = emblem;
if(config.getSignatureNote() != null && !config.getSignatureNote().isEmpty()) {
param.setProperty("SIG_NOTE", config.getSignatureNote());
diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PrepareSigningState.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PrepareSigningState.java
index 786ed920..bb3190ef 100644
--- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PrepareSigningState.java
+++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PrepareSigningState.java
@@ -32,7 +32,7 @@ import at.asit.pdfover.commons.Messages;
import at.asit.pdfover.gui.workflow.StateMachine;
import at.asit.pdfover.gui.workflow.Status;
import at.asit.pdfover.gui.workflow.config.ConfigurationManager;
-import at.asit.pdfover.signator.CachedFileNameEmblem;
+import at.asit.pdfover.signator.Emblem;
import at.asit.pdfover.signator.PDFFileDocumentSource;
import at.asit.pdfover.signer.pdfas.PdfAs4SignatureParameter;
import at.asit.pdfover.signer.pdfas.PdfAs4Signer;
@@ -134,8 +134,8 @@ public class PrepareSigningState extends State {
this.state.signatureParameter.signaturePosition = status.signaturePosition;
}
- if (configuration.getDefaultEmblem() != null && !configuration.getDefaultEmblem().isEmpty()) {
- this.state.signatureParameter.emblem = new CachedFileNameEmblem(configuration.getDefaultEmblem());
+ if (configuration.getDefaultEmblemPath() != null && !configuration.getDefaultEmblemPath().isEmpty()) {
+ this.state.signatureParameter.emblem = new Emblem(configuration.getDefaultEmblemPath());
}
if (configuration.getSignatureNote() != null
diff --git a/pdf-over-signator/src/main/java/at/asit/pdfover/signator/CachedFileNameEmblem.java b/pdf-over-signator/src/main/java/at/asit/pdfover/signator/CachedFileNameEmblem.java
deleted file mode 100644
index 236fb905..00000000
--- a/pdf-over-signator/src/main/java/at/asit/pdfover/signator/CachedFileNameEmblem.java
+++ /dev/null
@@ -1,271 +0,0 @@
-/*
- * Copyright 2012 by A-SIT, Secure Information Technology Center Austria
- *
- * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by
- * the European Commission - subsequent versions of the EUPL (the "Licence");
- * You may not use this work except in compliance with the Licence.
- * You may obtain a copy of the Licence at:
- * http://joinup.ec.europa.eu/software/page/eupl
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the Licence is distributed on an "AS IS" basis,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the Licence for the specific language governing permissions and
- * limitations under the Licence.
- */
-package at.asit.pdfover.signator;
-
-// Imports
-import java.awt.Graphics2D;
-import java.awt.image.BufferedImage;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.nio.file.Files;
-import java.nio.file.Paths;
-import java.util.Iterator;
-import java.util.Properties;
-
-import javax.imageio.ImageIO;
-import javax.imageio.ImageReader;
-import javax.imageio.stream.ImageInputStream;
-
-import org.apache.commons.codec.digest.DigestUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.drew.imaging.ImageMetadataReader;
-import com.drew.imaging.ImageProcessingException;
-import com.drew.metadata.Metadata;
-import com.drew.metadata.MetadataException;
-import com.drew.metadata.exif.ExifDirectoryBase;
-import com.drew.metadata.exif.ExifIFD0Directory;
-
-/**
- *
- */
-public class CachedFileNameEmblem implements Emblem {
- /**
- * SLF4J Logger instance
- **/
- private static final Logger log = LoggerFactory.getLogger(CachedFileNameEmblem.class);
-
- private final String fileDir = System.getProperty("user.home") + File.separator + ".pdf-over";
- private final String imgFileName = ".emblem";
- private final String imgFileExt = "png";
- private final String propFileName = ".emblem.properties";
-
- private final String imgProp = "IMG";
- private final String hshProp = "HSH";
- private final int maxWidth = 480;
- private final int maxHeight = 600;
-
- private String fileName = null;
-
- /**
- * Constructor
- * @param filename
- */
- public CachedFileNameEmblem(String filename) {
- this.fileName = filename;
- }
-
- private String getFileHash(String filename) throws IOException {
- InputStream is = Files.newInputStream(Paths.get(this.fileName));
- return DigestUtils.md5Hex(is);
- }
-
- /**
- * Correctly rotate JPEG image by EXIF header
- * @param img the image
- * @param imgFile the image file
- * @return the fixed image
- */
- public static BufferedImage fixImage(BufferedImage img, File imgFile) {
- int oheight = img.getHeight();
- int owidth = img.getWidth();
-
- // Read EXIF metadata for jpeg
- ImageInputStream iis = null;
- try {
- iis = ImageIO.createImageInputStream(imgFile);
- Iterator<ImageReader> imageReaders = ImageIO.getImageReaders(iis);
-
- while (imageReaders.hasNext()) {
- ImageReader reader = imageReaders.next();
- log.debug(reader.getFormatName());
- if (reader.getFormatName().equals("JPEG")) {
- try {
- Metadata metadata = ImageMetadataReader.readMetadata(imgFile);
- ExifIFD0Directory metaDirectory = (metadata != null) ? metadata.getFirstDirectoryOfType(ExifIFD0Directory.class) : null;
- if (metaDirectory != null) {
- int orientation = metaDirectory.getInt(
- ExifDirectoryBase.TAG_ORIENTATION);
- if (orientation > 2) {
- // rotate
- double rotation = 0;
- int height = owidth;
- int width = oheight;
- switch ((orientation + 1) / 2) {
- case 2:
- rotation = Math.PI;
- height = oheight;
- width = owidth;
- break;
- case 3:
- rotation = Math.PI / 2;
- break;
- case 4:
- rotation = 3 * Math.PI / 2;
- break;
- }
- log.debug("EXIF orientation " + orientation + ", rotating " + rotation + "rad");
- BufferedImage result = new BufferedImage(width, height, img.getType());
- Graphics2D g = result.createGraphics();
- g.translate((width - owidth) / 2, (height - oheight) / 2);
- g.rotate(rotation, owidth / 2, oheight / 2);
- g.drawRenderedImage(img, null);
- g.dispose();
- img = result;
- owidth = width;
- oheight = height;
- }
- if (((orientation < 5) && (orientation % 2 == 0)) ||
- (orientation > 5) && (orientation % 2 == 1)) {
- // flip
- log.debug("flipping image");
- BufferedImage result = new BufferedImage(owidth, oheight, img.getType());
- Graphics2D g = result.createGraphics();
- g.drawImage(img, owidth, 0, -owidth, oheight, null);
- g.dispose();
- img = result;
- }
- }
- } catch (ImageProcessingException e) {
- log.error("Error reading emblem metadata", e);
- } catch (MetadataException e) {
- log.error("Error reading emblem metadata", e);
- } catch (NullPointerException e) {
- log.error("Error reading emblem metadata", e);
- }
- }
- }
- } catch (IOException e) {
- log.error("Error reading image" , e);
- } finally {
- try {
- if (iis != null)
- iis.close();
- } catch (IOException e) {
- log.debug("Error closing stream", e);
- }
- }
- return img;
- }
-
- private static BufferedImage scaleImage(BufferedImage img, int maxWidth, int maxHeight) {
- int oheight = img.getHeight();
- int owidth = img.getWidth();
-
- double ratio = (double)owidth/(double)oheight;
-
- int height = oheight;
- int width = owidth;
- if (height > maxHeight) {
- height = maxHeight;
- width = (int) (maxHeight * ratio);
- }
- if (width > maxWidth) {
- width = maxWidth;
- height = (int) (maxWidth / ratio);
- }
- BufferedImage result = img;
- if (width != owidth || height == oheight) {
- //scale image
- log.debug("Scaling emblem: " + owidth + "x" + oheight + " to " + width + "x" + height);
- result = new BufferedImage(width, height, img.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : img.getType());
- Graphics2D g = result.createGraphics();
- g.drawImage(img, 0, 0, width, height, null);
- g.dispose();
- }
- return result;
- }
-
- /* (non-Javadoc)
- * @see at.asit.pdfover.signator.Emblem#getFileName()
- */
- @Override
- public String getFileName() {
- String emblemImg = this.fileName;
- String emblemHsh = null;
- String cachedEmblemFileName = this.fileDir + File.separator + this.imgFileName + "." + this.imgFileExt;
-
- if (emblemImg == null || !(new File(emblemImg).exists()))
- return null;
-
- Properties emblemProps = new Properties();
- // compare cache, try to load if match
- try {
- InputStream in = new FileInputStream(new File(this.fileDir, this.propFileName));
- emblemProps.load(in);
- if (emblemImg.equals(emblemProps.getProperty(this.imgProp))) {
- emblemHsh = getFileHash(emblemImg);
- if (emblemHsh.equals(emblemProps.getProperty(this.hshProp))) {
- log.debug("Emblem cache hit: " + cachedEmblemFileName);
- return cachedEmblemFileName;
- }
- }
- log.debug("Emblem cache miss");
- } catch (Exception e) {
- log.warn("Can't load emblem cache", e);
- }
-
- try {
- // create new cache
- if (emblemHsh == null)
- emblemHsh = getFileHash(emblemImg);
- emblemProps.setProperty(this.imgProp, emblemImg);
- emblemProps.setProperty(this.hshProp, emblemHsh);
- File imgFile = new File(emblemImg);
-
- BufferedImage img = ImageIO.read(imgFile);
- img = fixImage(img, imgFile);
- img = scaleImage(img, this.maxWidth, this.maxHeight);
-
- File file = new File(this.fileDir, this.imgFileName + "." + this.imgFileExt);
- ImageIO.write(img, this.imgFileExt, file); // ignore returned boolean
- OutputStream out = new FileOutputStream(new File(this.fileDir, this.propFileName));
- emblemProps.store(out, null);
- } catch (IOException e) {
- log.error("Can't save emblem cache", e);
- return this.fileName;
- }
- return cachedEmblemFileName;
- }
-
- /**
- * Return the original filename
- * @return the original filename
- */
- public String getOriginalFileName() {
- return this.fileName;
- }
-
- /**
- * Return the original filename
- * @return the original filename
- */
- public String getOriginalFileHash() {
- if (this.fileName == null || !(new File(this.fileName).exists()))
- return "";
- try {
- return getFileHash(this.fileName);
- } catch (IOException e) {
- log.debug("Error getting file hash", e);
- return "";
- }
- }
-}
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 71f764d3..6c7d208e 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
@@ -15,13 +15,256 @@
*/
package at.asit.pdfover.signator;
+// Imports
+import java.awt.Graphics2D;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.Iterator;
+import java.util.Properties;
+
+import javax.imageio.ImageIO;
+import javax.imageio.ImageReader;
+import javax.imageio.stream.ImageInputStream;
+
+import org.apache.commons.codec.digest.DigestUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.drew.imaging.ImageMetadataReader;
+import com.drew.imaging.ImageProcessingException;
+import com.drew.metadata.Metadata;
+import com.drew.metadata.MetadataException;
+import com.drew.metadata.exif.ExifDirectoryBase;
+import com.drew.metadata.exif.ExifIFD0Directory;
+
/**
- * Provides a logo for a signature block
+ *
*/
-public interface Emblem {
+public class Emblem {
+ /**
+ * SLF4J Logger instance
+ **/
+ private static final Logger log = LoggerFactory.getLogger(Emblem.class);
+
+ private final String fileDir = System.getProperty("user.home") + File.separator + ".pdf-over";
+ private final String imgFileName = ".emblem";
+ private final String imgFileExt = "png";
+ private final String propFileName = ".emblem.properties";
+
+ private final String imgProp = "IMG";
+ private final String hshProp = "HSH";
+ private final int maxWidth = 480;
+ private final int maxHeight = 600;
+
+ private String fileName = null;
+
+ /**
+ * Constructor
+ * @param filename
+ */
+ public Emblem(String filename) {
+ this.fileName = filename;
+ }
+
+ private String getFileHash(String filename) throws IOException {
+ InputStream is = Files.newInputStream(Paths.get(this.fileName));
+ return DigestUtils.md5Hex(is);
+ }
+
+ /**
+ * Correctly rotate JPEG image by EXIF header
+ * @param img the image
+ * @param imgFile the image file
+ * @return the fixed image
+ */
+ public static BufferedImage fixImage(BufferedImage img, File imgFile) {
+ int oheight = img.getHeight();
+ int owidth = img.getWidth();
+
+ // Read EXIF metadata for jpeg
+ ImageInputStream iis = null;
+ try {
+ iis = ImageIO.createImageInputStream(imgFile);
+ Iterator<ImageReader> imageReaders = ImageIO.getImageReaders(iis);
+
+ while (imageReaders.hasNext()) {
+ ImageReader reader = imageReaders.next();
+ log.debug(reader.getFormatName());
+ if (reader.getFormatName().equals("JPEG")) {
+ try {
+ Metadata metadata = ImageMetadataReader.readMetadata(imgFile);
+ ExifIFD0Directory metaDirectory = (metadata != null) ? metadata.getFirstDirectoryOfType(ExifIFD0Directory.class) : null;
+ if (metaDirectory != null) {
+ int orientation = metaDirectory.getInt(
+ ExifDirectoryBase.TAG_ORIENTATION);
+ if (orientation > 2) {
+ // rotate
+ double rotation = 0;
+ int height = owidth;
+ int width = oheight;
+ switch ((orientation + 1) / 2) {
+ case 2:
+ rotation = Math.PI;
+ height = oheight;
+ width = owidth;
+ break;
+ case 3:
+ rotation = Math.PI / 2;
+ break;
+ case 4:
+ rotation = 3 * Math.PI / 2;
+ break;
+ }
+ log.debug("EXIF orientation " + orientation + ", rotating " + rotation + "rad");
+ BufferedImage result = new BufferedImage(width, height, img.getType());
+ Graphics2D g = result.createGraphics();
+ g.translate((width - owidth) / 2, (height - oheight) / 2);
+ g.rotate(rotation, owidth / 2, oheight / 2);
+ g.drawRenderedImage(img, null);
+ g.dispose();
+ img = result;
+ owidth = width;
+ oheight = height;
+ }
+ if (((orientation < 5) && (orientation % 2 == 0)) ||
+ (orientation > 5) && (orientation % 2 == 1)) {
+ // flip
+ log.debug("flipping image");
+ BufferedImage result = new BufferedImage(owidth, oheight, img.getType());
+ Graphics2D g = result.createGraphics();
+ g.drawImage(img, owidth, 0, -owidth, oheight, null);
+ g.dispose();
+ img = result;
+ }
+ }
+ } catch (ImageProcessingException e) {
+ log.error("Error reading emblem metadata", e);
+ } catch (MetadataException e) {
+ log.error("Error reading emblem metadata", e);
+ } catch (NullPointerException e) {
+ log.error("Error reading emblem metadata", e);
+ }
+ }
+ }
+ } catch (IOException e) {
+ log.error("Error reading image" , e);
+ } finally {
+ try {
+ if (iis != null)
+ iis.close();
+ } catch (IOException e) {
+ log.debug("Error closing stream", e);
+ }
+ }
+ return img;
+ }
+
+ private static BufferedImage scaleImage(BufferedImage img, int maxWidth, int maxHeight) {
+ int oheight = img.getHeight();
+ int owidth = img.getWidth();
+
+ double ratio = (double)owidth/(double)oheight;
+
+ int height = oheight;
+ int width = owidth;
+ if (height > maxHeight) {
+ height = maxHeight;
+ width = (int) (maxHeight * ratio);
+ }
+ if (width > maxWidth) {
+ width = maxWidth;
+ height = (int) (maxWidth / ratio);
+ }
+ BufferedImage result = img;
+ if (width != owidth || height == oheight) {
+ //scale image
+ log.debug("Scaling emblem: " + owidth + "x" + oheight + " to " + width + "x" + height);
+ result = new BufferedImage(width, height, img.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : img.getType());
+ Graphics2D g = result.createGraphics();
+ g.drawImage(img, 0, 0, width, height, null);
+ g.dispose();
+ }
+ return result;
+ }
+
+ /* (non-Javadoc)
+ * @see at.asit.pdfover.signator.Emblem#getFileName()
+ */
+ public String getFileName() {
+ String emblemImg = this.fileName;
+ String emblemHsh = null;
+ String cachedEmblemFileName = this.fileDir + File.separator + this.imgFileName + "." + this.imgFileExt;
+
+ if (emblemImg == null || !(new File(emblemImg).exists()))
+ return null;
+
+ Properties emblemProps = new Properties();
+ // compare cache, try to load if match
+ try {
+ InputStream in = new FileInputStream(new File(this.fileDir, this.propFileName));
+ emblemProps.load(in);
+ if (emblemImg.equals(emblemProps.getProperty(this.imgProp))) {
+ emblemHsh = getFileHash(emblemImg);
+ if (emblemHsh.equals(emblemProps.getProperty(this.hshProp))) {
+ log.debug("Emblem cache hit: " + cachedEmblemFileName);
+ return cachedEmblemFileName;
+ }
+ }
+ log.debug("Emblem cache miss");
+ } catch (Exception e) {
+ log.warn("Can't load emblem cache", e);
+ }
+
+ try {
+ // create new cache
+ if (emblemHsh == null)
+ emblemHsh = getFileHash(emblemImg);
+ emblemProps.setProperty(this.imgProp, emblemImg);
+ emblemProps.setProperty(this.hshProp, emblemHsh);
+ File imgFile = new File(emblemImg);
+
+ BufferedImage img = ImageIO.read(imgFile);
+ img = fixImage(img, imgFile);
+ img = scaleImage(img, this.maxWidth, this.maxHeight);
+
+ File file = new File(this.fileDir, this.imgFileName + "." + this.imgFileExt);
+ ImageIO.write(img, this.imgFileExt, file); // ignore returned boolean
+ OutputStream out = new FileOutputStream(new File(this.fileDir, this.propFileName));
+ emblemProps.store(out, null);
+ } catch (IOException e) {
+ log.error("Can't save emblem cache", e);
+ return this.fileName;
+ }
+ return cachedEmblemFileName;
+ }
+
+ /**
+ * Return the original filename
+ * @return the original filename
+ */
+ public String getOriginalFileName() {
+ return this.fileName;
+ }
+
/**
- * Return the file name of the Emblem
- * @return the file name
+ * Return the original filename
+ * @return the original filename
*/
- public String getFileName();
+ public String getOriginalFileHash() {
+ if (this.fileName == null || !(new File(this.fileName).exists()))
+ return "";
+ try {
+ return getFileHash(this.fileName);
+ } catch (IOException e) {
+ log.debug("Error getting file hash", e);
+ return "";
+ }
+ }
}