QImage的縮放是比較常用的,今天碰到一個問題:
我發現縮放後的圖檔看起來很不清晰,于是看了一下QImage的scaled方法。發現它預設的是快速縮放,也就是增加scale的處理速度,犧牲的就是圖檔的品質。當我更需要圖檔品質的時候,就需要稍稍修改下scale的參數以實作更清楚的縮放。
一般縮放函數是這樣的:
QImage image = picData.toImage();
QImage igScaled = image.scaled(316, 236);
增加圖檔品質的縮放是這樣的:
QImage image = picData.toImage();
QImage igScaled = image.scaled(316, 236,Qt::IgnoreAspectRatio,Qt::SmoothTransformation);
我們來看一下效果對比吧:
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIiclRnblN0LclHdpZXYyd2LcBzNvwVZ2x2bzNXak9CX90TQNNkRrFlQKBTSvwFbslmZvwFMwQzLcVmepNHdu9mZvwFVywUNMZTY18CX052bm9CX90TQPVTR65UNVRkT4FEVkZXUYpVd1kmYr50MZV3YyI2cKJDT29GRjBjUIF2LcRHelR3LcJzLctmch1mclRXY39TMxcDOzcjM0ETMycDM3EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
(快速縮放)
(清楚縮放)
效果是不是差很多!
以下是QImage的Scaled方法接口:
inline QImage scaled(int w, int h, Qt::AspectRatioMode aspectMode = Qt::IgnoreAspectRatio,
Qt::TransformationMode mode = Qt::FastTransformation) const
{ return scaled(QSize(w, h), aspectMode, mode); }
QImage scaled(const QSize &s, Qt::AspectRatioMode aspectMode = Qt::IgnoreAspectRatio,Qt::TransformationMode mode = Qt::FastTransformation) const;
看一下AspectRatioMode這個枚舉:
enum AspectRatioMode {
IgnoreAspectRatio,
KeepAspectRatio,
KeepAspectRatioByExpanding
};
再看一下TransformationMode這個枚舉:
enum TransformationMode {
FastTransformation,
SmoothTransformation
};
好啦,又解決一個小問題!