gpt4 book ai didi

c++ - 在 C++ 中使用 ImageMagick 将图像缓冲区转换为 pdf

转载 作者:行者123 更新时间:2023-11-28 01:12:02 25 4
gpt4 key购买 nike

我已经下载了 ImageMagick 的 dll,想知道是否有人知道一些示例代码来完成一个简单的任务:

我已经用 C++ 生成了一个图像,并且有 RGB 格式的缓冲区。在通过 TCP 套接字发送之前,我需要将其转换为 PDF 格式(无需写入文件)。

使用 ImageMagick(或任何其他库)是否可行?

最佳答案

假设您正在使用 Magick++ Magick::Image 有一个可以从内存中的 blob 创建图像的构造函数和一个可以写入 PDF 的 write() 方法(以及一堆其他格式)到内存中的 blob。

请注意,在我的 Linux 机器上,ImageMagick 似乎在运行期间创建了一些临时文件。我不确定是否满足您的要求,但它可能是可配置的。

您可能会从这个代码片段中得到一个好主意:

#include <iostream>
#include <Magick++.h>

using namespace std;
using namespace Magick;

static char imageData[] = {
/* ... */
};

int main(int argc, char** argv)
{
/* Initialize the library */
InitializeMagick(*argv);

/* Instantiate an image from RGB data */
Image image(4, // Width
14, // Height
"RGB", // Color components ordering
CharPixel, // Components storage type
imageData);// Image data

/* Write pdf in memory */
Blob b;
image.write(&b, string("pdf"));

/* write pdf data to cout */
/* it should be easy to send it over a socket instead */
cout.write(static_cast<const char*>(b.data()), b.length());

return 0;
}

编辑:我应该补充一点,在 Windows 上将二进制数据写入 cout 会导致问题,除非您将输出流切换为二进制模式。上面的代码只是一个简短的示例,所以我忽略了它。

关于c++ - 在 C++ 中使用 ImageMagick 将图像缓冲区转换为 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2436217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com