圖像格式轉換
由 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使用隱式數據共享。這個函數執行共享像素數據的深度拷貝。