繁体   English   中英

从缓冲区构建 QImage

[英]Build a QImage from a buffer

例如,我如何从缓冲区构建 QImage?
在这种情况下,我使用 3x3 的缓冲区,其值从 0(黑色)到 255(白色)。

0 255 0
255 0 255
0 255 0

并将其存储到unsigned char buffer[9] = {0, 255, 0, 255, 0, 255, 0, 255, 0};

目前我正在尝试这个但不起作用:

QImage image{buffer, 3, 3, QImage::Format_Grayscale8};

您正在使用的构造函数...

QImage(uchar *data, int width, int height, QImage::Format format, QImageCleanupFunction cleanupFunction = nullptr, void *cleanupInfo = nullptr)

有警告...

数据必须是32位对齐的,并且图像中数据的每条扫描线也必须是32位对齐的

因此, QImage实现期望每个扫描线中的字节数是 4 的倍数——数据缓冲区不满足这个条件。 而是使用允许您显式指定每扫描线字节数的构造函数...

QImage(uchar *data, int width, int height, int bytesPerLine, QImage::Format format, QImageCleanupFunction cleanupFunction = nullptr, void *cleanupInfo = nullptr)

所以你的代码变成......

unsigned char buffer[9] = {0, 255, 0, 255, 0, 255, 0, 255, 0};
QImage image{buffer, 3, 3, 3, QImage::Format_Grayscale8};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM