Android PDF到PNG质量不佳

时间:2015-03-17 20:26:21

标签: java android pdf png

我正在尝试将.pdf文件中的页面转换为.png。一切都有效,但质量相当高,即使我在压缩过程中将质量设置为100。有人能说明为什么它会给我这么糟糕的结果吗?

PDF到PNG

try {
        inputStream = new FileInputStream(file);

        long length = file.length();
        bytes = new byte[(int) length];
        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length && (numRead = inputStream.read(bytes, offset, bytes.length - offset)) >= 0) {
            offset += numRead;
        }
        ByteBuffer buffer = ByteBuffer.NEW(bytes);
        PDFFile pdf_file = new PDFFile(buffer);
        PDFPage page = pdf_file.getPage(1, true);
        RectF rect = new RectF(0, 0, (int) page.getBBox().width(),
                (int) page.getBBox().height());

        Bitmap image = page.getImage((int)rect.width(), (int)rect.height(), rect, true, true);
        root = new File("/storage/emulated/0/DCIM" + "/Pay Reports");
        if (!root.exists()) {
            root.mkdirs();
        }
        file = new File(root, "PayPeriod" + readFromFile("startPayPeriod.txt").substring(2) + "_" + readFromFile("endPayPeriod.txt").substring(2) + ".png");
        FileOutputStream os = new FileOutputStream(file);
        image.compress(Bitmap.CompressFormat.PNG, 100, os);
    }catch (Exception e) {
        e.printStackTrace();

0 个答案:

没有答案