簡體   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