|
找到了这个文章,但是是针对整个图片的,不知道如何配合 QPainter 和 QPen 来实现对鼠标路径的模糊效果。
- #include "mainwindow.h"
- #include <QGraphicsBlurEffect>
- #include <QGraphicsPixmapItem>
- #include <QGraphicsScene>
- #include <QApplication>
- #include <QPainter>
- #include <QLabel>
- #include <QImage>
- #include <QPixmap>
- QImage applyEffectToImage(QImage src, QGraphicsEffect *effect, int extent = 0)
- {
- if (src.isNull()) return QImage(); //No need to do anything else!
- if (!effect) return src; //No need to do anything else!
- QGraphicsScene scene;
- QGraphicsPixmapItem item;
- item.setPixmap(QPixmap::fromImage(src));
- item.setGraphicsEffect(effect);
- scene.addItem(&item);
- QImage res(src.size() + QSize(extent * 2, extent * 2), QImage::Format_ARGB32);
- res.fill(Qt::transparent);
- QPainter ptr(&res);
- scene.render(&ptr, QRectF(), QRectF(-extent, -extent, src.width() + extent * 2, src.height() + extent*2 ));
- return res;
- }
- QImage blurred(const QImage& image, const QRect& rect, int radius, bool alphaOnly = false)
- {
- int tab[] = { 14, 10, 8, 6, 5, 5, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2 };
- int alpha = (radius < 1) ? 16 : (radius > 17) ? 1 : tab[radius-1];
- QImage result = image.convertToFormat(QImage::Format_ARGB32_Premultiplied);
- int r1 = rect.top();
- int r2 = rect.bottom();
- int c1 = rect.left();
- int c2 = rect.right();
- int bpl = result.bytesPerLine();
- int rgba[4];
- unsigned char* p;
- int i1 = 0;
- int i2 = 3;
- if (alphaOnly)
- i1 = i2 = (QSysInfo::ByteOrder == QSysInfo::BigEndian ? 0 : 3);
- for (int col = c1; col <= c2; col++) {
- p = result.scanLine(r1) + col * 4;
- for (int i = i1; i <= i2; i++)
- rgba[i] = p[i] << 4;
- p += bpl;
- for (int j = r1; j < r2; j++, p += bpl)
- for (int i = i1; i <= i2; i++)
- p[i] = (rgba[i] += ((p[i] << 4) - rgba[i]) * alpha / 16) >> 4;
- }
- for (int row = r1; row <= r2; row++) {
- p = result.scanLine(row) + c1 * 4;
- for (int i = i1; i <= i2; i++)
- rgba[i] = p[i] << 4;
- p += 4;
- for (int j = c1; j < c2; j++, p += 4)
- for (int i = i1; i <= i2; i++)
- p[i] = (rgba[i] += ((p[i] << 4) - rgba[i]) * alpha / 16) >> 4;
- }
- for (int col = c1; col <= c2; col++) {
- p = result.scanLine(r2) + col * 4;
- for (int i = i1; i <= i2; i++)
- rgba[i] = p[i] << 4;
- p -= bpl;
- for (int j = r1; j < r2; j++, p -= bpl)
- for (int i = i1; i <= i2; i++)
- p[i] = (rgba[i] += ((p[i] << 4) - rgba[i]) * alpha / 16) >> 4;
- }
- for (int row = r1; row <= r2; row++) {
- p = result.scanLine(row) + c2 * 4;
- for (int i = i1; i <= i2; i++)
- rgba[i] = p[i] << 4;
- p -= 4;
- for (int j = c1; j < c2; j++, p -= 4)
- for (int i = i1; i <= i2; i++)
- p[i] = (rgba[i] += ((p[i] << 4) - rgba[i]) * alpha / 16) >> 4;
- }
- return result;
- }
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- #if 1
- QLabel label;
- QImage image("d:/2018-08-13_13-36-56.png");
- image = blurred(image,image.rect(), 10, false);
- label.setPixmap(QPixmap::fromImage(image));
- label.show();
- #endif
- #if 0
- QGraphicsBlurEffect *blur = new QGraphicsBlurEffect;
- blur->setBlurRadius(8);
- QImage source("d:/2018-08-13_13-36-56.png");
- QImage result = applyEffectToImage(source, blur);
- result.save("d:/2018-08-13_13-36-56.backup.png");
- #endif
- return a.exec();
- }
复制代码 |
|