2017-07-17 42 views
0

我正在獲取圖像行數據並將其保存在unsigned char數組中。接下來我試圖從這些數據中創建新的圖像文件。將原始數據保存爲新文件

如何獲得數據:

for(int i = 0; i < x; i++) { 
     for(int j = 0; j < y; j++) { 
      byteArray[i][j] = *((image.bits() + i) + j); 
     } 
    } 

其中x - >寬度圖像,Y - >圖像的高度。另外據我瞭解bits()返回指向第一個像素數據,所以與「我」和「j」我可以移動該指針。因爲我有255,255,120等數值(r,g,b,一個像素值)的數組。

如何創建新的形象:

void saveFile(unsigned char **array, const int &x, const int &y) { 
    QImage newImage(x, y, QImage::Format_ARGB32); 
    newImage.loadFromData((const unsigned char*)array, x * y, "JPG"); 
    bool success = newImage.save("przerobione", "JPG", -1); 

    if(success == true) { 
     qDebug() << "Zapisano!"; 
    } 
    else { 
     qDebug() << "nope"; 
    } 
} 

不幸的是,從不創建文件。我究竟做錯了什麼 ?

+0

您沒有假設以圖像原始數據保存到2維陣列。只需將其保存在普通數組中。然後使用其中一個[QImage構造函數](http://doc.qt.io/qt-5/qimage.html#QImage-6)傳遞數組(不是2維)。 –

回答

相關問題