如何将Pdf文件转换为位图图像

时间:2016-04-23 06:56:21

标签: android

我无法找到正确的源代码。我正在下载并添加源代码我得到的错误就像类找不到创建类。当然我添加了一些jar也在库中。请提供一些适当而有用的代码。

1 个答案:

答案 0 :(得分:1)

要将pdf转换为图片,请使用android-pdfview并使用以下代码

DecodeServiceBase decodeService = new DecodeServiceBase(new PdfContext());
decodeService.setContentResolver(mContext.getContentResolver());

// a bit long running
decodeService.open(Uri.fromFile(pdf));

int pageCount = decodeService.getPageCount();
for (int i = 0; i < pageCount; i++) {
PdfPage page = decodeService.getPage(i);
RectF rectF = new RectF(0, 0, 1, 1);

// do a fit center to 1920x1080
double scaleBy = Math.min(AndroidUtils.PHOTO_WIDTH_PIXELS / (double)     page.getWidth(), //
        AndroidUtils.PHOTO_HEIGHT_PIXELS / (double) page.getHeight());
int with = (int) (page.getWidth() * scaleBy);
int height = (int) (page.getHeight() * scaleBy);

// you can change these values as you to zoom in/out
// and even distort (scale without maintaining the aspect ratio)
// the resulting images

// Long running
Bitmap bitmap = page.renderBitmap(with, height, rectF);

try {
    File outputFile = new File(mOutputDir, System.currentTimeMillis() + FileUtils.DOT_JPEG);
    FileOutputStream outputStream = new FileOutputStream(outputFile);

    // a bit long running
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);

    outputStream.close();
} catch (IOException e) {
    LogWrapper.fatalError(e);
}
}

您应该在后台执行此任务,例如的AsyncTask