ZXing 可以说是世界上最受欢迎的开源条形码SDK了。它的库最初是由Java所写,而今已经移植到了C++, .NET, Python, Objective-C, PHP 以及一些其他的编程语言上。正式因为ZXing的免费,许多开发者喜欢用它来开发条形码扫描软件。然而对于一些复杂的商业场景,ZXing并不能完全胜任,特别是多码场景。在本文中,我将一步步展示如何建立一个基于.NET的条形码识别应用,并比较一下开源SDK与商用SDK之间的性能表现。
ZXing vs Dynamsoft Barcode SDK
支持的条形码类型
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 Command Line Barcode Reader
在Visual Studio中建立一个新的控制台应用程序
在菜单栏,依次点击 Tools > NuGet Package Manager > Package Manager Console 来安装Dynamsoft Barcode Reader 和 ZXing .Net。
Install-Package Dynamsoft.DotNet.Barcode
Install-Package ZXing.Net
另外,你需要添加 System.Drawing 框架
获取文件夹中所有的图片:
string[] files = Directory.GetFiles(directory);
加载图片转换为Bitmap:
Bitmap barcodeBitmap = (Bitmap)Image.FromFile(file);
请注意:PDF格式的图片暂时不支持。如果输入格式为PDF会报异常。
用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的表现不仅人如意。而Dynamsoft Barcode Reader 作为一个商用得条形码识别SDK在解多码时,表现出耗时短准确率高的优点。
源码地址:
https://github.com/yushulx/dynamsoft-barcode-reader-vs-zxing