使用PDF库在现有PDF文件上绘制小图像

时间:2018-10-03 12:32:44

标签: android svg bitmap pdf-generation png

我正在开发一个创建PDF文件的应用,我需要在其上绘制一些小图像。

问题是PDF上绘制的图像模糊并且质量很低。 我还尝试过在不缩放图像的情况下绘制图像,并且同样发生,它们很模糊。

这是我绘制一张图像的片段(我不使用iText)

Bitmap calendarBitmap = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.icon_200ppp_calendar);
pdfCanvas.drawBitmap(scaleToFitHeight(calendarBitmap, 16), xCoord, yCoord, null);

public Bitmap scaleToFitHeight(Bitmap b, int height) {
   float factor = height / (float) b.getHeight();
   return Bitmap.createScaledBitmap(b, (int) (b.getWidth() * factor), height, true);
}

我有PNG和SVG图像(转换为VectorDrawable),结果是相同的。

这是PNG: PNG file

这是通过Android Studio从SVG文件生成的XML:SVG converted to VectorDrawable

这些是结果:

Blurry images zoom in

Blurry images zoom out

1 个答案:

答案 0 :(得分:0)

您必须将图像的密度设置X倍。 例如:

Bitmap bm = new Bitmap(..);
bm.setDensity(bm.getDensity()*4);
// try with different X's: 2, 3, 4, 5, 6.

发生这种质量损失的原因是,当您写入PDF时,它将自动调整位图的大小以适合Canvas分辨率。同样,如果您调整图像大小,位图大小,则必须小心,如果您拥有50X50的图像,并且想要将其大小调整为50X10,它将使图像混乱。而是尝试将位图调整大小:

bm.getWidth()/4, 
bm.getHeight()/4

,依此类推。