QImage img( "Red.jpg" );
if ( false == img.isNull() )
{
QVector<QRgb> v = img.colorTable(); // returns a list of colors contained in the image's color table.
for ( QVector<QRgb>::const_iterator it = v.begin(), itE = v.end(); it != itE; ++it )
{
QColor clrCurrent( *it );
std::cout << "Red: " << clrCurrent.red()
<< " Green: " << clrCurrent.green()
<< " Blue: " << clrCurrent.blue()
<< " Alpha: " << clrCurrent.alpha()
<< std::endl;
}
}
然而,上面的这个例子确实返回了颜色表。颜色表不包括两次相同的颜色。它们将按出现顺序添加一次。
如果要获取每个像素的颜色,可以使用下一行:
for ( int row = 1; row < img.height() + 1; ++row )
for ( int col = 1; col < img.width() + 1; ++col )
{
QColor clrCurrent( img.pixel( row, col ) );
std::cout << "Pixel at [" << row << "," << col << "] contains color ("
<< clrCurrent.red() << ", "
<< clrCurrent.green() << ", "
<< clrCurrent.blue() << ", "
<< clrCurrent.alpha() << ")."
<< std::endl;
}