我有一个从像素图构建的 QImage,如下所示:
QPixmap fullPmap = topItem->pixmap();
fullPmap = fullPmap.copy(isec.toRect());
QImage chip = fullPmap.toImage();
这基本上与屏幕上的矩形相交以将图像裁剪为碎片尺寸。
我现在需要从芯片获取表示该数据的字符数组。
我怎样才能做到这一点?
我尝试过这样的事情:
unsigned char * data = chip.bits();
当我显示“数据”时,我得到一个完全扭曲的图像,与我的实际芯片完全不同。
fullPmap 是 RGB 图像(如果有的话)。我有一些代码用于将其转换为灰度:
QRgb col;
int gray;
for (int i = 0; i < chip.width(); ++i)
{
for (int j = 0; j < chip.height(); ++j)
{
col = chip.pixel(i, j);
gray = qGray(col);
chip.setPixel(i, j, qRgb(gray, gray, gray));
}
}
我不太喜欢,但这似乎是做这样的事情最简单的方法。
显示从bits()返回的数据如下所示:
imwidth = chip.width();
imheight = chip.height();
QImage *qi = new QImage(imwidth, imheight, QImage::Format_RGB32);
// #pragma omp parallel for
for (int i = 0 ; i < imheight ; i++)
for (int j = 0 ; j < imwidth ; j++)
{
qi->setPixel(j,i,qRgb(data[i*imwidth + j],data[i*imwidth + j],data[i*imwidth + j]));
}