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

2023-12-15 14:31:11

在这里插入图片描述
【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

文章来源:https://blog.csdn.net/qq_34974229/article/details/134874027
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。