【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

相关推荐

  1. 在VS IDE中搜索所有带有中文字符串

    2023-12-09 06:44:02       26 阅读
  2. PhpSpreadsheet 读取 excel 里面图片

    2023-12-09 06:44:02       38 阅读
  3. opencv读写路径包含中文文件

    2023-12-09 06:44:02       24 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-09 06:44:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-09 06:44:02       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-09 06:44:02       87 阅读
  4. Python语言-面向对象

    2023-12-09 06:44:02       96 阅读

热门阅读

  1. torchvision中的标准ResNet50网络结构

    2023-12-09 06:44:02       54 阅读
  2. uniapp app端路由跳转时设置跳转过渡时间

    2023-12-09 06:44:02       66 阅读
  3. R语言读文件“-“变成“.“

    2023-12-09 06:44:02       48 阅读
  4. 一篇文章熟练掌握 Axios

    2023-12-09 06:44:02       60 阅读
  5. iOS app切换后台时添加模糊遮罩层

    2023-12-09 06:44:02       59 阅读
  6. Python函数的参数

    2023-12-09 06:44:02       62 阅读
  7. 128.最长连续子序列

    2023-12-09 06:44:02       46 阅读
  8. SQLite基本使用

    2023-12-09 06:44:02       58 阅读
  9. redis中序列化问题,value包含全路径类名解析

    2023-12-09 06:44:02       50 阅读
  10. ALLEGRO PCB 如何设置增加的过孔

    2023-12-09 06:44:02       64 阅读
  11. GDS Configuration File Changes to Support Dynamic Routing

    2023-12-09 06:44:02       65 阅读