重点:
1.QPixmap主要用于在界面上显示图像,它可以对图像进行缩放,可以加载BMP、JPG、PNG等格式的图片文件,然后在 OLabel组件上显示图像。
2.QImage可以读取BMP、JPG、PNG 等格式的图片件,存储图像中所有像素的颜色数据。QImage的接口函数可以实现图像的缩放、旋转、镜翻转等简单处理,可以转换颜色数据格式。因为QImage可以读写图像中每个像素的颜色数掘所以结合图像处理算法,我们可以对图像进行各种处理,例如调整亮度、调整对比度、模糊处理等。
QImage转换数据并采用QPixmap显示
- void MainWindow::on_btnFormatConvert_clicked(QString fileName)
- {//图像格式转换
- QImage m_image;
- m_image.load(fileName); //从当前文件重新载入
- int index=ui->comboFormat->currentIndex();
- if (index ==0)
- m_image.convertTo(QImage::Format_RGB16); //RGB565
- else if (index ==1)
- m_image.convertTo(QImage::Format_RGB888); //RGB888
- else if (index ==2)
- m_image.convertTo(QImage::Format_RGB32); //RGBx888
- else if (index ==3)
- // newImage = image.convertToFormat(QImage::Format_Grayscale8); //不改变原图
- // newImage = image.convertedTo(QImage::Format_Grayscale8); //不改变原图像
- m_image.convertTo(QImage::Format_Grayscale8); //8位灰度
- else if (index ==4)
- m_image.convertTo(QImage::Format_Grayscale16);//16位灰度
- else if (index ==5)
- m_image.convertTo(QImage::Format_Indexed8); //8位索引
- else
- return;
-
- QPixmap pixmap=QPixmap::fromImage(m_image); //刷新界面的图像显示
- ui->labPic->setPixmap(pixmap);
- }