[英]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.