簡體   English   中英

從QBytearray創建一個QImage

[英]Creating a QImage from QBytearray

我正在嘗試從保存為二進制文件中16位整數的數據構造QImage。 我可以很好地加載數據,但是當我使用QImage :: loadFromData(QBytearray ba)函數(返回false)時,程序失敗,如下所示:

QBytearray frame;
QImage pic = QImage(256, 256, QImage::Format_RGB888);

for(int i = 0; i < height; i++) {
    for(int j = 0; j < width; j++) {
        // Access value of pixel at each location
        datum = store[i][j];

        for(int c = 0; c < 3; c++) {
            // Calculate colour at given pixel
            col = (255.0f * ((float)datum - (float)min) / ((float)(max - min)));
            // Assign colour value to the pixel
            frame[c+3*j+3*i*width] = ((unsigned char)col);
        }
    }
}

pic.loadFromData(frame);

我從以前編寫的Java代碼中重新調整了它的用途,該代碼可以按預期運行(完全相同的數據):

BufferedImage image = = new BufferedImage(256, 256, BufferedImage.TYPE_3BYTE_BGR);

byte[] data = image.getRaster().getDataBuffer();

for (j=0; j<height; j++) {
    for (i=0; i<width; i++) {
        //Find value of the pixels at the location
        datum=data[j][i];
        for (c=0; c<3; c++) {
            //Calculate the colour at the given pixel
            col=(255.0f*((float)datum-(float)min)/((float)(max-min)));
            //Assign the colour value to the pixel
            data[c+3*i+3*j*width] = (byte)col;
        } 
    }
}

有人可以幫我看看我在哪里弄錯了嗎? 我已經迷戀了好幾天,全都沒主意。

好的,假設您實際上是在嘗試為單個像素設置RGB值,在閱讀QImage詳細信息之后 ,我看到您可以使用以下方法進行操作:

value = qRgb(189, 149, 39); // 0xffbd9527
image.setPixel(1, 1, value);

因此,類似:

QImage pic = QImage(256, 256, QImage::Format_RGB888);
QRgb value;
int r,b,g;

for(int i = 0; i < height; i++) {
    for(int j = 0; j < width; j++) {
        // Access value of pixel at each location
        datum = store[i][j];
        //I get really confused what is going on here... you don't seem to be actually using `c` for the calculation?
        for(int c = 0; c < 3; c++) { //let's just pretend you set the ints r,b,g in here somewhere
            // Calculate colour at given pixel
            col = (255.0f * ((float)datum - (float)min) / ((float)(max - min)));
        }
        // Assign colour value to the pixel
        value = qRgb(r, g, b);
        pic.setPixel(i, j, value);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM