使用Qt调整图像大小并获得最佳质量

13

有人能帮我在Qt中调整图片大小而不让图片失真吗?以下是我的代码。目前的结果与原始质量相比不太好。谢谢...

QImage img(name);
QPixmap pixmap;
pixmap = pixmap.fromImage(img.scaled(width,height,Qt::IgnoreAspectRatio,Qt::FastTransformation));
QFile file(folder+"/"+name);
file.open(QIODevice::WriteOnly);
pixmap.save(&file, "jpeg",100);
file.close();

你是缩小还是扩展图像?如果你扩展图像,你将总是有一个“大”的质量损失,因为新空间中没有比原始图像更多的信息。然而,每次调整大小都会导致质量损失。有很多算法可以解决这个问题,它们都有不同的优缺点。 - Sebastian Lange
你试过使用Qt::SmoothTransformation而不是Qt::FastTransformation吗? - h2so5
1个回答

25
你需要在 scaled 函数中传递 Qt::SmoothTransformation 变换模式,例如:
QImage img(name);
QPixmap pixmap;
pixmap = pixmap.fromImage(img.scaled(width,height,Qt::IgnoreAspectRatio,Qt::SmoothTransformation));
QFile file(folder+"/"+name);
file.open(QIODevice::WriteOnly);
pixmap.save(&file, "jpeg",100);
file.close();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接