fix: png to jpg conversion

This commit is contained in:
Georgy Litvinov 2021-09-16 22:31:30 +02:00
parent 3191be6e53
commit 401bfc67ec

View file

@ -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 );
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();
}
}