将位图转换为PDF时的大文件大小

时间:2017-09-08 05:18:25

标签: android pdf bitmap kotlin

我正在将我的位图文件转换为PDF但是当我这样做时,我注意到文件大小增加了很多。在下面的示例中,它从3 MB变为8MB。关于我的代码有什么问题的任何建议?这似乎是对我的直接转换。

谢谢!

代码

                   Timber.d("Creating PDF")

                    val document = PdfDocument()

                    receipt.images.forEachIndexed { index, file ->

                        Timber.d("Picture #$index: ${file.absolutePath}")
                        Timber.d("Filesize: ${file.length() / 1000000}MB")
                        Timber.d("Adding picture to PDF ")

                        val originalBitmap = BitmapFactory.decodeFile(file.absolutePath)
                        Timber.d("Decoded bitmap size: ${originalBitmap.byteCount / 1000000}MB")

                        val pageInfo = PdfDocument.PageInfo.Builder(originalBitmap.width, originalBitmap.height, 1).create()
                        val page = document.startPage(pageInfo)
                        val canvas = page.canvas

                        canvas.drawBitmap(originalBitmap, 0f, 0f, null)
                        document.finishPage(page)
                    }


                    val file = File(cacheDir, "${receipt.name}.pdf")
                    Timber.d("Writing PDF to ${file.absolutePath}")
                    document.writeTo(FileOutputStream(file))
                    Timber.d("PDF file size: ${file.length() / 1000000}MB")

                    Timber.d("Closing PDF")
                    document.close()

logcat的

D/ListFilesActivity$sh: Creating PDF
D/ListFilesActivity$sh: Picture #0: /data/user/0/com.example.debug/files/2538cb35-d037-4cc0-9f13-2ae98c6d2493
D/ListFilesActivity$sh: Filesize: 3MB
D/ListFilesActivity$sh: Adding picture to PDF 
D/ListFilesActivity$sh: Decoded bitmap size: 31MB
D/ListFilesActivity$sh: Writing PDF to /data/user/0/com.example.debug/cache/File 20170907_154650.pdf
D/ListFilesActivity$sh: PDF file size: 8MB
D/ListFilesActivity$sh: Closing PDF
D/ListFilesActivity$sh: Sharing PDF

0 个答案:

没有答案