A Comparison Between ZXing Decoder & IronBarcode
条形码提供了一种清晰且机器可读的数据呈现方式。 最初,条形码由宽度和间距各异的平行线组成,用于表示数据。 这些传统的线性或一维(1D)条形码可以通过称为条形码阅读器的专用光学设备进行扫描。 然而,条形码的发展催生了二维(2D)条形码,也称为矩阵码。 与传统条形码不同,二维条形码使用矩形、点和六边形等图案而不是条形。 要读取这些二维条形码,可以使用专门的光学扫描仪装置,或者也可以使用连接到运行解码软件的计算机的数码相机。 此外,智能手机等移动设备可以利用其内置摄像头和专用应用程序作为二维条形码扫描器。
ZXing条码扫描器
Zebra Crossing(通常被称为 ZXing)是一个开源的多格式 1D/2D 条形码图像处理工具包,它使用 Java 开发,并有其他语言的移植版本。 核心图像解码库、Java 特定客户端代码和 Android 客户端条形码扫描器只是构成 ZXing 的几个模块。 许多其他独立的开源项目都是基于它构建的。
1. 特点
它可以跟踪网址、联系信息、日历事件等等。 它是专为 Java SE 应用程序而设计的。 通过实现目标,条形码扫描器集成成为可能。 这是一个简单的谷歌眼镜应用程序。
2. 将 ZXing 与 .NET 结合使用
打开 Visual Studio,从文件菜单中选择"新建项目",然后选择"控制台应用程序"。 本文中,我们将选择 C# 控制台应用程序。

请在相应的文本框中输入项目名称和文件路径。 接下来,单击"创建"按钮选择所需的 .NET Framework。
如果您选择的是控制台应用程序,项目现在将创建其结构并打开 program.cs 文件,允许您输入程序代码并构建或执行它。

2.1 安装 ZXing 条码
在 NuGet 包管理器控制台中输入以下命令以安装 ZXing 库:
或者,您可以使用 NuGet 包管理器工具获取该包。 如图所示。 尝试安装你选择的第一个结果。

2.2 使用 ZXing 读取和写入条形码
我们可以使用以下示例代码创建条形码。 ZXing 允许我们创建 10 多种条形码格式。
using ZXing.Windows.Compatibility;
var options = new QrCodeEncodingOptions
{
Width = 250,
Height = 250,
};
var writer = new BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
writer.Options = options;
// Encode the string into a QR code bitmap image
System.Drawing.Bitmap _bitmap = writer.Write("Hello world");
// Save the bitmap as a PNG file
_bitmap.Save("Demo1.png");Imports ZXing.Windows.Compatibility
Private options = New QrCodeEncodingOptions With {
.Width = 250,
.Height = 250
}
Private writer = New BarcodeWriter()
writer.Format = BarcodeFormat.QR_CODE
writer.Options = options
' Encode the string into a QR code bitmap image
Dim _bitmap As System.Drawing.Bitmap = writer.Write("Hello world")
' Save the bitmap as a PNG file
_bitmap.Save("Demo1.png")上面的代码设置了QrCodeEncodingOptions的高度和宽度。 然后它创建了BarcodeWriter的实例。 对于BarcodeWriter,我们将条形码格式设置为QR码。 我们将之前创建的二维码选项分配给编写者。 BarcodeWriter中将给定字符串编码为条形码并将其作为位图图像返回。 图像使用位图的Save方法保存。以下是代码的结果。

下一个代码示例演示了如何使用 ZXing 解码条形码。
using ZXing.Windows.Compatibility;
// Load the barcode image into a bitmap
var barcodeBitmap = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile("demo.png");
// Create a BarcodeReader object
var reader = new BarcodeReader();
// Decode the bitmap into a result
var result = reader.Decode(barcodeBitmap);
if (result != null)
{
// Output the decoded text to the console
Console.WriteLine(result.Text);
Console.ReadKey();
}Imports ZXing.Windows.Compatibility
' Load the barcode image into a bitmap
Private barcodeBitmap = CType(System.Drawing.Bitmap.FromFile("demo.png"), System.Drawing.Bitmap)
' Create a BarcodeReader object
Private reader = New BarcodeReader()
' Decode the bitmap into a result
Private result = reader.Decode(barcodeBitmap)
If result IsNot Nothing Then
' Output the decoded text to the console
Console.WriteLine(result.Text)
Console.ReadKey()
End If在上面的代码中,我们首先将图像加载到位图中,然后创建一个BarcodeReader对象。 Decode函数允许我们将位图作为参数传递,可以以多种格式返回结果。 我们使用Text属性来获取编码在条形码中的文本。

IronBarcode
借助这个条形码库,读取和创建条形码变得非常简单。 使用 IronBarcode 的库可以轻松制作动态条形码。 只需几行代码,这个简单的库就可以生成条形码,这有助于我们对条形码图像进行编码。 IronBarcode 使我们能够使用 C# 和 VB.NET 等语言生成条形码。
1. 特点
IronBarcode 可以读取和写入大多数条形码图像格式和 QR 标准,包括 UPC A/E、Databar、EAN 8/13、MSI、Code 39/93/128、CodaB、RSS 14/Expanded 和 ITF。
- IronBarcode 在扫描扫描和实时视频帧时,可以校正旋转、噪声、失真和倾斜。 为了提高读取准确率和速度,IronBarcode 会在条形码图像创建时自动对其进行预处理。 动态条形码由于允许内容变化,因此经常被使用。 IronBarcode 利用多核多线程的能力对于批处理服务器来说是有利的。
- 在单页和多页文档中,IronBarcode 可以自动找到一个或多个条形码。
2. 使用 IronBarcode
要在解决方案中使用 IronBarcode 库,您必须下载所需的软件包。 为此,请在 NuGet 包管理器控制台中使用以下命令:
或者,您可以使用 NuGet 包管理器,它会显示所有搜索结果,以便查找和下载"条形码"包。 然后您可以从中选择要下载到程序中的必要软件包。

3. 使用 IronBarcode 读取和写入条形码
只需几行代码,我们就可以使用 IronBarcode 库快速制作条形码图像。 此外,它还允许我们将生成的条形码保存为单独的图片文件。以下是使用控制台程序创建条形码标签的 C# 代码示例。
using IronBarCode;
// Create a QR code with a medium error correction level
QRCodeWriter.CreateQrCode("Your text here", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium).SaveAsPng("demo.png");Imports IronBarCode
' Create a QR code with a medium error correction level
QRCodeWriter.CreateQrCode("Your text here", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium).SaveAsPng("demo.png")此代码使用中等级别的纠错生成500x500像素的图形,然后使用SaveAsPng方法将其保存到文件位置。
下一个代码示例读取我们在上一个示例中创建的二维码中编码的文本。
using IronBarCode;
// Load the QR code image into a bitmap
var barcodeBitmap = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile("demo.png");
// Read the barcode image
var reader = IronBarCode.BarcodeReader.Read(barcodeBitmap);
// Output the decoded value to the console
Console.WriteLine(reader.Values[0]);
Console.ReadKey();Imports IronBarCode
' Load the QR code image into a bitmap
Private barcodeBitmap = CType(System.Drawing.Bitmap.FromFile("demo.png"), System.Drawing.Bitmap)
' Read the barcode image
Private reader = IronBarCode.BarcodeReader.Read(barcodeBitmap)
' Output the decoded value to the console
Console.WriteLine(reader.Values(0))
Console.ReadKey()我们首先将图像加载到位图中,然后在Read方法读取图像。 我们在Values属性来获取从QR码读取的内容。
要了解更多关于 ZXing 以及它与 IronBarcode 的比较,请阅读这篇 下一篇博客文章。
我们的"读取条形码"教程还提供了有关如何使用 IronBarcode 读取条形码和二维码的更多信息。 更多代码教程请访问 IronBarcode。
结论
ZXing条码扫描器可以生成高质量的条码,但它已经过时,支持的条码格式也比较少。 此外,它的文档和产品支持也十分有限。
另一方面,IronBarcode 非常高效灵活,能够在多种操作系统上运行。 IronBarcode 可以更改条形码的颜色、大小、间距和字体。 它还支持 Crystal Reports。
开发者可以免费使用 IronBarcode 。 用户可以购买许可证来访问更多功能,并获得一整年的支持和产品更新。

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。