跳至页脚内容
USING IRONBARCODE
如何在 C# Windows 应用程序中读取条码扫描器

How to Use Barcode Scanners in C# Windows Apps

本教程将演示如何在C#控制台应用程序和.NET Windows窗体应用程序中使用IronBarcode库扫描QR码和条形码。

使用IronBarcode库,可以同时扫描和读取多个条形码,还可以成功扫描不完美的图像。 首先,我们来澄清一下什么是条码扫描器。

什么是条码扫描器?

条码是由一系列平行的黑线和宽度不等的白色间隔组成的方形或长方形图像。 条码扫描器或条码阅读器是一种能够读取印刷条码、解码条码中包含的数据并将数据发送到计算机的设备。

以下步骤将介绍如何借助IronBarcode库创建一个条码扫描器。

如何在C#中读取条码

  • 在Microsoft Visual Studio中创建一个.NET Windows窗体应用程序项目
  • 安装条形码库
  • 读取任何条码或QR码
  • 在一次扫描中读取多个条码或QR码
  • 允许IronBarcode从不完美的扫描和照片中读取

1. 在Microsoft Visual Studio中创建一个Windows窗体应用程序

打开Visual Studio > 点击创建新项目 > 选择Windows窗体应用程序模板 > 按下一步 > 命名项目 > 按下一步 > 选择目标.NET Framework > 点击创建按钮。

创建项目后,从Visual Studio工具箱按如下设计窗体:PictureBox、Label、TextBox和Button控件。

在C# Windows应用程序中使用条码扫描器,图1:条码扫描器 条码扫描器

2. 在C#中安装条码.NET库

可以通过以下三种方法之一安装条码库:

1. 程序包管理器控制台

在包管理器控制台中写下以下命令。 它将为您下载并安装程序包。

Install-Package BarCode

2. NuGet包管理器解决方案

您还可以通过使用NuGet包解决方案来安装条码库。 只需按照这些步骤操作:

点击工具 > NuGet程序包管理器 > 管理解决方案的NuGet程序包

这将为您打开NuGet包管理器。 点击浏览并搜索条码,然后安装类库。

3. 从链接下载

作为替代方案,可以下载IronBarCode.Dll并将其添加为项目的引用。

下载后,将以下引用添加到您的条码阅读器项目中。

using IronBarCode;
using IronBarCode;
Imports IronBarCode
$vbLabelText   $csharpLabel

3. 阅读任何条码或QR码

在.NET中使用IronBarcode库读取条码或QR码非常简单,.NET条码阅读器

条码扫描器

在您的项目中,浏览您希望读取的图像。 它将打开在PictureBox中; 立即点击"扫描代码"。 文本将出现在文本框中。

这是"浏览"按钮以打开图像的代码:

// Open file dialog   
OpenFileDialog open = new OpenFileDialog();
// Image filters  
open.Filter = "Image Files(*.jpg; *.png; *.jpeg; *.gif; *.bmp)|*.jpg;*.png;*.jpeg;*.gif;*.bmp";
if (open.ShowDialog() == DialogResult.OK) {  
    // Display image in PictureBox
    pictureBox1.Image = new Bitmap(open.FileName); 
    // Store image file path in class data member. Initialize it as string ImageFileName;
    ImageFileName = open.FileName; 
}
// Open file dialog   
OpenFileDialog open = new OpenFileDialog();
// Image filters  
open.Filter = "Image Files(*.jpg; *.png; *.jpeg; *.gif; *.bmp)|*.jpg;*.png;*.jpeg;*.gif;*.bmp";
if (open.ShowDialog() == DialogResult.OK) {  
    // Display image in PictureBox
    pictureBox1.Image = new Bitmap(open.FileName); 
    // Store image file path in class data member. Initialize it as string ImageFileName;
    ImageFileName = open.FileName; 
}
' Open file dialog   
Dim open As New OpenFileDialog()
' Image filters  
open.Filter = "Image Files(*.jpg; *.png; *.jpeg; *.gif; *.bmp)|*.jpg;*.png;*.jpeg;*.gif;*.bmp"
If open.ShowDialog() = DialogResult.OK Then
	' Display image in PictureBox
	pictureBox1.Image = New Bitmap(open.FileName)
	' Store image file path in class data member. Initialize it as string ImageFileName;
	ImageFileName = open.FileName
End If
$vbLabelText   $csharpLabel

"扫描代码"按钮的代码:

// Read the barcode from the image file path
BarcodeResult Result = BarcodeReader.Read(ImageFileName);
// Display the decoded text in TextBox
textBox1.Text = Result.Text;
// Read the barcode from the image file path
BarcodeResult Result = BarcodeReader.Read(ImageFileName);
// Display the decoded text in TextBox
textBox1.Text = Result.Text;
' Read the barcode from the image file path
Dim Result As BarcodeResult = BarcodeReader.Read(ImageFileName)
' Display the decoded text in TextBox
textBox1.Text = Result.Text
$vbLabelText   $csharpLabel

条码扫描器按如下图在文本框中显示条码数据:

在C# Windows应用程序中使用条码扫描器,图2:要扫描的C#条码图像 要使用C#扫描的条码图像

QR码扫描器

在本节中,IronBarcode库能够有效处理涉及倾斜QR码的实际情况。 尽管倾斜角度的QR码可以由Read方法处理和读取,但解析可能需要更多时间。 IronBarcode库提供了一种使用BarcodeReaderOptions作为额外参数来处理此类图像输入的自定义方法。 代码如下

// Define a collection of image filters to apply
var filtersToApply = new ImageFilterCollection() {
    new SharpenFilter(),
    new InvertFilter(),
    new ContrastFilter(),
    new BrightnessFilter(),
    new AdaptiveThresholdFilter(),
    new BinaryThresholdFilter()
};

// Configure barcode reader options with specified filters
BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions() {
    ImageFilters = filtersToApply,
    ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128,
};

// Read the barcode/QR code with custom options and display result
BarcodeResult Result = BarcodeReader.Read(ImageFileName, myOptionsExample);
textBox1.Text = Result.Text;
// Define a collection of image filters to apply
var filtersToApply = new ImageFilterCollection() {
    new SharpenFilter(),
    new InvertFilter(),
    new ContrastFilter(),
    new BrightnessFilter(),
    new AdaptiveThresholdFilter(),
    new BinaryThresholdFilter()
};

// Configure barcode reader options with specified filters
BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions() {
    ImageFilters = filtersToApply,
    ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128,
};

// Read the barcode/QR code with custom options and display result
BarcodeResult Result = BarcodeReader.Read(ImageFileName, myOptionsExample);
textBox1.Text = Result.Text;
' Define a collection of image filters to apply
Dim filtersToApply = New ImageFilterCollection() From {
	New SharpenFilter(),
	New InvertFilter(),
	New ContrastFilter(),
	New BrightnessFilter(),
	New AdaptiveThresholdFilter(),
	New BinaryThresholdFilter()
}

' Configure barcode reader options with specified filters
Dim myOptionsExample As New BarcodeReaderOptions() With {
	.ImageFilters = filtersToApply,
	.ExpectBarcodeTypes = BarcodeEncoding.QRCode Or BarcodeEncoding.Code128
}

' Read the barcode/QR code with custom options and display result
Dim Result As BarcodeResult = BarcodeReader.Read(ImageFileName, myOptionsExample)
textBox1.Text = Result.Text
$vbLabelText   $csharpLabel

打开倾斜QR码图像后的输出如下所示:

在C# Windows应用程序中使用条码扫描器,图4:倾斜QR码图像 倾斜QR码图像

在一次扫描中读取多个条码

PDF文档

条码图像可以从PDF文件中扫描,并可以根据需要显示每个结果。 以下示例代码允许您从PDF文件中读取多个条码。

// Scan for multiple barcodes within a PDF document
BarcodeResult[] PDFResults = BarcodeReader.ReadPdf("MultipleBarcodes.pdf");

// Work with the results found
foreach (var PageResult in PDFResults) { 
    string Value = PageResult.Value;
    int PageNum = PageResult.PageNumber;
    System.Drawing.Bitmap Img = PageResult.BarcodeImage;
    BarcodeEncoding BarcodeType = PageResult.BarcodeType;
    byte[] Binary = PageResult.BinaryValue;
    Console.WriteLine(PageResult.Value + " on page " + PageNum);
}
// Scan for multiple barcodes within a PDF document
BarcodeResult[] PDFResults = BarcodeReader.ReadPdf("MultipleBarcodes.pdf");

// Work with the results found
foreach (var PageResult in PDFResults) { 
    string Value = PageResult.Value;
    int PageNum = PageResult.PageNumber;
    System.Drawing.Bitmap Img = PageResult.BarcodeImage;
    BarcodeEncoding BarcodeType = PageResult.BarcodeType;
    byte[] Binary = PageResult.BinaryValue;
    Console.WriteLine(PageResult.Value + " on page " + PageNum);
}
' Scan for multiple barcodes within a PDF document
Dim PDFResults() As BarcodeResult = BarcodeReader.ReadPdf("MultipleBarcodes.pdf")

' Work with the results found
For Each PageResult In PDFResults
	Dim Value As String = PageResult.Value
	Dim PageNum As Integer = PageResult.PageNumber
	Dim Img As System.Drawing.Bitmap = PageResult.BarcodeImage
	Dim BarcodeType As BarcodeEncoding = PageResult.BarcodeType
	Dim Binary() As Byte = PageResult.BinaryValue
	Console.WriteLine(PageResult.Value & " on page " & PageNum)
Next PageResult
$vbLabelText   $csharpLabel

PDF文件中存在的条码和QR码:

在C# Windows应用程序中使用条码扫描器,图3:C# - 从PDF读取条码结果 C# - 从PDF读取条码结果

从不完美的图像中读取条码

在现实世界的使用场景中,条码通常会在图像、扫描、缩略图或照片中发现瑕疵,并可能包含数字噪声或被倾斜。 本节演示如何从缩略图中读取条码数据。

缩略图

IronBarcode库使用C#条码生成器,甚至可以读取条码的损坏缩略图。

在C# Windows应用程序中使用条码扫描器,图5:自动条码缩略图尺寸修正。 使用IronBarcode在C#中可读文件 自动条码缩略图尺寸修正。 使用IronBarcode在C#中可读文件

它会自动检测到过小的条码图像,无法合理代表实际条码,然后放大并清除与缩略图相关的所有数字噪声,从而使其再次可读。

// Small or 'Thumbnail' barcode images are automatically detected by IronBarCode and corrected for wherever possible even if they have much digital noise.
BarcodeResult SmallResult = BarcodeReader.Read("ThumbnailOfBarcode.gif");
// Small or 'Thumbnail' barcode images are automatically detected by IronBarCode and corrected for wherever possible even if they have much digital noise.
BarcodeResult SmallResult = BarcodeReader.Read("ThumbnailOfBarcode.gif");
' Small or 'Thumbnail' barcode images are automatically detected by IronBarCode and corrected for wherever possible even if they have much digital noise.
Dim SmallResult As BarcodeResult = BarcodeReader.Read("ThumbnailOfBarcode.gif")
$vbLabelText   $csharpLabel

摘要

IronBarcode is a versatile .NET software library and C# QR Code Generator for scanning and reading a wide range of barcode image formats, and it can do so whether or not these barcodes are perfect screen grabs or are in fact photographs, scans, or other imperfect real-world images. Additionally, IronBarcode offers a wide range of customization options to improve barcode reading speed, such as crop regions or multi-threading, and the accuracy of the ML model. 访问官方文档页面以获取有关IronBarcode的更多信息。

当前,如果您购买完整的Iron Suite,可以以购买两个库的价格获得五个库。

常见问题解答

在 C# 应用程序的上下文中,条码扫描器是什么?

条码扫描器是一种读取打印条码、解码信息并将其发送到计算机的设备。在 C# 应用程序中,这种功能可以通过像 IronBarcode 这样的库来实现。

如何使用 C# 创建用于条码扫描的 Windows 窗体应用程序?

要在 C# 中创建用于条码扫描的 Windows 窗体应用程序,打开 Visual Studio,使用“Windows 窗体应用程序模板”创建一个新项目,配置目标 .NET Framework,并使用 PictureBox、Label、TextBox 和 Button 等控件设计窗体。

推荐的在 C# 项目中安装条码库的方法是什么?

可以通过包管理器控制台使用 Install-Package IronBarCode、通过 NuGet 包管理器,或下载 DLL 并将其添加为引用在 C# 项目中安装条码库,比如 IronBarcode。

能否使用 C# 库在一次扫描中读取多个条码?

是的,使用 IronBarcode,可以通过 BarcodeReader.ReadPdf 方法在一次扫描中读取多个条码,甚至从 PDF 文档中。

库是如何管理从低质量图像中读取条码的?

IronBarcode 通过应用图像过滤器和放大技术来减少数字噪声,从而确保准确读取低质量图像中的条码。

C# 库如 IronBarcode 支持哪些条码格式?

IronBarcode 支持多种条码格式,包括 QR 码和 Code128。即使图像不完美或使用相机拍摄它也能读取这些格式。

在 .NET 应用程序中实现条码读取的步骤是什么?

要实现条码读取,将图像加载到 PictureBox 中,触发“扫描代码”操作,并使用 IronBarcode 处理并在 TextBox 中显示解码后的文本。

IronBarcode 能否有效处理歪斜或倾斜的 QR 码?

是的,IronBarcode 可以通过使用 BarcodeReaderOptions 应用必要的图像滤镜和调整来准确读取歪斜的 QR 码。

IronBarcode 为条码读取提供了哪些定制化功能?

IronBarcode 提供裁剪区域、多线程和参数调整等功能,以提升条码读取速度和准确性。

在哪里可以找到更多关于在 C# 中使用条码库的详细信息?

欲了解在 C# 中使用条码库的更多详细信息,可以访问 Iron Software 网站上的官方文档页面。

Jordi Bardia
软件工程师
Jordi 最擅长 Python、C# 和 C++,当他不在 Iron Software 利用这些技能时,他就在游戏编程。分享产品测试、产品开发和研究的责任,Jordi 在持续的产品改进中增加了巨大的价值。多样的经验使他面临挑战并保持投入,他表示这是在 Iron Software 工作的最喜欢的方面之一。Jordi 在佛罗里达州迈阿密长大,并在佛罗里达大学学习计算机科学和统计学。