Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从缓冲区生成QImage_C++_Image_Qt_Buffer_Qimage - Fatal编程技术网

C++ 从缓冲区生成QImage

C++ 从缓冲区生成QImage,c++,image,qt,buffer,qimage,C++,Image,Qt,Buffer,Qimage,例如,如何从缓冲区构建QImage? 在这种情况下,我使用3x3的缓冲区,其值从0(黑色)到255(白色)。 0 255 0 255 0 255 0 255 0 并将其存储到无符号字符缓冲区[9]={0,255,0,255,0,255,0} 目前,我正在尝试这个方法,但不起作用: QImage image{buffer, 3, 3, QImage::Format_Grayscale8}; 您正在使用的构造函数 QImage(uchar *data, int width, int height,

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

0 255 0
255 0 255
0 255 0

并将其存储到
无符号字符缓冲区[9]={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};

您正在使用的构造函数

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};