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);

我们来看一下效果对比吧:

QImage缩放后图片更清晰处理(快速缩放)QImage缩放后图片更清晰处理(清楚缩放)

效果是不是差很多!


以下是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
    };

好啦,又解决一个小问题!

相关文章:

  • 2021-11-17
  • 2022-01-29
  • 2021-07-22
  • 2021-11-04
猜你喜欢
  • 2022-12-23
  • 2021-11-02
  • 2022-12-23
相关资源
相似解决方案