【问题标题】:Rotating images causes distortion on Qt旋转图像会导致 Qt 失真
【发布时间】:2012-10-10 19:24:48
【问题描述】:

我在 Qt 中旋转图像时遇到了一些问题。每次我用QPainter 旋转我的图像时,它都会变得越来越失真。这是初始图像:

经过一些迭代,它变成:

这是我的代码:

void Ship::Move(int x, int y)
{
    QPixmap rotatePixmap(shipPixels.size());
    rotatePixmap.fill(Qt::transparent);

    QTransform transform;
    transform.translate(
            rotatePixmap.size().width() / 2,
            rotatePixmap.size().height() / 2
    );

    transform.rotate(degree);
    transform.translate(
            - rotatePixmap.size().width() / 2, 
            - rotatePixmap.size().height() / 2
    );

    QPainter p(&rotatePixmap);
    p.setRenderHints(
            QPainter::Antialiasing | QPainter::SmoothPixmapTransform,
            true
    );

    p.setTransform(transform);
    p.drawPixmap(0, 0, shipPixels);
    p.end();

    shipPixels = rotatePixmap;
    this->setPixmap(shipPixels);
    this->move(QPoint(x, y));
    degree = 0;
}

对我来说,Qt 保留了图像质量。这种行为完全奇怪。有什么原因吗?

【问题讨论】:

  • 每次都需要重新计算新的旋转位图。您正在累积舍入错误。
  • 是的,如果您在帧之间保持 shipPixels 不变,而是将其用作锚点,那么您应该很高兴。
  • 您看到的效果是不可避免的,因为除了 90 度增量以外的任何旋转量都是有损操作。损失加起来。它与 Qt 无关。
  • @MarkRansom 好点子,尽管理论和实践并不总是相互配合——我敢肯定有几个库会以 90 度增量模糊或扭曲图像! :-/

标签: c++ qt image-rotation rounding-error


【解决方案1】:

根据答案,新代码是这样的。其他发现自己遇到相同问题的用户可以获取此代码:

void Ship::Move(int x, int y)
{
    QPixmap sourceImage(shipPixels);

    QPixmap rotatePixmap(sourceImage.size());
    rotatePixmap.fill(Qt::transparent);

    QTransform transform;
    transform.translate(sourceImage.size().width() / 2, sourceImage.size().height() / 2);
    transform.rotate(degree);
    transform.translate(-sourceImage.size().width() / 2, -sourceImage.size().height() / 2);

    QPainter p;
    p.setRenderHints(QPainter::Antialiasing|QPainter::SmoothPixmapTransform, true);
    p.begin(&rotatePixmap);
    p.setTransform(transform);
    p.drawPixmap(0, 0, sourceImage);
    p.end();

    rotatePixmap.save("temp.png");

    this->setPixmap(rotatePixmap);
    this->move(QPoint(x, y));
}

【讨论】:

    【解决方案2】:
    QPainter pixmap;
    pixmap.setRenderHints(QPainter::Antialiasing|QPainter::SmoothPixmapTransform,true);
    

    这就是你要找的。​​p>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-30
      • 2011-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多