[英]Scaled image blurry in PDFBox
I'm trying to scaling an image with size = 2496 x 3512 into a PDF document. 我正在尝试将尺寸= 2496 x 3512的图像缩放为PDF文档。 I'm using PDFBox to generate it but the scaled image ends up blurred.
我正在使用PDFBox生成它,但缩放的图像最终模糊。
Here are some snippets: 以下是一些片段:
PDF Page size (A4) returned by page.findMediaBox().createDimension(): java.awt.Dimension[width=612,height=792] PDF页面大小(A4)由page.findMediaBox()返回.createDimension():java.awt.Dimension [width = 612,height = 792]
Then I calculate the scaled dimension based on the Page size vs. Image size which returns: java.awt.Dimension[width=562,height=792] I use the code below in order to calculate the scaled dimension: 然后我根据页面大小与图像大小计算缩放维度,返回:java.awt.Dimension [width = 562,height = 792]我使用下面的代码来计算缩放维度:
public static Dimension getScaledDimension(Dimension imgSize, Dimension boundary) { int original_width = imgSize.width; int original_height = imgSize.height; int bound_width = boundary.width; int bound_height = boundary.height; int new_width = original_width; int new_height = original_height; // first check if we need to scale width if (original_width > bound_width) { //scale width to fit new_width = bound_width; //scale height to maintain aspect ratio new_height = (new_width * original_height) / original_width; } // then check if we need to scale even with the new height if (new_height > bound_height) { //scale height to fit instead new_height = bound_height; //scale width to maintain aspect ratio new_width = (new_height * original_width) / original_height; } return new Dimension(new_width, new_height); }
And to actually perform the image scaling I'm using Image Scalr API: 为了实际执行图像缩放我正在使用Image Scalr API:
BufferedImage newImg = Scalr.resize(img, Scalr.Method.ULTRA_QUALITY, Scalr.Mode.FIT_EXACT, scaledWidth, scaledHeight, Scalr.OP_ANTIALIAS);
My question is what am I doing wrong? 我的问题是我做错了什么? A big image shouldn't be blurred when scaled to a smaller size.
缩放到较小尺寸时,不应模糊大图像。 Is this something related to the PDF page resolution/size?
这是与PDF页面分辨率/大小相关的内容吗?
Thank you, 谢谢,
Gyo 木桥
Ok, I found a way to add images without losing the quality. 好的,我找到了一种在不损失质量的情况下添加图像的方法。
Actually to make the image not be blurred I let PDFBox to resize the image by giving it the desired size. 实际上,为了使图像不被模糊,我让PDFBox通过给出所需的大小来调整图像的大小。 Like the code below:
像下面的代码:
PDXObjectImage ximage = new PDJpeg(doc, new FileInputStream(new File("/usr/gyo/my_large_image.jpg")), 1.0f);
PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, false);
Dimension scaledDim = getScaledDimension(new Dimension(ximage.getWidth(), ximage.getHeight()), page.getMediaBox().createDimension());
contentStream.drawXObject(ximage, 1, 1, scaledDim.width, scaledDim.height);
contentStream.close();
Thank you, 谢谢,
Gyo 木桥
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.