当前位置: 首页 > news >正文

Qt实现 图片处理器PictureEdit

目录

  • 图片处理器PictureEdit
  • 1 创建工具栏
  • 2 打开图片
  • 3 显示图片
  • 4 灰度处理
  • 5 颜色反转
  • 6 马赛克

图片处理器PictureEdit

创建工程,添加资源文件

image-20230904144039679

1 创建工具栏

  • widget.h
#include <QWidget>
#include<QPixmap>
#include<QFileDialog>
#include<QAction>
#include<QToolBar>
#include<QIcon>

class QToolBar;
private:
    void createTollBar();

private:
    QPixmap pic;
    QToolBar * toolBar;
  • widget.cpp
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    this->createTollBar();
    this->resize(400,500);
}
  • 添加按钮及图标
void Widget::createTollBar()
{
    this->toolBar = new QToolBar(this);

    //打开图片
    QAction *openAct = new QAction(QIcon("://Icon/open.png"),"打开图片");
    this->toolBar->addAction(openAct);

    //还原
    QAction *recoverAct = new QAction(QIcon("://Icon/recover.png"),"还原图片");
    this->toolBar->addAction(recoverAct);

    //灰图
    QAction *grayAct = new QAction(QIcon("://Icon/gray.png"),"灰图处理");
    this->toolBar->addAction(grayAct);

    //反转
    QAction *invertnAct = new QAction(QIcon("://Icon/Invert colors.png"),"颜色反转");
    this->toolBar->addAction(invertnAct);

    //模糊
    QAction *blurAct = new QAction(QIcon("://Icon/blurring.png"),"模糊处理");
    this->toolBar->addAction(blurAct);

    //马赛克
    QAction *mosaicAct = new QAction(QIcon("://Icon/mosaic.png"),"马赛克处理");
    this->toolBar->addAction(mosaicAct);

    //水墨
    QAction *inkAct = new QAction(QIcon("://Icon/ink painting.png"),"水墨处理");
    this->toolBar->addAction(inkAct);
}

image-20230904144248136

2 打开图片

  • widget.h中添加槽
public slots:
    void openPic();
  • widget.cpp中实现
void Widget::openPic()
{
    QString filePath = QFileDialog::getOpenFileName(this,"打开图片","./","image(*.png *.jpg *.bmp");
    this->pic = QPixmap(filePath);
    this->resize(this->pic.size());
}
  • 然后在createToolBar函数中连接
    //打开图片
    QAction *openAct = new QAction(QIcon("://Icon/open.png"),"打开图片");

    //连接信号
    connect(openAct,&QAction::triggered,this,&Widget::openPic);
    this->toolBar->addAction(openAct);

image-20230904150102099

不过此时添加后图片还无法显示

3 显示图片

  • widget.h中重写画图的虚函数
protected:
    //重写虚函数
    void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
  • widget.cpp
void Widget::paintEvent(QPaintEvent *event)
{
    //显示在当前窗口
    QPainter p(this);
    //绘制图片
    p.drawPixmap(0,0,this->pic.width(),this->pic.height(),this->pic);
    QWidget::paintEvent(event);
}

image-20230904151536324

4 灰度处理

  • widget.h
    void grayPic();
  • widget.cpp
void Widget::grayPic()
{
    QImage image = this->pic.toImage();
    image.convertToFormat(QImage::Format_ARGB32);//转换成ARGB32以方便设置颜色
    //分辨遍历长和宽,因为图片长方形
    for(int i=0; i<=image.width();i++)
    {
        for(int j=0;j<image.height();j++)
        {
            //每个像素点都修改
            QRgb pixel = image.pixel(i,j);
            //平均值
            int avg = qGray(pixel);
            image.setPixel(i,j,qRgb(avg,avg,avg));
        }
    }
    this->pic = QPixmap::fromImage(image);
    //重新画图
    this->update();
}
  • createToolBar函数,连接信号
    //灰图
    QAction *grayAct = new QAction(QIcon("://Icon/gray.png"),"灰图处理");
    connect(grayAct,&QAction::triggered,this,&Widget::grayPic);
    this->toolBar->addAction(grayAct);

image-20230904152803901

5 颜色反转

  • widget.h
    void invertnPic();
  • widget.cpp
void Widget::invertnPic()
{
    QImage image = this->pic.toImage();
    image.convertToFormat(QImage::Format_ARGB32);//转换成ARGB32以方便设置颜色
    //分辨遍历长和宽,因为图片长方形
    for(int i=0; i<=image.width();i++)
    {
        for(int j=0;j<image.height();j++)
        {
            //每个像素点都修改
            QColor pixel = image.pixelColor(i,j);

            int r = pixel.red();
            int g = pixel.green();
            int b = pixel.blue();

            QColor newColor((255-r),(255,g),(255-b));
            image.setPixelColor(i,j,newColor);
        }
    }
    this->pic = QPixmap::fromImage(image);
    //重新画图
    this->update();
}
  • createToolBar函数,连接信号
    //反转
    QAction *invertnAct = new QAction(QIcon("://Icon/Invert colors.png"),"颜色反转");
    connect(invertnAct,&QAction::triggered,this,&Widget::invertnPic);
    this->toolBar->addAction(invertnAct);

image-20230904154033208

6 马赛克

  • widget.h
    void mosaicPic();
  • widget.cpp
void Widget::mosaicPic()
{
    QImage image = this->pic.toImage();
    image.convertToFormat(QImage::Format_ARGB32);//转换成ARGB32以方便设置颜色
    //分辨遍历长和宽,因为图片长方形
    int w = image.width()%SIZE;
    int h = image.height()%SIZE;
    for(int i=0; i<=image.width()-w;i+=SIZE)
    {
        for(int j=0;j<image.height()-h;j+=SIZE)
        {
            //每个像素点都修改
            int r=0,g=0,b=0;
            for(int m = i;m<i+SIZE;m++)
            {
                for(int n = j;n<j+SIZE;n++)
                {
                    QColor c = image.pixelColor(m,n);
                    r+=c.red();
                    g+=c.green();
                    b+=c.blue();
                }
            }
            QColor newColor(r/(SIZE*SIZE),g/(SIZE*SIZE),b/(SIZE*SIZE));
            for(int m = i;m<i+SIZE;m++)
            {
                for(int n = j;n<j+SIZE;n++)
                {
                    image.setPixelColor(m,n,newColor);
                }
            }
        }
    }
    this->pic = QPixmap::fromImage(image);
    //重新画图
    this->update();
}
  • createToolBar函数,连接信号
    //马赛克
    QAction *mosaicAct = new QAction(QIcon("://Icon/mosaic.png"),"马赛克处理");
    connect(mosaicAct,&QAction::triggered,this,&Widget::mosaicPic);
    this->toolBar->addAction(mosaicAct);

image-20230904162403774

  • createToolBar函数,连接信号
    //马赛克
    QAction *mosaicAct = new QAction(QIcon("://Icon/mosaic.png"),"马赛克处理");
    connect(mosaicAct,&QAction::triggered,this,&Widget::mosaicPic);
    this->toolBar->addAction(mosaicAct);

[外链图片转存中…(img-xxswHv9g-1696602317319)]

相关文章:

  • Kafka日志索引详解以及生产常见问题分析与总结
  • AdaBoost(上):数据分析 | 数据挖掘 | 十大算法之一
  • 参与现场问题解决总结(Kafka、Hbase)
  • Hibernate验证用户提交对象信息
  • Typescript 综合笔记:解读一个github中的React 网页
  • 国微FPGA培训
  • 云计算:常用系统前端与后端框架
  • 岛屿的数量
  • 【【萌新的SOC学习之AXI接口简介】】
  • thinkphp6 - 超详细使用阿里云短信服务发送验证码功能,TP框架调用对接阿里云短信发验证码(详细示例代码,一键复制开箱即用)
  • 第二证券:汽车产业链股活跃,恒勃股份、博俊科技“20cm”涨停
  • BS EN 12104-2023 软木地砖检测
  • Flutter环境搭建及新建项目
  • 【Git笔记】之Git重命名详解
  • 【OSPF宣告——network命令与多区域配置实验案例】
  • 【Qt】三种方式实现抽奖小游戏
  • unity操作_Camera c#
  • fastadmin框架如何开启事务
  • GO 中的指针?
  • 【AI视野·今日Robot 机器人论文速览 第四十九期】Fri, 6 Oct 2023
  • 巴西总统卢拉抵达北京
  • 可量产9MWh超大容量储能系统亮相慕尼黑,宁德时代:大储技术迈入新时代
  • 中国天主教组织发贺电对新教皇当选表示祝贺
  • 国家主席习近平同普京总统签署关于进一步深化中俄新时代全面战略协作伙伴关系的联合声明
  • 少年中国之少年的形塑
  • 进化版大巴黎通杀英超,那个男人后悔了吗