1.图像数据读取/保存
QImage类,可以通过文件或者数据流获取图像数据。
通过文件读取:
QImage::QImage(const QString &fileName, const char *format = Q_NULLPTR)
支持以下格式:
Format | Description | Qt’s support |
---|---|---|
BMP | Windows Bitmap | Read/write |
GIF | Graphic Interchange Format (optional) | Read |
JPG | Joint Photographic Experts Group | Read/write |
JPEG | Joint Photographic Experts Group | Read/write |
PNG | Portable Network Graphics | Read/write |
PBM | Portable Bitmap | Read |
PGM | Portable Graymap | Read |
PPM | Portable Pixmap | Read/write |
XBM | X11 Bitmap | Read/write |
XPM | X11 Pixmap | Read/write |
通过数据流读取:
QImage::QImage(uchar *data, int width, int height, Format format, QImageCleanupFunction cleanupFunction = Q_NULLPTR, void *cleanupInfo = Q_NULLPTR) //连续内存的读写版本
QImage::QImage(const uchar *data, int width, int height, Format format, QImageCleanupFunction cleanupFunction = Q_NULLPTR, void *cleanupInfo = Q_NULLPTR) //连续内存的只读版本
QImage::QImage(uchar *data, int width, int height, int bytesPerLine, Format format, QImageCleanupFunction cleanupFunction = Q_NULLPTR, void *cleanupInfo = Q_NULLPTR) //非连续内存的读写版本
QImage::QImage(const uchar *data, int width, int height, int bytesPerLine, Format format, QImageCleanupFunction cleanupFunction = Q_NULLPTR, void *cleanupInfo = Q_NULLPTR) //非连续内存的只读版本
这里的数据流指的是图像数据以二进制流的形式存放在内存中。
注意,QImage只支持8位深的单通道灰度图像,此时需要设置colortabel或者将单通道转3通到,对于彩色图像则可以直接创建。
QImage内部数据按照行4字节对齐,即图像一行的字节数必须整除4,如果图像实际宽度w能整除4,则可以采用连续内存的构造函数创建QImage,如果图像实际宽度不能整除4,则必须采用非连续内存的构造函数创建QImage,此时需要指定每行的实际字节数
示例:对于w* h=1000 * 1000的图像,由于1000/4=250,可以采用连续内存的构造函数
//imgData的格式是RGB888
QImage qimage(imgData,1000,1000,QImage::Format_RGB888);
对于w* h=1050 * 1000的图像,由于1050/4=262.5,必须采用非连续内存的构造函数
//imgData的格式是RGB888
QImage qimage(imgData,1050,1000,1050*3,QImage::Format_RGB888);
在创建QImage时,构造函数自动在每一行后面补零,由于w=1050,每行字节数=3150,需要补2个字节,即3152,实际qimage占用内存为3152*1000,比原来多了2000个字节。
需要注意,以下接口并非针对图像数据的数据流,而是图像文件的数据流:
bool QImage::loadFromData(const uchar *data, int len, const char *format = Q_NULLPTR)
举个例子,如.bmp的数据流,不仅包括图像数据本身,还包括bmp文件头
注意,QImage的拷贝构造函数是浅拷贝:
QImage::QImage(const QImage &image)
2.图像的roi区域
QImage QImage::copy(const QRect &rectangle = QRect()) const
3.图像放缩
QImage QImage::scaled ( const QSize & size,Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio, Qt::TransformationModetransformMode = Qt::FastTransformation ) const
其中,enum Qt::AspectRatioMode包括:
Qt::IgnoreAspectRatio : The size is scaled freely. The aspect ratio is not preserved.
Qt::KeepAspectRatio : The size is scaled to a rectangle as large as possible inside a given rectangle, preserving the aspect ratio.(维持长宽比)
Qt::KeepAspectRatioByExpanding : The size is scaled to a rectangle as small as possible outside a given rectangle, preserving the aspect ratio.
enum Qt::TransformationMode包括:
Qt::FastTransformation:The transformation is performed quickly, with no smoothing.(最近邻)
Qt::SmoothTransformation:The resulting image is transformed using bilinear filtering.(双线性插值)
示例:
QImage* imgScaled = new QImage;
*imgScaled=img->scaled(width,
height,
Qt::KeepAspectRatio);
ui->label->setPixmap(QPixmap::fromImage(*imgScaled));
3.图像旋转
首先,获得几何变换矩阵:
QMatrix &QMatrix::rotate(qreal degrees)
然后,几何变换函数:
QImage QImage::transformed(const QMatrix &matrix, Qt::TransformationMode mode = Qt::FastTransformation) const
示例:
QImage* imgRatate = new QImage;
QMatrix matrix;
matrix.rotate(270);
*imgRotate = img->transformed(matrix);
ui->label->setPixmap(QPixmap::fromImage(*imgRotate));