-
C#条码识别的解决方案(ZBar)
简介#
主流的识别库主要有ZXing.NET和ZBar,OpenCV 4.0后加入了QR码检测和解码功能。本文使用的是ZBar,同等条件下ZBar识别率更高,图片和部分代码参考在C#中使用ZBar识别条形码。
使用ZBar#
通过NuGet安装ZBar,必须使用1.0.0版本,最新的1.0.2版本无法自动生成相关的dll并且使用不了1.0.0版的dll。
库默认支持netcoreapp3.1,在.NET6环境下也能正常使用,正常情况下输出目录会自动生成lib文件夹和dll文件。
注:ZBar 1.0.0在x86平台下可正常运行,但Debug会报错,建议使用x64或AnyCPU。
条码识别:
/// <summary>
/// 条码识别
/// </summary>
static List<ZBar.Symbol> ScanBarCode(string filename)
{
var bitmap = (Bitmap)Image.FromFile(filename);
bitmap = MakeGrayscale3(bitmap);
List<ZBar.Symbol> result = new List<ZBar.Symbol>();
using (var scanner = new ZBar.ImageScanner())
{
var symbols = scanner.Scan(bitmap);
if (symbols != null && symbols.Count > 0)
{
result.AddRange(symbols);
}
}
return result;
}
/// <summary>
/// 处理图片灰度
/// </summary>
static Bitmap MakeGrayscale3(Bitmap original)
{
//create a blank bitmap the same size as original
Bitmap newBitmap = new Bitmap(original.Width, original.Height);
//get a graphics object from the new image
Graphics g = Graphics.FromImage(newBitmap);
//create the grayscale ColorMatrix
System.Drawing.Imaging.ColorMatrix colorMatrix = new System.Drawing.Imaging.ColorMatrix(
new float[][]
{
new float[] {.3f, .3f, .3f, 0, 0},
new float[] {.59f, .59f, .59f, 0, 0},
new float[] {.11f, .11f, .11f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
});
//create some image attributes
ImageAttributes attributes = new ImageAttributes();
//set the color matrix attribute
attributes.SetColorMatrix(colorMatrix);
//draw the original image on the new image
//using the grayscale color matrix
g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
//dispose the Graphics object
g.Dispose();
return newBitmap;
}
使用方法:
Console.WriteLine(ZBar.ZBar.Version);
var symbols = ScanBarCode("426301-20160127111209879-611759974.jpg");
string result = string.Empty;
symbols.ForEach(s => Console.WriteLine($"条码类型:{s.Type} 条码内容:{s.Data} 条码质量:{s.Quality}"));
Console.ReadKey();
扩展:其它条码识别库#
在C#平台下还有一个ThoughtWorks.QRCode库也支持条码解析,具体效果还没有测试。原始代码最后的版本是在2015年,后面的版本只是将库做了个标准版,按自己的需求选择版本:
- ThoughtWorks.QRCode:原始版本,最后更新时间2015年。
- ThoughtWorks.QRCode.Standard(推荐使用):netstandard2.0版本,修复bug,增加了自动QRCodeVersion功能。
- ThoughtWorks.QRCode.Core:netstandard2.0版本,功能未修复变更。
识别库使用方法参考:C#使用zxing,zbar,thoughtworkQRcode解析二维码,附源代码。
扩展:开源扫码软件#
推荐一个C# WPF 原生开发的在电脑上识别条码的工具软件QrCodeScanner,功能如下:
- 支持四种模式:截图识别 + 摄像头识别 + 本地图片识别 + 作为扫描枪使用
- 支持Zbar和Zxing两种主流引擎
- 支持多码同扫
- 支持Material Design缤纷主题色与暗黑模式
-
独创的扫描枪模式
附件#
- 测试项目、开源备份(提取码: f4gp):https://pan.baidu.com/s/1OxpUv-8ApDWznxtSvbbNWg
出处:https://www.cnblogs.com/timefiles/p/17573031.html
栏目列表
最新更新
求1000阶乘的结果末尾有多少个0
详解MyBatis延迟加载是如何实现的
IDEA 控制台中文乱码4种解决方案
SpringBoot中版本兼容性处理的实现示例
Spring的IOC解决程序耦合的实现
详解Spring多数据源如何切换
Java报错:UnsupportedOperationException in Col
使用Spring Batch实现批处理任务的详细教程
java中怎么将多个音频文件拼接合成一个
SpringBoot整合ES多个精确值查询 terms功能实
数据库审计与智能监控:从日志分析到异
SQL Server 中的数据类型隐式转换问题
SQL Server中T-SQL 数据类型转换详解
sqlserver 数据类型转换小实验
SQL Server数据类型转换方法
SQL Server 2017无法连接到服务器的问题解决
SQLServer地址搜索性能优化
Sql Server查询性能优化之不可小觑的书签查
SQL Server数据库的高性能优化经验总结
SQL SERVER性能优化综述(很好的总结,不要错
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比