From 5c4f6dd07b601992e9afd2d8201a46519e04be2b Mon Sep 17 00:00:00 2001 From: Andreas Fitzek Date: Mon, 9 Jan 2017 09:26:07 +0100 Subject: Fixed NullPointerException with reading EXIF Metadata for JPEG Files --- .../pdfover/signator/CachedFileNameEmblem.java | 85 ++++++++++++---------- 1 file changed, 45 insertions(+), 40 deletions(-) (limited to 'pdf-over-signator/src/main/java/at') 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 index 73f25837..2b70afa3 100644 --- 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 @@ -101,52 +101,57 @@ public class CachedFileNameEmblem implements Emblem { if (reader.getFormatName().equals("JPEG")) { try { Metadata metadata = ImageMetadataReader.readMetadata(imgFile); - int orientation = metadata.getFirstDirectoryOfType( - ExifIFD0Directory.class).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; + + if(metadata != null) { + int orientation = metadata.getFirstDirectoryOfType( + ExifIFD0Directory.class).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; } - 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); } } } -- cgit v1.2.3