Qt开发——图片缩放简述

代码如下:

	QImage Image;
    Image.load("d:/test.jpg");
    QPixmap pixmap = QPixmap::fromImage(Image);
    int with = ui->labPic->width();
    int height = ui->labPic->height();
    QPixmap fitpixmap = pixmap.scaled(with, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);  // 饱满填充
    //QPixmap fitpixmap = pixmap.scaled(with, height, Qt::KeepAspectRatio, Qt::SmoothTransformation);  // 按比例缩放
    ui->labPic->setPixmap(fitpixmap);

介绍:
scaled方法有几个参数需要介绍,方法原型:

QPixmap scaled(const QSize &s, Qt::AspectRatioMode aspectMode = Qt::IgnoreAspectRatio,
                   Qt::TransformationMode mode = Qt::FastTransformation) const;

参数一: 放大的size
参数二:枚举值

enum AspectRatioMode
{
    IgnoreAspectRatio,
    KeepAspectRatio,
    KeepAspectRatioByExpanding
}

IgnoreAspectRatio 矩形框有多大,图片就缩放成多大,不限制原图片的长宽比
KeepAspectRatio 保持原图片的长宽比,且不超过矩形框的大小
KeepAspectRatioByExpanding 根据矩形框的大小最大缩放图片

参数三:枚举值

    enum TransformationMode {
        FastTransformation,
        SmoothTransformation
    };

FastTransformation 快速缩放,

SmoothTransformation 平滑缩放

但是如果你用缩放函数去做大图片的缩略图可能会发现”快速缩放”得到的图片质量不佳, 而”平滑缩放”质量很好但速度欠佳, 特别是原图非常大的时候smoothscale简直就是个噩梦阿。 这里就可以使用被称为“Cheat Scaling”的缩小图片的技巧了, 那就是先使用”快速缩放”得到一个中等大小的图片以获得较快的缩放速度, 再使用”平滑缩放”缩小至需要的大小以获得较好的图片质量。

 QImage result = img.scaled(800, 600).scaled(200, 150, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); 

一个公认比较好的方法是,先缩至缩略图4倍大小, 再进一步平滑缩放。 如果图片过大经过测试, 该算法甚至比“快速缩放”还要略快, 却能获得和“平滑缩放”极其接近的最终结果。 

本文福利, 免费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,Qt编程入门,QT信号与槽机制,QT图像绘制,QT网络,QT数据库编程,QT项目实战,QT嵌入式开发,Quick模块等等)↓↓↓↓↓↓见下面↓↓文章底部点击免费领取↓↓

  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值