You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Emgu CV v4.6中使用QRCodeDetector的Detect和Decode函数?

Emgu CV v4.6 + C# 6.0 二维码扫描实现指南

问题修正与核心说明

你当前的代码问题出在存储二维码顶点的Mat尺寸错误:二维码有4个顶点(每个顶点含x、y坐标),需要创建4行2列的32位整数类型Mat,才能正确存储顶点信息,供后续Decode方法使用。

完整实现代码

using Emgu.CV;
using Emgu.CV.QRCode;
using Emgu.CV.Structure;
using System;

namespace QRCodeReading
{
    class Program
    {
        static void Main(string[] args)
        {
            // 读取目标二维码图片
            Image<Bgr, byte> qrImage = new Image<Bgr, byte>(@"C:\Users\Dacapo\source\repos\qrcode_reading\qrcode_reading\qr.jpg");
            IInputArray inputImage = qrImage;

            // 创建存储二维码4个顶点的Mat:4行(对应4个顶点)、2列(每个顶点的x/y坐标),32位整数类型
            Mat qrVertices = new Mat(4, 2, Emgu.CV.CvEnum.DepthType.Cv32S, 1);

            QRCodeDetector detector = new QRCodeDetector();

            // 检测二维码:检测成功时,顶点坐标会写入qrVertices
            bool isQrDetected = detector.Detect(inputImage, qrVertices);

            if (isQrDetected)
            {
                // 解码二维码内容
                string decodedContent = detector.Decode(inputImage, qrVertices);
                Console.WriteLine("二维码解码结果:" + decodedContent);
            }
            else
            {
                Console.WriteLine("未检测到二维码");
            }

            // 手动释放Emgu CV资源,避免内存泄漏
            qrImage.Dispose();
            qrVertices.Dispose();
            detector.Dispose();
        }
    }
}

关键细节解释

  • Detect方法的第二个参数是输出参数:检测成功时,会将二维码的4个顶点坐标(顺时针顺序)写入该Mat中,这是Decode方法必需的位置信息。
  • Decode方法:传入原始图像和检测到的顶点Mat,直接返回解码后的字符串内容。
  • 简化写法:如果不需要单独处理顶点信息,可以直接调用DetectAndDecode一步完成检测和解码:
    string decodedContent = detector.DetectAndDecode(inputImage, qrVertices);
    if (!string.IsNullOrEmpty(decodedContent))
    {
        Console.WriteLine("二维码解码结果:" + decodedContent);
    }
    

内容的提问来源于stack exchange,提问作者Dacapo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 15:45:48