How to Scan Barcodes in ZXing For C# Developers
条形码提供了一种清晰且机器可读的数据呈现方式。 最初,条形码由宽度和间距各异的平行线组成,用于表示数据。 这些传统的线性或一维(1D)条形码可以通过称为条形码阅读器的专用光学设备进行扫描。 然而,条形码的发展催生了二维(2D)条形码,也称为矩阵码。 与传统条形码不同,二维条形码使用矩形、点和六边形等图案而不是条形。 要读取这些二维条形码,可以使用专门的光学扫描仪装置,或者也可以使用连接到运行解码软件的计算机的数码相机。 此外,智能手机等移动设备可以利用其内置摄像头和专用应用程序作为二维条形码扫描器。
ZXing条码扫描器
Zebra Crossing(通常被称为 ZXing)是一个开源的多格式 1D/2D 条形码图像处理工具包,它使用 Java 开发,并有其他语言的移植版本。 核心图像解码库、Java 特定客户端代码和 Android 客户端条形码扫描器只是构成 ZXing 的几个模块。 许多其他独立的开源项目都是基于它构建的。
1. 特点
它可以跟踪网址、联系信息、日历事件等等。 它是专为 Java SE 应用程序而设计的。 通过实现目标,条形码扫描器集成成为可能。 这是一个简单的谷歌眼镜应用程序。
2. 将 ZXing 与 .NET 结合使用
打开 Visual Studio,从文件菜单中选择"新建项目",然后选择"控制台应用程序"。 本文中,我们将选择 C# 控制台应用程序。
如何在 ZXing 中扫描条形码(面向 C# 开发人员)图 1
请在相应的文本框中输入项目名称和文件路径。 接下来,单击"创建"按钮选择所需的 .NET Framework。
如果您选择的是控制台应用程序,项目现在将创建其结构并打开 program.cs 文件,允许您输入程序代码并构建或执行它。
2.1 安装 ZXing 条码
在 NuGet 包管理器控制台中输入以下命令以安装 ZXing 库:
Install-Package ZXing.Net.Bindings.Windows.Compatibility
或者,您可以使用 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");
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 码。 我们将之前创建的二维码选项分配给编写者。 Write 方法在 BarcodeWriter 中将给定的字符串编码成条形码,并将其作为位图图像返回。 图像是使用位图的 Save 方法保存的。以下是代码的运行结果。
如何在 ZXing 中扫描条形码(面向 C# 开发人员)图 4
下一个代码示例演示了如何使用 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();
}
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 包管理器控制台中使用以下命令:
Install-Package BarCode
或者,您可以使用 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");
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")
这段代码使用中等程度的纠错生成 500 x 500 像素的图形,然后使用 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();
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 类上的 BarcodeReader 方法读取图像。 我们使用从 Read 方法返回的 BarcodeResults 对象上的 Values 属性来获取从二维码读取的内容。
要了解更多关于 ZXing 以及它与 IronBarcode 的比较,请阅读这篇 下一篇博客文章。
我们的"读取条形码"教程还提供了有关如何使用 IronBarcode 读取条形码和二维码的更多信息。 更多代码教程请访问 IronBarcode。
结论
ZXing条码扫描器可以生成高质量的条码,但它已经过时,支持的条码格式也比较少。 此外,它的文档和产品支持也十分有限。
另一方面,IronBarcode 非常高效灵活,能够在多种操作系统上运行。 IronBarcode 可以更改条形码的颜色、大小、间距和字体。 它还支持 Crystal Reports。
开发者可以免费使用 IronBarcode 。 用户可以购买许可证来访问更多功能,并获得一整年的支持和产品更新。
[{i:(ZXing 是其各自所有者的注册商标。 本网站与 ZXing 无任何关联,亦未获得 ZXing 的认可或赞助。 所有产品名称、徽标和品牌均为各自所有者的财产。 比较仅供参考,反映撰写时公开可用的信息。)}]
常见问题解答
如何在C#中将HTML转换为PDF?
你可以使用IronPDF的RenderHtmlAsPdf方法将HTML字符串转换为PDF。你还可以使用RenderHtmlFileAsPdf将HTML文件转换为PDF。
IronBarcode和ZXing有什么区别?
与ZXing相比,IronBarcode提供更大的灵活性,并支持更多的条形码格式。虽然ZXing对创建条形码有效,但在格式支持和文档方面有限。IronBarcode在效率上表现出色,并兼容各种操作系统。
如何使用移动设备扫描条码?
通过ZXing Android客户端条码扫描器,您可以利用设备的相机扫描条码。对于更强大的功能,可以将IronBarcode集成到移动应用中,以增强条码扫描能力。
IronBarcode支持2D条形码吗?
是的,IronBarcode支持1D和2D条形码,包括二维码,实现了多样化的条形码读取和创建。
IronBarcode可以处理动态条形码创建吗?
IronBarcode支持动态条形码创建,允许您根据特定要求自定义条形码的颜色、大小、间距和文字。
将IronBarcode集成到.NET项目中需要什么?
要将IronBarcode集成到.NET项目中,请通过NuGet包管理控制台使用Install-Package IronBarcode安装包或在NuGet包管理器中找到它。
使用IronBarcode是否有费用?
IronBarcode提供免费试用,但购买许可证可以获得额外功能、产品更新和一年的支持。
IronBarcode可用于从视频帧中读取条码吗?
可以,IronBarcode可以处理实时视频帧,校正旋转、噪声、失真和倾斜,从而提高条码扫描的准确性和速度。
ZXing用于条形码扫描的功能是什么?
ZXing提供了开源的条形码扫描工具包,特别是针对1D和2D条形码。它包括一个核心图像解码库和一个Android客户端条码扫描器。
IronBarcode支持哪些编程语言?
IronBarcode支持C#和VB.NET,非常适合使用.NET框架的开发人员。

