我已经能够使用类似下面的方法在 Qt 中的标签中显示图像:
transformPixels(0,0,1,imheight,imwidth,1);//sets unsigned char** imageData
unsigned char* fullCharArray = new unsigned char[imheight * imwidth];
for (int i = 0 ; i < imheight ; i++)
for (int j = 0 ; j < imwidth ; j++)
fullCharArray[(i*imwidth)+j] = imageData[i][j];
QImage *qi = new QImage(fullCharArray, imwidth, imheight, QImage::Format_RGB32);
ui->viewLabel->setPixmap(QPixmap::fromImage(*qi,Qt::AutoColor));
所以fullCharArray是一个由二维数组imageData映射而来的unsigned char数组,换句话说就是imheight * imwidth字节。
问题是,标签中似乎只显示了我图片的一部分。图片很大。我想显示完整的图像,按比例缩小以适合标签,并保留纵横比。
此外,QImage 格式是我能找到的唯一一种格式,它似乎可以准确地表示我想要显示的图像,这是我应该期待的吗?我每个像素只使用一个字节(无符号字符 - 值从 0 到 255),看起来 RGB32 对于该数据类型没有多大意义,但其他的都没有显示任何远程正确的内容
编辑: 按照 dan gallaghers 的建议,我实现了这段代码:
QImage *qi = new QImage(fullCharArray, imwidth, imheight, QImage::Format_RGB32);
int labelWidth = ui->viewLabel->width();
int labelHeight = ui->viewLabel->height();
QImage small = qi->scaled(labelWidth, labelHeight,Qt::KeepAspectRatio);
ui->viewLabel->setPixmap(QPixmap::fromImage(small,Qt::AutoColor));
但这会导致我的程序“意外结束”,代码为 0
最佳答案
Qt 不直接支持灰度图像构造。您需要使用 8 位索引彩色图像:
QImage * qi = new QImage(imageData, imwidth, imheight, QImage::Format_Indexed8);
for(int i=0;i<256;++i) {
qi->setColor(i, qRgb(i,i,i));
}
关于c++ - 在 Qt 中显示和调整来自 QImage 的灰度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4727625/