如何将XImage保存为PNG格式的base64字符串?

发布于 2024-11-18 06:08:31 字数 99 浏览 3 评论 0原文

我正在 Ubuntu Linux 和 C++ 中进行开发。

我将桌面图像捕获到 XImage。

如何将XImage保存为PNG格式的base64字符串?

I'm developing in Ubuntu Linux and C++.

I captured the desktop image to XImage.

How to save XImage as base64 string in PNG format?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只为一人 2024-11-25 06:08:31

这是一个将 XImage 数据转换为 jpeg 图像的 C 函数:

void write_jpeg( FILE *outfile, int width, int height, unsigned char *rgb, int quality)
{
    struct jpeg_compress_struct cinfo;
    struct jpeg_error_mgr jerr;
    JSAMPROW scanline[1];
    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_compress(&cinfo);
    jpeg_stdio_dest(&cinfo, outfile);
    cinfo.image_width = width;
    cinfo.image_height = height;
    cinfo.input_components = 3;
    cinfo.in_color_space = JCS_RGB;
    jpeg_set_defaults(&cinfo);
    jpeg_set_quality(&cinfo, quality, TRUE);
    jpeg_start_compress(&cinfo, TRUE);
    while (cinfo.next_scanline < (unsigned int) height)
    {
        scanline[0] = rgb + 3 * width * cinfo.next_scanline;
        jpeg_write_scanlines(&cinfo, scanline, 1);
    }
    jpeg_finish_compress(&cinfo);
    jpeg_destroy_compress(&cinfo);
}

我相信您可以轻松地将其放入您的代码中。您只需要使用适当的库来保存 PNG 文件(而不是 jpeg 文件)。

Here is a C function to convert XImage data into a jpeg image :

void write_jpeg( FILE *outfile, int width, int height, unsigned char *rgb, int quality)
{
    struct jpeg_compress_struct cinfo;
    struct jpeg_error_mgr jerr;
    JSAMPROW scanline[1];
    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_compress(&cinfo);
    jpeg_stdio_dest(&cinfo, outfile);
    cinfo.image_width = width;
    cinfo.image_height = height;
    cinfo.input_components = 3;
    cinfo.in_color_space = JCS_RGB;
    jpeg_set_defaults(&cinfo);
    jpeg_set_quality(&cinfo, quality, TRUE);
    jpeg_start_compress(&cinfo, TRUE);
    while (cinfo.next_scanline < (unsigned int) height)
    {
        scanline[0] = rgb + 3 * width * cinfo.next_scanline;
        jpeg_write_scanlines(&cinfo, scanline, 1);
    }
    jpeg_finish_compress(&cinfo);
    jpeg_destroy_compress(&cinfo);
}

I am sure you can easily fit this into your code. You just need to use appropriate library to save a PNG file (instead of a jpeg file).

七秒鱼° 2024-11-25 06:08:31

我想说答案应该是使用 libpng。然而,在快速查看文档之后,我发现这个库的方式更多比需要的复杂(简单的事情应该很容易,困难的事情应该是可能的......在查看手册之后,似乎使用 libpng 的简单事情几乎是不可能的)。 PNG 格式有无数的选项,libpng 非常灵活且可插入,但显然当您只想做一些更简单的事情(例如保存单个不透明图像)时,没有简单的捷径。

我想我会倾向于以原始格式转储图像,然后使用 imagemagick (可能通过管道)。

另一方面,您是否考虑使用 Qt 而不是裸 X?捕获屏幕很容易,您可以以更易于管理的格式 (QImage) 获取屏幕。保存为几乎任何您喜欢的文件格式也很简单(但是 png 格式对 Qt 的 CPU 来说有点困难,因此如果您想进行实时视频捕获,则不建议这样做)。

I was about to say that the answer should be using libpng. However after a quick look at the documentation I find this library way much more complex than needed (easy things should be easy, hard things should be possible... after looking at the manual seems that easy things with libpng are instead close to impossible). PNG format has a gajillion options, libpng is extremely flexible and pluggable but apparently there is no easy shortcut when you just want to do something simpler (like saving a single opaque image).

I think I would be tempted to just dump the image in a raw format and then use imagemagick (possibly through a pipe).

On the other hand did you consider use Qt instead of bare X? Capturing the screen is easy and you can get it in a much more manageable format (QImage). Saving to almost any file format you like is also trivial (png format is however somewhat hard on CPU with Qt, so not recommended if you wanna do a live video capture).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文