QT4 How to blur QPixmap image?

前端 未结 6 2083
遥遥无期
遥遥无期 2021-01-06 02:21

QT4 How to blur QPixmap image?

I am looking for something like one of the following:

Blur(pixmap); 
painter.Blur(); 
painter.Blur(rect);
         


        
6条回答
  •  醉话见心
    2021-01-06 02:49

    Method 1a: grab the raw bits and do it yourself. You'll need to be sufficiently familiar with bitmaps and blurring algorithms to implement the blur yourself. If you want that sort of precision, this is the way to go.

    QImage image = pixmap.toImage();
    if (image.format() != QImage::Format_RGB32)
         image = image.convertToFormat(QImage::Format_RGB32);
    uchar* bits = image.bits();
    int rowBytes = image.bytesPerLine();
    DoMyOwnBlurAlgorithm(bits, image.width(), image.height(), rowBytes);
    return QPixmap::fromImage(image);
    

    Method 1b: who needs raw bits? You can use image.pixel(x,y) and image.setPixel(x,y,color) instead. This won't be as fast as 1a, but it should be a bit easier to understand and code.

    QImage image = pixmap.toImage();
    QImage output(image.width(), image.height(), image.format());
    for (int y=0; y

    Method 2: use a QGraphicsBlurEffect, through a widget or scene. The code here uses a label widget:

    QPixmap BlurAPixmap(const QPixmap& inPixmap)
    {
        QLabel* label = new QLabel();
        label->setPixmap(inPixmap);
        label->setGraphicsEffect(new QGraphicsBlurEffect());
        QPixmap output(inPixmap.width(), inPixmap.height());
        QPainter painter(&output);
        label->render(&painter);
        return output;
    }
    

    Tweak as needed. For example, I'm presuming the default graphics blur effect is acceptable. I'm using Method 2 in my project.

提交回复
热议问题