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

相关文章

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

深入理解Go语言中二维切片的使用

《深入理解Go语言中二维切片的使用》本文深入讲解了Go语言中二维切片的概念与应用,用于表示矩阵、表格等二维数据结构,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录引言二维切片的基本概念定义创建二维切片二维切片的操作访问元素修改元素遍历二维切片二维切片的动态调整追加行动态

prometheus如何使用pushgateway监控网路丢包

《prometheus如何使用pushgateway监控网路丢包》:本文主要介绍prometheus如何使用pushgateway监控网路丢包问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录监控网路丢包脚本数据图表总结监控网路丢包脚本[root@gtcq-gt-monitor-prome

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

SpringBoot中如何使用Assert进行断言校验

《SpringBoot中如何使用Assert进行断言校验》Java提供了内置的assert机制,而Spring框架也提供了更强大的Assert工具类来帮助开发者进行参数校验和状态检查,下... 目录前言一、Java 原生assert简介1.1 使用方式1.2 示例代码1.3 优缺点分析二、Spring Fr

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected

C++ Log4cpp跨平台日志库的使用小结

《C++Log4cpp跨平台日志库的使用小结》Log4cpp是c++类库,本文详细介绍了C++日志库log4cpp的使用方法,及设置日志输出格式和优先级,具有一定的参考价值,感兴趣的可以了解一下... 目录一、介绍1. log4cpp的日志方式2.设置日志输出的格式3. 设置日志的输出优先级二、Window