将视图转换为位图时丢失质量

时间:2018-02-08 21:25:20

标签: android bitmap kotlin itext bytearrayoutputstream

我正在尝试将视图转换为图像,并使用iText将它们放入PDF文件中。但我失去了品质。它与Bitmap.compress()函数有关吗?

 val screen = getBitmapFromView(
        context.window.findViewById(id))
    try {
      val stream = ByteArrayOutputStream()
      screen.compress(Bitmap.CompressFormat.PNG, 100, stream)
      val image = Image.getInstance(stream.toByteArray())
      image.alignment = Image.ALIGN_CENTER
      document.add(image)
}

我的getBitmapFromView()功能:

private fun getBitmapFromView(view: View): Bitmap {
      val returnedBitmap = Bitmap.createBitmap(view.width, view.height,
          Bitmap.Config.ARGB_8888)
      val canvas = Canvas(returnedBitmap)
      val bgDrawable = view.background
      if (bgDrawable != null)
        bgDrawable.draw(canvas)
      else
        canvas.drawColor(Color.WHITE)
      view.draw(canvas)
      return returnedBitmap
    }

enter image description here enter image description here

编辑:添加了屏幕截图

0 个答案:

没有答案