OpenCV4.5.1新增微信QRCode解码功能使用步骤与测评 附源码和效果视频

导读:

    微信开源了QRCode解码功能,并可以在OpenCV中使用,本期将介绍使用步骤和效果演示。

图片

 

使用步骤:   

    WeChatQRCode模块为OpenCV4.5.1新增功能,需要在github下载最新opencv源码master和contrib部分编译后使用。

    源码编译使用CMake,相关编译教程很多博客写的很详细,这里只做关键步骤说明:

    (1) 编译是勾选BUILD_opencv_wechat_qrcode选项(默认勾选)

图片

    (2) 勾选BUILD_opencv_world 这样只生成一个dll方便使用

图片

    (3) 先Configure,后Generate,直至不报错(警告可忽略)

    (4) 打开OpenCV.Sln,先生成ALLBuild,再仅用于项目生成Install,生成成功(无错误,无失败)会得到如下文件:

图片

 

图片

    在include/opencv2文件夹下会多一个wechat_qrcode.hpp

图片

    bin文件夹下只生成opencv_world451.dll

图片


    接下来就是配置步骤:包含目录、库目录、附加依赖项和环境变量,这个大家用这么久OpenCV应该都会了,这里跳过。

    最后上代码:


#include "pch.h"
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/wechat_qrcode.hpp>

using namespace std;
using namespace cv;

int main()
{
  //加载图片解码
  Ptr<wechat_qrcode::WeChatQRCode> detector;
  string detect_prototxt = "./model/detect.prototxt";
  string detect_caffe_model = "./model/detect.caffemodel";
  string sr_prototxt = "./model/sr.prototxt";
  string sr_caffe_model = "./model/sr.caffemodel";

  Mat img = imread("./QR/T33/result.bmp");
  try 
  {
    detector = makePtr<wechat_qrcode::WeChatQRCode>(detect_prototxt, detect_caffe_model,
                                                  sr_prototxt, sr_caffe_model);
  }
  catch (const std::exception& e) 
  {
    cout <<
      "\n---------------------------------------------------------------\n"
      "Failed to initialize WeChatQRCode.\n"
      "Please, download 'detector.*' and 'sr.*' from\n"
      "https://github.com/WeChatCV/opencv_3rdparty/tree/wechat_qrcode\n"
      "and put them into the current directory.\n"
      "---------------------------------------------------------------\n";
    cout << e.what() << endl;
    return 0;
  }

  vector<Mat> vPoints;
  vector<String> strDecoded;
  strDecoded = detector->detectAndDecode(img, vPoints);
  for (int i = 0; i < strDecoded.size(); i++)
  {
    cout << "decode-" << i+1 << ": " << strDecoded[i] << endl;
    Point pt1 = Point((int)vPoints[i].at<float>(0, 0), (int)vPoints[i].at<float>(0, 1));
    Point pt2 = Point((int)vPoints[i].at<float>(1, 0), (int)vPoints[i].at<float>(1, 1));
    Point pt3 = Point((int)vPoints[i].at<float>(2, 0), (int)vPoints[i].at<float>(2, 1));
    Point pt4 = Point((int)vPoints[i].at<float>(3, 0), (int)vPoints[i].at<float>(3, 1));
    line(img, pt1, pt2, Scalar(0, 255, 0), 2);
    line(img, pt2, pt3, Scalar(0, 255, 0), 2);
    line(img, pt3, pt4, Scalar(0, 255, 0), 2);
    line(img, pt4, pt1, Scalar(0, 255, 0), 2);
    putText(img, strDecoded[i], pt1, 0, 0.5, Scalar(255, 0, 0), 2);
  }
  imshow("wechat_qrcode", img);
  waitKey();
  imwrite("result.png", img);

    核心函数strDecoded = detector->detectAndDecode(img, vPoints);可以得到QRCode位置和解码内容,使用到的模型下载地址如下:      https://github.com/WeChatCV/opencv_3rdparty

图片

    加载两张图像测试效果:

图片

图片

    视频实时测试效果:

OpenCV使用微信QRCode解码功能效果演示

    使用体验:非常适用APP开发者手机端扫码使用,如果是工业应用还需要自己做预处理和增强等步骤。大家有兴趣可以自己尝试。

### 实现微信二维码解码功能的方法 要在 Visual Studio 中实现微信二维码解码功能,可以按照以下方法操作: #### 方法一:基于 OpenCV 的 C/C++ Native 方式 可以通过调用 OpenCV 提供的 `wechat_qrcode` 模块来完成此功能。以下是具体步骤示例。 1. **配置环境** 需要先安装支持 `wechat_qrcode` 功能模块的 OpenCV 版本(如 OpenCV 4.5.1 或更高版本)。如果未包含该模块,则需自行编译并启用它[^1]。 2. **编写头文件导出函数** 创建一个用于封装解码逻辑的类或静态接口。例如,在 `ZrdDecoder.h` 文件中定义如下内容: ```cpp #pragma once #include <iostream> #include <opencv2/opencv.hpp> #include <opencv2/wechat_qrcode.hpp> extern "C" _declspec(dllexport) void Init(); extern "C" _declspec(dllexport) const char* Decode(const unsigned char*, int, int); // 存放解码结果 static char decodeResult[256] = { 0 }; // 微信解码器实例 static cv::Ptr<cv::wechat_qrcode::WeChatQRCode> detector; ``` 3. **实现核心逻辑** 编写对应的 `.cpp` 文件以初始化解码器以及处理图像数据流。 ```cpp #include "ZrdDecoder.h" void Init() { try { std::string modelPath = "./detect.prototxt"; // 替换为实际路径 std::string weightPath = "./detect.caffemodel"; // 替换为实际路径 detector = cv::wechat_qrcode::WeChatQRCode::create(modelPath, weightPath); } catch (const std::exception& e) { std::cerr << "Initialization failed: " << e.what() << std::endl; } } const char* Decode(const unsigned char* imageData, int width, int height) { cv::Mat image(height, width, CV_8UC1, (void*)imageData); if (!detector || image.empty()) { return ""; } std::vector<std::string> results; detector->decode(image, results); if (!results.empty()) { strcpy_s(decodeResult, results[0].c_str()); } else { decodeResult[0] = '\0'; } return decodeResult; } ``` 上述代实现了两个主要功能:一是加载模型权重;二是接收二进制图片数据作为输入参数,并返回解析后的字符串结果[^2]。 --- #### 方法二:借助 ZXing.Net 库集成到 .NET 平台 对于希望快速开发的应用场景,可以直接利用第三方库 ZXing.Net 来简化流程。 1. **引入 NuGet 包** 打开 Visual Studio 后,右键单击目标项目 -> “管理 NuGet 程序包”,搜索关键词 `ZXing.Net` 安装最新稳定版[^3]。 2. **编实现** 下面是一个简单的控制台应用程序案例展示如何读取本地存储的一张 PNG 图片并提取其中嵌入的信息。 ```csharp using System; using ZXing; namespace QRCodeDemoApp { class Program { static void Main(string[] args) { var reader = new BarcodeReader(); string filePath = @"D:\test.png"; var bitmap = new System.Drawing.Bitmap(filePath); var result = reader.Decode(bitmap); Console.WriteLine($"Decoded content: {result?.Text ?? "<No data found>"}"); } } } ``` 此处需要注意的是,当传入无效或者不兼容类型的图形资源时可能会抛异常,因此建议加入必要的错误捕获机制加以保护。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Color Space

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值