C++ QImage::将单声道格式化为.png和Qgraphicscene
我已经创建了一个格式为C++ QImage::将单声道格式化为.png和Qgraphicscene,c++,qt,qgraphicsview,qgraphicsscene,qimage,C++,Qt,Qgraphicsview,Qgraphicsscene,Qimage,我已经创建了一个格式为QImage::format_Mono的QImage。当我尝试通过qgraphicscene将图像显示到QGraphicsView时,视图保持不变。使用通过QPixmap::fromImage()函数生成的QPixmap将QImage加载到场景中。我还尝试使用save函数将QPixmap保存为PNG/JPG/BMP,但没有成功。基本代码结构如下所示: QGraphicsView *view = new QGraphicsView(); QGraphicsScene *sce
QImage::format_Mono
的QImage
。当我尝试通过qgraphicscene
将图像显示到QGraphicsView
时,视图保持不变。使用通过QPixmap::fromImage()
函数生成的QPixmap
将QImage
加载到场景中。我还尝试使用save函数将QPixmap
保存为PNG/JPG/BMP,但没有成功。基本代码结构如下所示:
QGraphicsView *view = new QGraphicsView();
QGraphicsScene *scene = new QGraphicsScene();
view.setScene(scene);
QImage img(size,QImage::Format_Mono);
QVector<QRgb> v;
v.append(Qt::color0); // I have tried using black and white
v.append(Qt::color1); // instead of color0 and 1 as well.
img.setColorTable(v);
// Do some stuff to populate the image using img.setPixel(c,r,bw)
// where bw is an index either 0 or 1 and c and r are within bounds
QPixmap p = QPixmap::fromImage(img);
p.save("mono.png");
scene->addPixmap(p);
// Code to display the view
QGraphicsView*view=newqgraphicsview();
qgraphicscene*场景=新的qgraphicscene();
视图.场景(场景);
QImage img(大小,QImage::格式);
qv矢量;
v、 追加(Qt::color0);//我试过用黑白两色
v、 附加(Qt::color1);//而不是颜色0和1。
img.可设置颜色(v);
//使用img.setPixel(c、r、bw)填充图像
//其中,bw是一个索引0或1,c和r在范围内
QPixmap p=QPixmap::fromImage(img);
p、 保存(“mono.png”);
场景->添加pixmap(p);
//显示视图的代码
如果我将图像改为QImage::Format_RGB888
,并用黑色或白色填充像素,则PNG/视图将适当显示
如何更新代码以在
QGraphicsView
中显示QImage
?错误在于Qt::GlobalColor
s(如Qt::white
或Qt::color0
)的类型为QColor
,而不是QRgb
。(QRgb
是无符号int的typedef)
您可以使用方法QColor::rgb()
将QColor
转换为QRgb
,或者使用全局方法QRgb(r,g,b)
直接创建QRgb
。下面是一个完整的工作示例,它显示(并保存为PNG)非常精确的图像,无论mono
是true
还是false
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QGraphicsView *view = new QGraphicsView();
QGraphicsScene *scene = new QGraphicsScene();
view->setScene(scene);
int W = 100;
int H = 100;
QImage img;
uint color0 = qRgb(255,0,0);
uint color1 = Qt::green.rgb();
bool mono = true;
if(mono)
{
img = QImage(QSize(W,H),QImage::Format_Mono);
QVector<QRgb> v; v << color0 << color1;
img.setColorTable(v);
for(int i=0; i<W; i++)
for(int j=0; j<H; j++)
{
uint index;
if(j-(j/10)*10 > 5)
index = 0;
else
index = 1;
img.setPixel(i,j,index);
}
}
else
{
img = QImage(QSize(W,H),QImage::Format_RGB888);
for(int i=0; i<W; i++)
for(int j=0; j<H; j++)
{
uint color;
if(j-(j/10)*10 > 5)
color = color0;
else
color = color1;
img.setPixel(i,j,color);
}
}
QPixmap p = QPixmap::fromImage(img);
p.save("mono.png");
scene->addPixmap(p);
view->show();
return app.exec();
}
#包括
#包括
#包括
int main(int argc,字符**argv)
{
QApplication应用程序(argc、argv);
QGraphicsView*视图=新的QGraphicsView();
qgraphicscene*场景=新的qgraphicscene();
查看->设置场景(场景);
int W=100;
int H=100;
QImage img;
uint color0=qRgb(255,0,0);
uint color1=Qt::green.rgb();
布尔单声道=真;
中频(单声道)
{
img=QImage(QSize(W,H),QImage::Format_Mono);
QVector v;v顺便说一句,我想view.setScene(scene);
只是SO中的一个输入错误,而不是在你的代码中。你是对的。我发现问题在于我使用的是Qt全局颜色,即Qt::white
和Qt::color0
。我必须使用QColor(Qt::white).rgb()
转换这些值,使其按预期运行。