如何在 Android 中将 PDF 页面转换为图像? [英] How to convert a PDF page to an image in Android?
问题描述
我需要做的就是获取(本地保存的)PDF 文档
并将其中的一个或所有页面转换为图像格式,例如 JPG 或 PNG.>
我尝试了很多 PDF 渲染/查看解决方案,例如 APV PDF Viewer、APDFViewer、droidreader、android-pdf、MuPdf 和许多其他人,但到目前为止无法弄清楚如何将 pdf 页面转换为图像?.
另外,我宁愿有一个 PDF 到图像转换器,而不是我需要编辑以将 PDF 转换为图像的 PDF 渲染器.
要支持 API 8 及更高版本,请遵循:
使用此库:android-pdfview 和以下代码,您可以可靠地转换 PDF将页面转换为图像(JPG、PNG):
DecodeServiceBase decodeService = new DecodeServiceBase(new PdfContext());decodeService.setContentResolver(mContext.getContentResolver());//运行时间有点长decodeService.open(Uri.fromFile(pdf));int pageCount = decodeService.getPageCount();for (int i = 0; i < pageCount; i++) {PdfPage 页面 = decodeService.getPage(i);RectF rectF = 新的 RectF(0, 0, 1, 1);//做一个适合 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);//您可以在放大/缩小时更改这些值//甚至扭曲(缩放而不保持纵横比)//结果图像//长时间运行位图 bitmap = page.renderBitmap(with, height, rectF);尝试 {File outputFile = new File(mOutputDir, System.currentTimeMillis() + FileUtils.DOT_JPEG);FileOutputStream outputStream = new FileOutputStream(outputFile);//运行时间有点长bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);outputStream.close();} catch (IOException e) {LogWrapper.fatalError(e);}}
你应该在后台完成这项工作,即通过使用 AsyncTask
或类似的东西,因为很多方法需要计算或 IO 时间(我在评论中标记了它们).
All I need to do is take a (locally saved) PDF-document
and convert one or all of it's pages to image format like JPG or PNG.
I've tried lot's of PDF Rendering/viewing solutions like APV PDF Viewer, APDFViewer, droidreader, android-pdf, MuPdf and many others but couldn't figure it out so far that how to convert a pdf-page into image?.
EDIT: Also I'd rather have a PDF to image converter than a PDF renderer that I need to edit to convert PDF to image.
To support API 8 and above, follow:
Using this library: android-pdfview and the following code, you can reliably convert the PDF pages into images (JPG, PNG):
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);
}
}
You should do this work in the background i.e. by using an AsyncTask
or something similar as quite a few methods take computation or IO time (I have marked them in comments).
这篇关于如何在 Android 中将 PDF 页面转换为图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!