鱼C论坛

 找回密码
 立即注册
查看: 31|回复: 3

矢量文件转图片问题

[复制链接]
发表于 8 小时前 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
在C++ MFC中,我想将 *.ps、*.pdf、*.eps等矢量格式的文件快速转为指定分辨率和尺寸的高质量Mat图像(其中PDF文件会内嵌部份字体),请帮我写一段代码。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 8 小时前 | 显示全部楼层
在C++ MFC中将矢量文件转换为Mat图像,可以使用Ghostscript库进行处理。以下是示例代码:


  1. #include <opencv2/opencv.hpp>
  2. #include <windows.h>

  3. // 使用Ghostscript转换矢量文件到图像
  4. cv::Mat VectorToMat(const CString& inputFile, int dpi = 300, int width = 0, int height = 0)
  5. {
  6.     CString tempFile = _T("temp_output.png");
  7.     CString gsCommand;
  8.    
  9.     // 构建Ghostscript命令
  10.     if(width > 0 && height > 0)
  11.     {
  12.         gsCommand.Format(_T("gswin64c -dNOPAUSE -dBATCH -dSAFER -sDEVICE=png16m -r%d -g%dx%d -sOutputFile="%s" "%s""),
  13.             dpi, width, height, tempFile, inputFile);
  14.     }
  15.     else
  16.     {
  17.         gsCommand.Format(_T("gswin64c -dNOPAUSE -dBATCH -dSAFER -sDEVICE=png16m -r%d -sOutputFile="%s" "%s""),
  18.             dpi, tempFile, inputFile);
  19.     }
  20.    
  21.     // 执行Ghostscript命令
  22.     STARTUPINFO si = {sizeof(si)};
  23.     PROCESS_INFORMATION pi;
  24.     CreateProcess(NULL, gsCommand.GetBuffer(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
  25.    
  26.     WaitForSingleObject(pi.hProcess, INFINITE);
  27.     CloseHandle(pi.hProcess);
  28.     CloseHandle(pi.hThread);
  29.    
  30.     // 读取生成的图像文件
  31.     cv::Mat result = cv::imread(CStringA(tempFile).GetString());
  32.    
  33.     // 删除临时文件
  34.     DeleteFile(tempFile);
  35.    
  36.     return result;
  37. }

  38. // 使用示例
  39. void ConvertExample()
  40. {
  41.     // 转换PDF文件为300DPI的1920x1080图像
  42.     cv::Mat image = VectorToMat(_T("input.pdf"), 300, 1920, 1080);
  43.    
  44.     if(!image.empty())
  45.     {
  46.         // 处理图像...
  47.         cv::imshow("Result", image);
  48.         cv::waitKey(0);
  49.     }
  50. }
复制代码


使用前需要:

1. 安装Ghostscript(确保 gswin64c.exe 在系统路径中)
2. 配置OpenCV库
3. 包含必要的头文件

主要参数说明:
- dpi:输出图像分辨率
- width/height:输出图像尺寸(为0时保持原比例)
- 支持格式:PS、PDF、EPS等Ghostscript支持的矢量格式

注意:Ghostscript会自动处理嵌入字体,确保输出图像中的文字正确显示。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 7 小时前 | 显示全部楼层
FishC 发表于 2025-9-29 08:02
在C++ MFC中将矢量文件转换为Mat图像,可以使用Ghostscript库进行处理。以下是示例代码:

有没有不安装Ghostscript的转换方法?因为我看很多图像RIP软件是在没安装Ghostscript的前提下就将各种矢量文件转换成图像了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 7 小时前 | 显示全部楼层
cxnet 发表于 2025-09-29 08:15
FishC 发表于 2025-9-29 08:02
在C++ MFC中将矢量文 ...


可以使用Ghostscript DLL直接调用,避免安装完整软件。以下是使用gsdll32.dll的方案:


  1. #include <windows.h>
  2. #include "ierrors.h"
  3. #include "iapi.h"
  4. #include <opencv2/opencv.hpp>

  5. // 定义Ghostscript函数指针
  6. typedef int (GSDLLAPI *PGSEXECUTE)(void *instance, const char *command);
  7. typedef int (GSDLLAPI *PGSNEW)(void **pinstance, void *caller_handle);  
  8. typedef int (GSDLLAPI *PGSDELETE)(void *instance);
  9. typedef int (GSDLLAPI *PGSSETARGS)(void *instance, int argc, char **argv);

  10. class CVectorConverter {
  11. private:
  12.     HMODULE m_hGsDll;
  13.     void* m_gsInstance;
  14.     PGSNEW m_pgsNew;
  15.     PGSDELETE m_pgsDelete;
  16.     PGSEXECUTE m_pgsExecute;
  17.     PGSSETARGS m_pgsSetArgs;

  18. public:
  19.     CVectorConverter() : m_hGsDll(NULL), m_gsInstance(NULL) {}
  20.    
  21.     ~CVectorConverter() {
  22.         Cleanup();
  23.     }
  24.    
  25.     bool Initialize(const CString& dllPath = _T("gsdll32.dll")) {
  26.         m_hGsDll = LoadLibrary(dllPath);
  27.         if (!m_hGsDll) return false;
  28.         
  29.         m_pgsNew = (PGSNEW)GetProcAddress(m_hGsDll, "gsapi_new_instance");
  30.         m_pgsDelete = (PGSDELETE)GetProcAddress(m_hGsDll, "gsapi_delete_instance");
  31.         m_pgsExecute = (PGSEXECUTE)GetProcAddress(m_hGsDll, "gsapi_execute");
  32.         m_pgsSetArgs = (PGSSETARGS)GetProcAddress(m_hGsDll, "gsapi_set_args");
  33.         
  34.         if (!m_pgsNew || !m_pgsDelete || !m_pgsExecute) {
  35.             FreeLibrary(m_hGsDll);
  36.             return false;
  37.         }
  38.         
  39.         return (m_pgsNew(&m_gsInstance, NULL) == 0);
  40.     }
  41.    
  42.     cv::Mat ConvertToImage(const CString& inputFile, int dpi = 300,
  43.                           int width = 0, int height = 0) {
  44.         if (!m_gsInstance) return cv::Mat();
  45.         
  46.         CString tempFile = _T("temp_output.png");
  47.         CStringA tempFileA(tempFile);
  48.         CStringA inputFileA(inputFile);
  49.         
  50.         // 构建Ghostscript参数
  51.         const char* args[] = {
  52.             "gswin32c",
  53.             "-dNOPAUSE",
  54.             "-dBATCH",
  55.             "-dSAFER",
  56.             "-sDEVICE=png16m",
  57.             CStringA().Format("-r%d", dpi),
  58.             width > 0 && height > 0 ?
  59.                 CStringA().Format("-g%dx%d", width, height) : "-dUseCropBox",
  60.             CStringA().Format("-sOutputFile=%s", tempFileA.GetString()),
  61.             inputFileA.GetString(),
  62.             NULL
  63.         };
  64.         
  65.         int argc = 8; // 参数数量
  66.         if (m_pgsSetArgs(m_gsInstance, argc, (char**)args) == 0) {
  67.             if (m_pgsExecute(m_gsInstance) == 0) {
  68.                 cv::Mat result = cv::imread(tempFileA.GetString());
  69.                 DeleteFile(tempFile);
  70.                 return result;
  71.             }
  72.         }
  73.         
  74.         return cv::Mat();
  75.     }
  76.    
  77.     void Cleanup() {
  78.         if (m_gsInstance && m_pgsDelete) {
  79.             m_pgsDelete(m_gsInstance);
  80.             m_gsInstance = NULL;
  81.         }
  82.         if (m_hGsDll) {
  83.             FreeLibrary(m_hGsDll);
  84.             m_hGsDll = NULL;
  85.         }
  86.     }
  87. };

  88. // 使用示例
  89. void ConvertVectorFile() {
  90.     CVectorConverter converter;
  91.     if (converter.Initialize(_T("gsdll32.dll"))) {
  92.         cv::Mat image = converter.ConvertToImage(_T("input.pdf"), 300, 1920, 1080);
  93.         if (!image.empty()) {
  94.             // 处理图像...
  95.         }
  96.     }
  97. }
复制代码


使用步骤:

1. 从Ghostscript官网下载DLL文件包
2. 将以下文件放在可执行文件目录:
  • gsdll32.dll (或gsdll64.dll)
  • gsdll32.lib
  • gs文件夹(包含字体和资源)


3. 在项目中包含头文件:
ierrors.hiapi.h

优点:
  • 无需安装Ghostscript
  • 程序可独立分发
  • 转换速度快
  • 支持所有Ghostscript支持的格式


注意事项:
  • 确保DLL版本与项目架构匹配(32/64位)
  • 资源文件路径需要正确配置
  • 多线程环境下需要适当的同步


以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-9-29 16:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表