我正在尝试将使用 cameraa 拍摄的图像转换为 A4 PDF
// Read image width and height
float imageWidth = bitmap.getWidth();
float imageHeight = bitmap.getHeight();
// Read page width and height ignoring the margins
float pageWidth = PDRectangle.A4.getWidth() - (margin_left_right * 2);
float pageHeight = PDRectangle.A4.getHeight() - (margin_top_bottom * 2);
Log.d("Image Resize", String.valueOf(imageWidth) + " x " + String.valueOf(imageHeight));
Log.d("Image Resize", String.valueOf(pageWidth) + " x " + String.valueOf(pageHeight));
# Image: 4160.0 x 3120.0
# Page: 575.27563 x 821.8898
Log.d("Image Resize", String.valueOf(requiredRatio));
Log.d("Image Resize", String.valueOf(newImageWidth) + " x " + String.valueOf(newImageHeight));
# Required ratio: 0.13828741
# After scaling: 575 x 431
问题是大小为 4160 x 3120
的图像,在调整为 575 x 431
后,看起来非常像素化。
PDF 通常如何解决这个问题?在使用 contentStream.drawImage()
编写时,有没有办法在 600 dpi
下显示 4160 x 3120
?
这question还支持这样一个事实,如果在不调整大小等于图像高度和宽度的页面上写入,OP 只能看到 15% 的图像,应该如何在不损失质量的情况下按比例缩小?
最佳答案
有一个重载的 drawImage()
方法可以做到这一点
contentStream.drawImage(PDImageXObject, float x, float y)
contentStream.drawImage(PDImageXObject, float x, float y, float height, float width)
通过在 contentStream.drawImage()
上指定显式宽度和高度,可以实现这一点。例如,如果您想将图像缩小 4 倍但仍保持相同的分辨率,可以使用
scaleDownRatio = 0.25;
pageSize = PDRectangle.A4;
# this will draw image at bottom left corner
PDImageXObject pageImageXObject = LosslessFactory.createFromImage(document, bitmap);
contentStream.drawImage(pageImageXObject, 0, 0, bitmap.getWidth() * ratio, bitmap.getHeight() * ratio)
# to center it you can use something like
contentStream.drawImage(pageImageXObject, (pageSize.getWidth() - bitmap.getWidth() * ratio) / 2, (pageSize.getHeight() - bitmap.getHeight() * ratio) / 2, bitmap.getWidth() * ratio, bitmap.getHeight() * ratio)
关于android - PDFBox:将高清图像写入 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47993963/