xw.OpenCvSharp4.Barcode 4.11.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package xw.OpenCvSharp4.Barcode --version 4.11.0
                    
NuGet\Install-Package xw.OpenCvSharp4.Barcode -Version 4.11.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="xw.OpenCvSharp4.Barcode" Version="4.11.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="xw.OpenCvSharp4.Barcode" Version="4.11.0" />
                    
Directory.Packages.props
<PackageReference Include="xw.OpenCvSharp4.Barcode" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add xw.OpenCvSharp4.Barcode --version 4.11.0
                    
#r "nuget: xw.OpenCvSharp4.Barcode, 4.11.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package xw.OpenCvSharp4.Barcode@4.11.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=xw.OpenCvSharp4.Barcode&version=4.11.0
                    
Install as a Cake Addin
#tool nuget:?package=xw.OpenCvSharp4.Barcode&version=4.11.0
                    
Install as a Cake Tool

OpenCvSharp4.Barcode

给 OpenCvSharp4 补上 cv::barcode::BarcodeDetector(条形码检测 / 解码)能力的扩展包。

OpenCvSharp 4.11 的原生库里其实已经编译了该实现(位于 objdetect 模块),但托管层没有绑定,C# 无法直接调用。本包通过一个极小的原生 shim + P/Invoke 把它"打通":

  • 支持条码类型:EAN-13、EAN-8、UPC-A、UPC-E(结果里的 Type 分别为 EAN_13 / EAN_8 / UPC_A / UPC_E)
  • API 与 OpenCV 的 BarcodeDetector 保持一致(Detect / DetectAndDecode / 降采样阈值 / 梯度阈值 / 滤波尺寸)
  • 多目标框架:netstandard2.0、net48、net6.0、net8.0、net9.0、net10.0
  • 原生库静态链接精简版 OpenCV(仅 core/imgproc/features2d/flann/calib3d/objdetect),不依赖外部 opencv_world,也不会和 OpenCvSharp 自带的 OpenCV 冲突
  • 只提供 win-x64 原生库

安装

dotnet add package xw.OpenCvSharp4.Barcode --version 4.11.0

可选:包内已附带超分模型(models/sr.prototxt + models/sr.caffemodel,与微信二维码 wechat_qrcode 同源),安装后会自动复制到输出目录,可直接使用:

// 自动使用随包模型(模型缺失时退回默认检测器)
using var detector = BarcodeModels.CreateDetector();

// 或手动指定路径
using var detector2 = new BarcodeDetector(BarcodeModels.SrPrototxtPath, BarcodeModels.SrCaffeModelPath);

不传模型时行为与默认构造完全一致。

项目还需要引用 OpenCvSharp4 的原生运行时(一般已有):

dotnet add package OpenCvSharp4
dotnet add package OpenCvSharp4.runtime.win

使用

using OpenCvSharp;
using OpenCvSharp.Barcode;

using var image = Cv2.ImRead("barcode.png");
using var detector = new BarcodeDetector();

// 检测 + 解码(一次调用拿到多个条码)
foreach (var result in detector.DetectAndDecode(image))
{
    Console.WriteLine($"{result.Type}: {result.Text}");

    // 角点顺序:左下、左上、右上、右下
    foreach (var p in result.Corners)
        Cv2.Circle(image, (Point)p, 6, Scalar.Red, -1);
}

// 只检测(不解码)
Point2f[][] corners = detector.Detect(image);

// 常见调参
detector.DownsamplingThreshold = 1024;           // 默认 512,调大可提升小条码检出率
detector.GradientThreshold = 64;                 // 默认 64,调大可减少误检
detector.DetectorScales = new[] { 0.01f, 0.03f, 0.06f, 0.08f };

说明:图像会在托管侧自动规整为原生层接受的格式(8 位、1/3 通道、连续内存);连续内存的 CV_8UC1 / CV_8UC3 图像不会发生拷贝。

仓库结构

native/          C++ shim(CMake),导出纯 C 接口封装 cv::barcode::BarcodeDetector
src/             托管库(多目标框架 + P/Invoke + 原生库定位加载)
models/          超分模型 sr.prototxt / sr.caffemodel(随包分发)
tests/           端到端冒烟测试(真实条码图解码验证)
build/           build-native.ps1:编译静态 OpenCV + shim

从源码构建

前置:OpenCV 4.11.0 源码、CMake ≥ 3.20、Visual Studio 2022(C++ 桌面工作负载)。

# 1) 编译静态 OpenCV(core/imgproc/features2d/flann/calib3d/objdetect)+ 原生 shim
powershell -ExecutionPolicy Bypass -File build\build-native.ps1 -OpenCvSource <opencv-4.11.0 目录>

# 2) 冒烟测试
dotnet run --project tests\BarcodeSmokeTest

# 3) 打包
dotnet pack src\OpenCvSharp4.Barcode -c Release -o artifacts

# 4) 发布到 nuget.org(需要 API Key)
dotnet nuget push artifacts\xw.OpenCvSharp4.Barcode.4.11.0.nupkg --api-key <你的KEY> --source https://api.nuget.org/v3/index.json

许可

本包遵循 Apache-2.0 AND BSD-3-Clause AND Zlib。原生库静态链接/内含的第三方组件:

组件 许可 说明
OpenCV 4.11.0 Apache-2.0 见 OpenCV-LICENSE.txt
WeChat QRCode 超分模型 Apache-2.0(© Tencent) 见 WeChat-QRCode-LICENSE.txt
Intel ITT Notify BSD-3-Clause 见 THIRD-PARTY-NOTICES.txt
Protocol Buffers BSD-3-Clause 见 THIRD-PARTY-NOTICES.txt
zlib Zlib 见 THIRD-PARTY-NOTICES.txt
FlatBuffers Apache-2.0 见 THIRD-PARTY-NOTICES.txt

完整版权与许可声明见 THIRD-PARTY-NOTICES.txt。

本包为非官方扩展,与 OpenCvSharp、OpenCV、腾讯均无隶属或背书关系;相关名称仅用于说明兼容性与模型来源。

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 is compatible.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
4.11.1 90 11/29/2026
4.11.0 99 11/29/2026 4.11.0 is deprecated because it has critical bugs.