【发布时间】:2017-08-17 05:04:17
【问题描述】:
我使用com.itextpdf:itextg 将图像转换为 pdf 有点奇怪,它正在裁剪图像并且只占用了 25% 的图像。它在其中一部手机中运行良好,但在其他手机(主要是三星系列)中无限期地收割
下面是我的代码
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(FILE));
document.open();
ImageView iv = (ImageView) this.findViewById(R.id.imageViewNamed);
iv.buildDrawingCache(true);
Bitmap img = iv.getDrawingCache(true);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
img.compress(Bitmap.CompressFormat.PNG, 100, stream);
Image myImg = Image.getInstance(stream.toByteArray());
document.add(myImg);
stream.close();
document.close();
【问题讨论】:
-
此行为可能是由于制造商在视图中对绘图缓存的不同实现所致。也许某些三星设备存储图像的缓存较少,这会导致 PDF 内的图像被裁剪。我建议你不要信任drawingCache,而是创建自己的自定义实现将图像转换为PDF
-
嘿,谢谢@MatPag 那么我们该如何应对这种情况呢?如果我将相同的绘图缓存保存到 jpg 或 png 则图像很好。问题仅与pdf有关
-
你的图片来源是什么?为什么要从 imageview 中获取它?
-
因为我们在保存或将其放入 pdf 之前对图像进行了裁剪和编辑
-
您是否使用库来裁剪和编辑图像?
标签: android image pdf image-processing itext