diff --git a/src/main/java/w2phtml/base/BinaryGraphicsDocument.java b/src/main/java/w2phtml/base/BinaryGraphicsDocument.java index d5e8dec..8e524b2 100644 --- a/src/main/java/w2phtml/base/BinaryGraphicsDocument.java +++ b/src/main/java/w2phtml/base/BinaryGraphicsDocument.java @@ -232,7 +232,6 @@ public class BinaryGraphicsDocument implements OutputFile { return; } ByteArrayInputStream bis = new ByteArrayInputStream(blob); - try { BufferedImage image = ImageIO.read(bis); if (image == null) { @@ -243,20 +242,26 @@ public class BinaryGraphicsDocument implements OutputFile { int width = image.getWidth(); int leftOffset = offsets[3]; int topOffset = offsets[0]; - int newWidth = width - offsets[3] - offsets[1]; - int newHeight = height - offsets[0] - offsets[2]; - BufferedImage croppedImage = image.getSubimage(leftOffset, topOffset, newWidth , newHeight ); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); + int croppedWidth = width - offsets[3] - offsets[1]; + int croppedHeight = height - offsets[0] - offsets[2]; + BufferedImage croppedImage = new BufferedImage(croppedWidth, croppedHeight, BufferedImage.TYPE_3BYTE_BGR); + int px[] = new int[croppedWidth * croppedHeight]; + image.getRGB(leftOffset, topOffset, croppedWidth, croppedHeight, px, 0, croppedWidth); + croppedImage.setRGB(0, 0, croppedWidth, croppedHeight, px, 0, croppedWidth); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(croppedImage, "JPG", baos); sMimeType = MIMETypes.JPEG; this.blob = baos.toByteArray(); + if (blob.length == 0) { + throw new IOException(); + } this.cropped = true; updateFileName(offsets); } catch (IOException e) { - logger.error("Image " + sFileName + " " + e.getLocalizedMessage()); + logger.error("Error. Image " + sFileName + " " + e.getLocalizedMessage()); e.printStackTrace(); } catch (RasterFormatException e ) { - logger.error("Image " + sFileName + " " + e.getLocalizedMessage()); + logger.error("Error. Image " + sFileName + " " + e.getLocalizedMessage()); e.printStackTrace(); } } @@ -289,13 +294,11 @@ public class BinaryGraphicsDocument implements OutputFile { ImageIO.write(resizedImage, "JPG", baos); this.blob = baos.toByteArray(); extractPPI(); - //logger.error(sFileName + " Vertical PPI " + this.verticalPPI); - //logger.error(sFileName + " Horizontal PPI " + this.horizontalPPI); } else { - logger.error("Image " + sFileName + " width = 0"); + logger.error("Error. Image " + sFileName + " width = 0"); } } catch (IOException e) { - logger.error("Image " + sFileName + " " + e.getLocalizedMessage()); + logger.error("Error. Image " + sFileName + " " + e.getLocalizedMessage()); e.printStackTrace(); } }