简体   繁体   English

从缓冲区构建 QImage

[英]Build a QImage from a buffer

How I can build a QImage from a buffer for example?例如,我如何从缓冲区构建 QImage?
In this case I'm using a buffer of 3x3 with vale from 0 (black) to 255 (white).在这种情况下,我使用 3x3 的缓冲区,其值从 0(黑色)到 255(白色)。

0 255 0 0 255 0
255 0 255 255 0 255
0 255 0 0 255 0

and it is store into a unsigned char buffer[9] = {0, 255, 0, 255, 0, 255, 0, 255, 0};并将其存储到unsigned char buffer[9] = {0, 255, 0, 255, 0, 255, 0, 255, 0};

At the moment I'm trying this but doesn't work:目前我正在尝试这个但不起作用:

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

The constructor you're using...您正在使用的构造函数...

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

has the caveat...有警告...

data must be 32-bit aligned, and each scanline of data in the image must also be 32-bit aligned数据必须是32位对齐的,并且图像中数据的每条扫描线也必须是32位对齐的

Hence the QImage implementation expects the number of bytes in each scanline to be a multiple of 4 -- a condition not satisfied by your data buffer.因此, QImage实现期望每个扫描线中的字节数是 4 的倍数——数据缓冲区不满足这个条件。 Instead make use of the constructor that allows you to specify the bytes-per-scanline explicitly...而是使用允许您显式指定每扫描线字节数的构造函数...

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

So your code becomes...所以你的代码变成......

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