基于 Qt 的可视化图片处理程序——图片的加载与保存

基于 Qt 的可视化图片处理程序——图片的加载与保存

图片的加载

点击按钮选择文件

首先要确定,使用 Qt 中 QFileDialog 库中的 getLoadFile 进行选择文件。

代码如下:

LoadPicPath = QFileDialog::getOpenFileName(this,"选择图片","C://Users//ppqpp//Pictures","Pic Files (*.jpg;*.jeg;*.png;*.gif);;All Files (*);");
// getOpenFile 参数包含窗口的父类,窗口名称,默认选择地址,和要选择的文件格式
// 设置文件扩展名过滤,注意用双分号间隔

运行效果如下:

image

image

图片加载到 Label

在 Qt 中,我们一种最常用的图片加载方式就是使用 QImage 库,此处我们通过 QImage 加载图片,之后通过加载到 QPixmap 中,最终显示在 Label 上

首先声明 QImage 变量,并加载我们指定路径下的图片:

    QImage img;
    img.load(LoadPicPath);
    // LoadPicPath 是我们选择的文件的路径及名称

在成功加载图片之后,我们通过将图加载到 QPixmap 上,最终将图片显示在 Label 上:

image = img.scaled(this->ui->PicLabel->size(),Qt::KeepAspectRatio, Qt::SmoothTransformation);
/*
首先要按照比例合理缩放图片的大小,并且在缩放的过程中要保证图片不失帧
Qt::KeepAspectRatio 按比例缩放  Qt::SmoothTransformation 缩放不失帧
*/
QPixmap pixmap = QPixmap::fromImage(image);
// 在pixmap中加载图片
this->ui->PicLabel->setPixmap(pixmap)

// 将图片显示到 Label 中

运行效果如下:

image

完整代码

void MainWindow::Load(){    // 加载图片
    QImage img;
    img.load(LoadPicPath);
    w_initial = img.width();
    h_initial = img.height();
    image = img.scaled(this->ui->PicLabel->size(),Qt::KeepAspectRatio, Qt::SmoothTransformation);
    pixmap = QPixmap::fromImage(image);
    // Qt::KeepAspectRatio 按比例缩放  Qt::SmoothTransformation 缩放不失帧
    this->ui->PicLabel->clear();
    this->ui->PicLabel->setPixmap(pixmap);
    this->getpixel();
}
    
    void MainWindow::on_actionLoadPic_triggered()    // 点击事件
{
    if(LoadPicPath == ""){
        if_load_save = false;
    }
    else{
        qDebug()<<LoadPicPath;
        if_load_save = true;
        qDebug()<<"if_load_save = "<<if_load_save;
    }
    LoadPicPath = QFileDialog::getOpenFileName(this,"选择图片","C://Users//ppqpp//Pictures","Pic Files (*.jpg;*.jeg;*.png;*.gif);;All Files (*);");
    if(if_load_save){
        savedialog->show();
        savedialog->exec();
    }
    else{
        //        this->init_all();
        this->Load();
    }
}

图片的保存

点击按钮保存路径

与加载图片的方法一致,使用 getSaveFile 函数选择要保存图片的路径:

 MainWindow::SavePicPath = QFileDialog::getSaveFileName(this,"C://Users//ppqpp//Pictures",MainWindow::LoadPicPath,"Pic Files (*);");

运行效果如下:

image

image

保存图片

在保存图片之前,我们需要先提取要进行保存的目标图片,通过一种类似于截屏的方式 pixmap()->toImage() 对图片进行提取,然后缩放到原来的大小,最后进行保存

代码如下:

QImage img = this->ui->PicLabel->pixmap()->toImage().scaled(w_initial,h_initial,Qt::KeepAspectRatio, Qt::SmoothTransformation);
img.save(SavePicPath);

完整代码

void SaveDialog::on_pushButton_clicked()
{
    MainWindow::SavePicPath = QFileDialog::getSaveFileName(this,"C://Users//ppqpp//Pictures",MainWindow::LoadPicPath,"Pic Files (*);");
    this->ui->PathEdit->setText(MainWindow::SavePicPath);
}

void SaveDialog::on_SaveButton_clicked()
{
    qDebug()<<"Save new Pic"<<MainWindow::LoadPicPath;
    if(this->ui->PathEdit->text() != ""){
        emit to_save();
        if(MainWindow::if_load_save){
            qDebug()<<"if_load_save = "<<MainWindow::if_load_save;
            qDebug()<<"save_to_load";
            MainWindow::if_load_save = false;
            emit save_to_load();
        }
        this->close();
    }
}


void MainWindow::Cover_save(){
    qDebug()<<"Cover_save";
    QImage img = this->ui->PicLabel->pixmap()->toImage().scaled(w_initial,h_initial,Qt::KeepAspectRatio, Qt::SmoothTransformation);
    img.save(SavePicPath);
}
posted @ 2022-09-22 16:05  ppqppl  阅读(1018)  评论(0编辑  收藏  举报