Qt Windows和Android使用MuPDF预览PDF文件

2024-02-08 09:12

本文主要是介绍Qt Windows和Android使用MuPDF预览PDF文件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 1. Windows MuPDF编译
  • 2. Android MuPDF编译
  • 3. 引用 MuPDF 库
  • 4. 解析本地PDF文件


1. Windows MuPDF编译

使用如下命令将MuPDF的源码克隆到本地

git clone --recursive git://git.ghostscript.com/mupdf.git

直接用VS,打开 mupdf/platform/win32/mupdf.sln 工程文件,然后编译即可,我这边用的是VS2019 编译的x64的版本,编译中并没有报错。 编译完成后会生成 libmupdf.lib 库文件。


2. Android MuPDF编译

使用如下命令将MuPDF的源码克隆到本地

git clone --recursive git://git.ghostscript.com/mupdf-android-viewer.git

(1) 修改 mupdf-android-viewer/jni/libmupdf/platform/java 路径下的 Android.mk 文件,添加
LOCAL_SHORT_COMMANDS := true

...
LOCAL_LDFLAGS := -Wl,--gc-sections
LOCAL_LDFLAGS += $(MUPDF_EXTRA_LDFLAGS)LOCAL_SHORT_COMMANDS := true
include $(BUILD_SHARED_LIBRARY)

(2) 修改 mupdf-android-viewer/jni/libmupdf/platform/java 路径下的 Application.mk 文件,添加
APP_SHORT_COMMANDS := true

APP_SHORT_COMMANDS := trueifdef USE_TESSERACT
APP_STL := c++_static
endif

然后打开 AndroidStudio 直接构建即可,最后会生成 libmupdf_java.so 文件,如果找不到可以用everything找一下,我的生成目录是在 mupdf-android-viewer/app/build/intermediates/merged_native_libs/debug/out/lib 下


3. 引用 MuPDF 库

Qt的.pro文件中增加如下配置,分别添加Windows和Android库的头文件和库文件目录

win32 {# PDFINCLUDEPATH += $$PWD/../thirdLibs/MuPDF/win/includeCONFIG(debug, debug|release) {LIBS += -L$$PWD/../thirdLibs/MuPDF/win/libs/Debug -llibmupdf}CONFIG(release, debug|release) {LIBS += -L$$PWD/../thirdLibs/MuPDF/win/libs/Release -llibmupdf}
}android {INCLUDEPATH += $$PWD/../thirdLibs/MuPDF/android/includeLIBS += -L$$PWD/../thirdLibs/MuPDF/android/libs -lmupdf_java
}

4. 解析本地PDF文件

头文件

#ifndef MUPDFWRAPERCORE_H
#define MUPDFWRAPERCORE_H#include <QObject>
#include <QImage>struct fz_context;
struct fz_document;
struct fz_pixmap;class UTILS_EXPORT MuPDFWraperCore : public QObject
{Q_OBJECTpublic:MuPDFWraperCore(QObject* parent = nullptr);~MuPDFWraperCore();// 初始化上下文void initContext(void);// 加载PDF文件bool loadPdfFile(const QString& pdfPath);// 读取PDF某一页QImage loadPdfPage(int nPage, qreal zoom = 100, qreal rotate = 0);// 获取总页数int getTotalPage(void);private:int m_nTotalPage = 0;fz_context *m_pCtx = nullptr;fz_document *m_pDoc = nullptr;fz_pixmap *m_pPix = nullptr;
};#endif

cpp文件

#include "MuPDFWraperCore.h"
#include "mupdf/fitz.h"
#include <QDebug>MuPDFWraperCore::MuPDFWraperCore(QObject* parent):QObject(parent)
{}MuPDFWraperCore::~MuPDFWraperCore()
{if (m_pPix)fz_drop_pixmap(m_pCtx, m_pPix);if (m_pDoc)fz_drop_document(m_pCtx, m_pDoc);if (m_pCtx)fz_drop_context(m_pCtx);m_pPix = nullptr;m_pDoc = nullptr;m_pCtx = nullptr;
}// 初始化上下文
void MuPDFWraperCore::initContext(void)
{if (m_pCtx == nullptr)m_pCtx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED);if (!m_pCtx) {qInfo() << "Create PDF Context Error!";return;}/* Register the default file types to handle. */fz_try(m_pCtx)fz_register_document_handlers(m_pCtx);fz_catch(m_pCtx){
//        fz_report_error(m_pCtx);qInfo() << "cannot register document handlers";fz_drop_context(m_pCtx);m_pCtx = nullptr;return;}
}// 加载PDF文件
bool MuPDFWraperCore::loadPdfFile(const QString& pdfPath)
{if (m_pCtx == nullptr) {initContext();}if (m_pCtx == nullptr)return false;/* Open the document. */fz_try(m_pCtx)m_pDoc = fz_open_document(m_pCtx, pdfPath.toStdString().c_str());fz_catch(m_pCtx){
//        fz_report_error(m_pCtx);qInfo() << "cannot open document";fz_drop_context(m_pCtx);m_pCtx = nullptr;return false;}/* Count the number of pages. */fz_try(m_pCtx)m_nTotalPage = fz_count_pages(m_pCtx, m_pDoc);fz_catch(m_pCtx){
//        fz_report_error(m_pCtx);qInfo() << "cannot count number of pages";fz_drop_document(m_pCtx, m_pDoc);fz_drop_context(m_pCtx);m_pCtx = nullptr;m_pDoc = nullptr;return false;}return true;
}// 读取PDF某一页
QImage MuPDFWraperCore::loadPdfPage(int nPage, qreal zoom, qreal rotate)
{if (m_pCtx == nullptr || m_pDoc == nullptr)return QImage();if (nPage >= m_nTotalPage) {qInfo() << "Page Over Page Total Count";return QImage();}/* Compute a transformation matrix for the zoom and rotation desired. *//* The default resolution without scaling is 72 dpi. */fz_matrix ctm = fz_scale(zoom / 100, zoom / 100);ctm = fz_pre_rotate(ctm, rotate);/* Render page to an RGB pixmap. */if (m_pPix) {fz_drop_pixmap(m_pCtx, m_pPix);}fz_try(m_pCtx)m_pPix = fz_new_pixmap_from_page_number(m_pCtx, m_pDoc, nPage, ctm, fz_device_rgb(m_pCtx), 0);fz_catch(m_pCtx){
//        fz_report_error(m_pCtx);qInfo() << "cannot render page";fz_drop_document(m_pCtx, m_pDoc);fz_drop_context(m_pCtx);return QImage();}QImage image(m_pPix->w, m_pPix->h, QImage::Format_RGB888);for (int y = 0; y < m_pPix->h; ++y){unsigned char* p = &m_pPix->samples[y * m_pPix->stride];for (int x = 0; x < m_pPix->w; ++x){image.setPixel(x, y, qRgb(p[0], p[1], p[2]));p += m_pPix->n;}}return image/*QImage(m_pPix->samples, m_pPix->w, m_pPix->h, QImage::Format_RGB888)*/;
}// 获取总页数
int MuPDFWraperCore::getTotalPage(void)
{return m_nTotalPage;
}

上面的代码比较简单,基本操作API如下:

  • fz_new_context: 创建PDF上下文
  • fz_register_document_handlers: 注册要处理的默认文件类型
  • fz_open_document: 打开PDF文件
  • fz_count_pages: 获取PDF的总页数
  • fz_scale:获取缩放矩阵
  • fz_pre_rotate: 获取旋转矩阵
  • fz_new_pixmap_from_page_number:读取文档某一页并转化为图像

使用如下代码可将 fz_pixmap 转化为 QImage

QImage image(m_pPix->w, m_pPix->h, QImage::Format_RGB888);
for (int y = 0; y < m_pPix->h; ++y)
{unsigned char* p = &m_pPix->samples[y * m_pPix->stride];for (int x = 0; x < m_pPix->w; ++x){image.setPixel(x, y, qRgb(p[0], p[1], p[2]));p += m_pPix->n;}
}

最后直接渲染这个QImage就完成了PDF的预览 ^v^


效果截图:
Windows-PDF预览:
Windows-PD预览
Android-PDF预览:
Android-PDF预览

这篇关于Qt Windows和Android使用MuPDF预览PDF文件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/690571

相关文章

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

nginx启动命令和默认配置文件的使用

《nginx启动命令和默认配置文件的使用》:本文主要介绍nginx启动命令和默认配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录常见命令nginx.conf配置文件location匹配规则图片服务器总结常见命令# 默认配置文件启动./nginx

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

Linux脚本(shell)的使用方式

《Linux脚本(shell)的使用方式》:本文主要介绍Linux脚本(shell)的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录概述语法详解数学运算表达式Shell变量变量分类环境变量Shell内部变量自定义变量:定义、赋值自定义变量:引用、修改、删