【qt】Qt+OpenCv读取带有中文路径的图片

在这里插入图片描述
【opencv4.5.1版本】下载exe解压即可。。。https://opencv.org/releases/page/2/
【qt5.15.2】

pro文件

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++17

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    mainwindow.cpp


HEADERS += \
    mainwindow.h

FORMS += \
    mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

CONFIG(release, debug|release) {
    LIBS += -L$$PWD/../opencv/build/x64/vc15/lib/ -lopencv_world451
    opencv.files += $$PWD/sdk/opencv/bin/opencv_world451.dll
    opencv.path += $$OUT_PWD/Release
}
else {
    LIBS += -L$$PWD/../opencv/build/x64/vc15/lib/ -lopencv_world451d
    opencv.files += $$PWD/sdk/opencv/bin/opencv_world451d.dll
    opencv.path += $$OUT_PWD/Debug
}

COPIES += opencv

INCLUDEPATH += E:\opencv\opencv\build\include
DEPENDPATH += E:\opencv\opencv\build\include

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <opencv2/opencv.hpp>
using namespace cv;

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    QString m_imgPath = "";

private slots:
    void on_pushButton_clicked();
    void showImg();

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QDebug>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    m_imgPath = QFileDialog::getOpenFileName(this, tr("Open Image"), QCoreApplication::applicationDirPath(), tr("*.png *.jpg"));   //打开图片文件,选择图片
    qDebug() << "filename: " << m_imgPath;
    showImg();
}

void MainWindow::showImg()
{
    QFile file(m_imgPath);
    if (!file.open(QIODevice::ReadOnly))
    {
        qDebug() << u8"文件打开失败";
        return;
    }
    qDebug() << u8"文件读取成功";

    // 获取文件大小
    qint64 fileSize = file.size();

    // 读取文件字节到缓冲区
    QByteArray buffer;
    buffer.resize(fileSize);
    QDataStream in(&file);
    in.readRawData(buffer.data(), fileSize);

    // 将缓冲区转换为cv::Mat
    cv::Mat image = cv::imdecode(cv::_InputArray(buffer.constData(), buffer.size()), cv::IMREAD_UNCHANGED);

    if (image.empty())
    {
        qDebug() << u8"无法解码图像文件";
        return;
    }

    // 继续处理图像
    cv::cvtColor(image, image, cv::COLOR_BGR2RGB);
    QImage disimage = QImage(image.data, image.cols, image.rows,
                             image.step, QImage::Format_RGB888);
    ui->oldlabel->setPixmap(QPixmap::fromImage(disimage));
    ui->oldlabel->setScaledContents(true);
}

最后进行编译部署:
https://blog.csdn.net/qq_34974229/article/details/134872879?spm=1001.2014.3001.5502

  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值