Barcode识别:如何应对多条形码和复杂场景
程序员文章站
2022-07-14 18:05:11
...
越来越多的移动应用内置扫码功能。包括微信在内,有相当一部分app的扫码功能是基于ZXing这个开源库开发的。终端用户的需求比较简单,通常情况下我们只需要扫描一个条形码,比如购物,付款。然而,在商用场景中,往往需要同时处理多个条形码,比如仓储,物流。图像的质量也参差不齐。在多码及复杂场景中,ZXing和商用SDK比较就有明显的劣势。
多条形码场景示例
假设在仓库中摆满了货物,上面都贴上条形码。如果工人拿着扫码枪一个个扫码,非常麻烦。这个时候如果有多码识别,就可以节约大量的时间。
通过Dynamsoft Barcode Reader的在线Barcode应用,可以得到如下结果:
通过C#代码比较开源和商用Barcode SDK的性能差距
SDK下载
- ZXing.Net (开源)
- Dynamsoft Barcode Reader(商用)
支持的条形码类型
- ZXing: UPC-A, UPC-E, EAN-8, EAN-13, Code 39, Code 93, Code 128, ITF, Codabar, MSI, RSS-14 (all variants), QR Code, Data Matrix, Aztec and PDF-417.
- Dynamsoft Barcode Reader: Code 39, Code 93, Code 128, Codabar, EAN-8, EAN-13, UPC-A, UPC-E, Interleaved 2 of 5 (ITF), Industrial 2 of 5 (Code 2 of 5 Industry, Standard 2 of 5, Code 2 of 5), ITF-14, QRCode, DataMatrix, and PDF417.
简单的.NET命令行条码识别工具
打开Visual Studio创建一个命令行应用.
安装ZXing.Net
Install-Package ZXing.Net
添加Dynamsoft.BarcodeReader.dll到References.
另外,勾选 System.Drawing framework.
获取测试集中的所有图片名:
string[] files = Directory.GetFiles(directory);
读取图片到Bitmap:
Bitmap barcodeBitmap = (Bitmap)Image.FromFile(file);
使用ZXing识别多个条形码:
ZXing.MultiFormatReader multiFormatReader = new ZXing.MultiFormatReader();
ZXing.Multi.GenericMultipleBarcodeReader multiBarcodeReader = new ZXing.Multi.GenericMultipleBarcodeReader(multiFormatReader);
LuminanceSource source = new BitmapLuminanceSource(bitmap);
ZXing.BinaryBitmap bBitmap = new ZXing.BinaryBitmap(new HybridBinarizer(source));
ZXing.Result[] zResults = multiBarcodeReader.decodeMultiple(bBitmap);
使用Dynamsoft Barcode Reader识别多个条形码:
Dynamsoft.Barcode.BarcodeReader reader = new Dynamsoft.Barcode.BarcodeReader();
reader.LicenseKeys = "t0068NQAAAJx5X8TaH/zQIy0Mm3HHIypzFTL+DQTIQah1eCiNcZygsi6sFa0cZiJVv+rRTyU29TpFsLA6hWiz+GAlQlGrRRg=";
TextResult[] results = reader.DecodeBitmap(barcodeBitmap, "");
通过Stopwatch查看耗时:
Stopwatch swDBR = Stopwatch.StartNew();
swDBR.Stop();
Console.WriteLine(swDBR.Elapsed.TotalMilliseconds + "ms");
格式化输出结果:
Console.WriteLine("{0, -10}{1, -20}{2, -20}", "DBR", "time: " + swDBR.Elapsed.TotalMilliseconds + "ms", " result count: " + results.Length);
Console.WriteLine("{0, -10}{1, -20}{2, -20}", "ZXing", "time: " + swZXing.Elapsed.TotalMilliseconds + "ms", " result count: " + zResults.Length);
用颜色区分结果:
Console.BackgroundColor = ConsoleColor.Blue;
Console.WriteLine("{0, -10}{1, -20}{2, -20}", "ZXing", "time: " + swZXing.Elapsed.TotalMilliseconds + "ms", " result count: " + zResults.Length);
Console.ResetColor();
Console.BackgroundColor = ConsoleColor.Red;
Console.WriteLine("{0, -10}{1, -20}{2, -20}", "DBR", "time: " + swDBR.Elapsed.TotalMilliseconds + "ms", " result count: " + results.Length);
Console.ResetColor();
测试图片
运行程序
dbr-zxing.exe [image folder]
通过测试发现,ZXing在多码和复杂场景中的表现不太理想。
源码
https://github.com/yushulx/dynamsoft-barcode-reader-vs-zxing