Android PdfDocument.Page - 图像大小的问题 [英] Android PdfDocument.Page - Problems with image size
问题描述
在绘制到PdfDocument.Page时,我遇到了图像变大的问题。
该应用针对运行Android 4.4.4(API级别19)的设备。
I am experiencing problems with images being larger when drawing onto a PdfDocument.Page. The app is targeted for a device running Android 4.4.4 (API level 19).
我正在生成pdf文档并添加如下图像:
I am generating a pdf document and adding an image as follows:
PdfDocument document = new PdfDocument();
//Create an A4 sized page 595 x 842 in Postscript points.
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(595, 842, 1).create();
PdfDocument.Page page = document.startPage(pageInfo);
Canvas canvas = page.getCanvas();
Bitmap logo = BitmapFactory.decodeResource(context.getResources(), R.drawable.pdf_logo);
Log.i(Consts.LOG_TAG, "pdf canvas density = " + canvas.getDensity());
Log.i(Consts.LOG_TAG, "pdf UOD logo density = " + logo.getDensity());
canvas.drawBitmap(logo, 50, 50, null);
document.finishPage(page);
/*
document is written out to a FileOuputStream().
Details omitted.
*/
文件我写得正确,我使用Canvas.drawText()方法呈现的其他文本看起来很好。
但是,在pdf文档中绘制的图像总是太大。
我检查过图像和画布的密度是否相同。
图像(pdf_logo.jpg)是:
- 160 dpi
- 160 x 160分辨率的物理像素。
- 所以是1英寸x 1英寸大小。
上述代码中的日志也返回以下内容:
04-22 11:55: 50.607:I / App(7856):pdf canvas density = 160
04-22 11:55:50.607:I / App(7856):pdf UOD logo密度= 160
这表明图像dpi也与Canvas密度相匹配。
但是,当图像被绘制到pdf文档上时,图像远大于1英寸x 1英寸。它的尺寸大约是2英寸x 2英寸的两倍。
我怀疑Android使用了图像的物理分辨率(160 x 160)并将其转换为PdfDocument.Page大小单位,Postscript点为1/72英寸。所以160个Postscript点相当于2.2英寸〜原始图像尺寸的两倍(1 x 1英寸)。
如果我将图像的大小减半,那么我会得到像素化,因为它实际上会将0.5 x 0.5英寸的图像绘制成1 x 1英寸的空间。
我想知道如何在PdfDocument.Page上绘制图像作为实际尺寸,而不会出现这些缩放/尺寸问题。
The document is written out correctly, and other text I have rendered using Canvas.drawText() method looks fine.
However, the image drawn is always too big in the pdf document.
I have checked the density of both the image and canvas are the same.
The image (pdf_logo.jpg) is:
- 160 dpi
- 160 x 160 physical pixels in resolution.
- So is 1 inch x 1 inch in size.
The logs in the above code also returned the following:
04-22 11:55:50.607: I/App(7856): pdf canvas density = 160
04-22 11:55:50.607: I/App(7856): pdf UOD logo density = 160
This shows that the image dpi matches the Canvas density as well. However, when the image is drawn onto the pdf document, the image is much larger than 1 inch x 1 inch. It is about double the size , just over 2 inch x 2 inch.
My suspicion is that Android has used the physical resolution of the image (160 x 160) and converted it into the PdfDocument.Page size units, Postscript points which are 1/72 inch. So 160 Postscript points equates to 2.2 inches ~ double the size of the original image (1 x 1 inch).
If I halve the size of the image then I get pixelation, as it will effectively be drawing an 0.5 x 0.5 inch image into a 1 x 1 inch space.
I would like to know how I can draw an image as actual size on a PdfDocument.Page, without having these scaling/size issues.
推荐答案
根据要求,我发现的解决方案是将密度设置为高于应有的值。
将位图密度设置为 DENSITY_XHIGH 而不是 DENSITY_HIGH 为我提供了相当大的渲染位图。位图更接近于生成pdf时我期望的大小。
As requested, the solution I found was to set the density to a value higher than what it should be. Setting the bitmap density to DENSITY_XHIGH instead of DENSITY_HIGH improved the rendered bitmap considerably for me. The bitmap is closer to the size I would expect when the pdf is generated.
这篇关于Android PdfDocument.Page - 图像大小的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!