C# 使用 ZXingCpp.Angle 进行条码、二维码识别

ZXingCpp.Angle

使用方法:dotnet add package ZXingCpp.Angle --version 1.0.0

nuget地址:NuGet Gallery | ZXingCpp.Angle 1.0.0

非官方修改版 :基于 zxing-cpp 3.1.1 与官方 .NET 封装 ZXingCpp 0.5.3,新增一维条码任意角度识别。

⚠️ 这是替代包:请用它替换 官方的 ZXingCpp 包,不要把两个包同时引用(托管程序集同名)。

相对上游的改进

项目 上游 3.1.1 本包
一维码可读角度 0°/90°(倾斜约 8~19° 以上就失败,取决于条码宽高比) ±45° + 0°/90°
细模组条码(1~2 px/模块、无抗锯齿截图) 常常失败 亚像素(2 点/像素)采样后可读
小条码在大照片里(占画面 2% 左右) 常失败 瓦片候选区域 + 细角度扫描后可读
反向读取时的识别框 可能被拉成对角线 已修正

解码结果(内容、格式、Position、Rotation)语义与官方一致;rotation() 折叠在 (-90°, 90°] (例如同一条轴会报 -64° 而不是 118°,两者等价)。

用法(核心 API)

与官方一致。注意用 ZXingCpp.ImageFormat 全限定,避免和 System.Drawing.Imaging.ImageFormat 歧义:

cs 复制代码
using System.Drawing;
using System.Drawing.Imaging;
using ZXingCpp;

using var bmp = new Bitmap(path);
var data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
                        ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
var iv = new ImageView(data.Scan0, bmp.Width, bmp.Height, ZXingCpp.ImageFormat.BGRA, data.Stride);
var reader = new BarcodeReader { TryHarder = true, TryRotate = true, TryInvert = true };
foreach (var bc in reader.From(iv))
    Console.WriteLine($"{bc.Format} {bc.Text} rot={bc.Orientation}°");   // 0~359 官方方向角

Barcode.Orientation 是 0~359 的方向角;Barcode.Rotation 是本包把它折叠到 (-90°, 90°] 的便捷值(同一条轴会报 -64° 而不是 118°,两者等价)。

注意:把图片画到新的 32bpp 位图上时用 DrawImage(src, 0, 0, src.Width, src.Height)(显式宽高), 否则 GDI+ 会按 DPI 缩放,条码被缩小后可能解不出。

增强托管 API(与 demo 一致)

本包内置了 demo 程序使用的一站式封装(using ZXingAngleDemo;),读整图、直接拿结果:

复制代码
using System.Drawing;
using ZXingCpp;
using ZXingAngleDemo;   // BarcodeScanner / ScanResult / BarcodeInfo

using var bmp = new Bitmap(path);
using var opts = BarcodeScanner.MakeOptions(tryHarder: true, tryRotate: true, tryInvert: true,
                                            tryDownscale: true, formatPreset: "全部", minLineCount: 2, maxSymbols: 0);
var r = BarcodeScanner.Scan(bmp, opts);
foreach (var b in r.Barcodes)
    Console.WriteLine($"{b.Format} \"{b.Text}\" rot={b.Rotation}° lines={b.LineCount} pts={b.Points.Length}");

BarcodeScanner 还提供 MakeOptions、PresetFormats、Rotate(Bitmap, angle, color)、Ensure32bpp。

平台

本包托管层面向 net8.0-windows ,原生库只带 win-x64 (runtimes/win-x64/native/ZXing.dll)。其它平台请自行用修改后的源码编译 (-DBUILD_SHARED_LIBS=ON -DZXING_C_API=ON)。

许可与声明

Apache-2.0,见随包的 LICENSE。对上游源码的修改清单见 NOTICE.md(Apache-2.0 §4(b) 要求声明修改)。 本项目与 zxing-cpp 作者无关,也未获其背书;问题请提到本包仓库,不要打扰上游。

相关推荐
郝学胜-神的一滴18 小时前
Effective STL 条款09:慎重选择删除元素的方法
开发语言·网络·数据结构·c++·算法·stl
geovindu1 天前
python:Logistic Regression
开发语言·后端·python·算法
摇滚侠1 天前
《On Java 中文版 基础卷》阅读笔记 内部类 21
java·开发语言·笔记
番茄味城市甜菜1 天前
二叉树刷题(C语言)
c语言·开发语言
布莱克6051 天前
Swift 自动引用计数(ARC)详解:原理、循环引用与最佳实践
开发语言·ios·swift·arc·自动引用计数
FungLeo1 天前
FengCMS·PHP+MySQL 升级实录 ·本地容器环境设计:只读挂载、LOCAL_UID、数据卷权限
开发语言·mysql·docker·php·fengcms
geovindu1 天前
python: Random Forest
开发语言·后端·python·算法·随机森林
真上帝的左手1 天前
2. 编程语言-Python-Anaconda
开发语言·python
.道阻且长.1 天前
Linux基础IO:理解文件以及回顾C文件接口
linux·运维·服务器·c语言·开发语言·职场和发展