【Qt】QImage使用总结

图像格式转换

由 RGB 格式转换成 BGR 格式

QImage::rgbSwapped()
返回一个QImage,其中所有像素的红色和蓝色组件的值被交换,有效地将RGB图像转换为BGR图像。

QImage image(fileName);
QImage bgr = image.rgbSwapped();

将彩色图转换成 灰度图

使用QImage::convertToFormat()函数,
参数选择QImage::Format_Grayscale8(需要Qt5.5以上版本才支持)。

QImage image(fileName);
QImage gray = image.convertToFormat(QImage::Format_Grayscale8);

图像保存

bool QImage::save(const QString &fileName, const char *format = Q_NULLPTR, int quality = -1) const

保存格式选择

参数format选择保存的格式,支持格式如下:
BMP(Windows Bitmap)
GIF(Graphic Interchange Format (optional))
JPG(Joint Photographic Experts Group)
JPEG(Joint Photographic Experts Group)
PNG(Portable Network Graphics)
PBM(Portable Bitmap)
PGM(Portable Graymap)
PPM(Portable Pixmap)
XBM(X11 Bitmap)
XPM(X11 Pixmap)

保存质量设置

quality必须在0到100或-1范围内。
指定0来获得小的压缩文件,100用于大的未压缩文件,和-1(默认)使用默认设置。

#include <QDir>
#include <QCoreApplication>

QString appDirPath = QCoreApplication::applicationDirPath();
QString imagePath = appDirPath + "/image.bmp";
imagePath = QDir::toNativeSeparators(imagePath);
image.save(imagePath,"BMP");

获取QImage中的数据

uchar *QImage::bits()
返回一个指向第一个像素数据的指针。这相当于函数scanLine(0)。
注意QImage使用隐式数据共享。这个函数执行共享像素数据的深度拷贝。

猜你喜欢

转载自blog.csdn.net/u010168781/article/details/80001467
今日推荐