我有一个QImage,类似于以下方式从一个pixmap构建:
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]));
}