使用IRONBARCODE

如何在 C# Windows 应用程序中使用 BarCode 扫描仪

乔尔迪·巴尔迪亚
乔尔迪·巴尔迪亚
2022年五月4日
更新 2024年一月20日
分享:

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

使用 IronBarcode 库,可以同时扫描和读取多个条形码,还能成功扫描不完美的图像。 首先让我们明确一下什么是 BarCode 扫描仪。

什么是 BarCode 扫描仪?

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

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

如何在 C# 中读取 BarCode;

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

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

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

创建项目后,在 Visual Studio 工具箱中设计如下表单:PictureBox、Label、TextBox 和 Button 控件。

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

条形码扫描器

2.在 C# 中安装 BarCode .NET 库;

可以使用以下三种方法之一安装 BarCode Library:

1.软件包管理器控制台

在软件包管理器控制台中编写以下命令。 它将为您下载和安装软件包。

Install-Package BarCode

2.NuGet 软件包管理器解决方案

您也可以使用 NuGet 软件包解决方案安装 BarCode Library。 只需按照以下步骤操作:

点击工具 > NuGet 包管理器 > 为解决方案管理 NuGet 包

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

3.从链接下载

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

下载后,将以下参考资料添加到您的 BarCode 阅读器项目中。

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

3.读取任何 BarCode 或 QR 码

在 .NET 中使用 .NET Barcode Reader 和 IronBarcode 库读取条形码或二维码非常简单。

BarCode 扫描仪

在您的项目中浏览图片,您希望阅读。 它将在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 picture box
    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 picture box
    pictureBox1.Image = new Bitmap(open.FileName); 
    // store image file path in class data member. Initialize it as string ImageFileName;
    ImageFileName = open.FileName; 
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

扫描代码 "按钮的代码:

BarcodeResult Result = BarcodeReader.Read(ImageFileName);
textBox1.Text = Result.Text;
BarcodeResult Result = BarcodeReader.Read(ImageFileName);
textBox1.Text = Result.Text;
Dim Result As BarcodeResult = BarcodeReader.Read(ImageFileName)
textBox1.Text = Result.Text
$vbLabelText   $csharpLabel

条形码扫描仪在文本框中显示的条形码数据如下:

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

要使用 C# 扫描的条形码图像

QR 码扫描器

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

// Choose which filters are to be applied (in order);
var filtersToApply = new ImageFilterCollection() {
    new SharpenFilter(),
    new InvertFilter(),
    new ContrastFilter(),
    new BrightnessFilter(),
    new AdaptiveThresholdFilter(),
    new BinaryThresholdFilter()
};

BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
    // Set chosen filters in BarcodeReaderOptions:
    ImageFilters = filtersToApply,

    ExpectBarcodeTypes = BarcodeEncoding.QRCode 
 BarcodeEncoding.Code128,
};
BarcodeResult Result = BarcodeReader.Read(ImageFileName, myOptionsExample);
textBox1.Text = Result.Text;
// Choose which filters are to be applied (in order);
var filtersToApply = new ImageFilterCollection() {
    new SharpenFilter(),
    new InvertFilter(),
    new ContrastFilter(),
    new BrightnessFilter(),
    new AdaptiveThresholdFilter(),
    new BinaryThresholdFilter()
};

BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
    // Set chosen filters in BarcodeReaderOptions:
    ImageFilters = filtersToApply,

    ExpectBarcodeTypes = BarcodeEncoding.QRCode 
 BarcodeEncoding.Code128,
};
BarcodeResult Result = BarcodeReader.Read(ImageFileName, myOptionsExample);
textBox1.Text = Result.Text;
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

打开倾斜的二维码图像后,输出结果如下:

如何在C# Windows应用程序中使用条形码扫描器,图4:倾斜的二维码图像

倾斜的二维码图像

一次扫描读取多个条形码

PDF 文档

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

// Multiple barcodes may be scanned up from a single document or image.  A PDF document may also used as the input 
imagePagedBarcodeResult [] PDFResults = BarcodeReader.ReadPdf("MultipleBarcodes.pdf");
// Work with the results
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);
}
// Multiple barcodes may be scanned up from a single document or image.  A PDF document may also used as the input 
imagePagedBarcodeResult [] PDFResults = BarcodeReader.ReadPdf("MultipleBarcodes.pdf");
// Work with the results
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);
}
' Multiple barcodes may be scanned up from a single document or image.  A PDF document may also used as the input 
Dim PDFResults() As imagePagedBarcodeResult = BarcodeReader.ReadPdf("MultipleBarcodes.pdf")
' Work with the results
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 文件中出现的 BarCode 和 QR 码:

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

C# - 从 PDF 读取条形码的结果

从不规则图像中读取 BarCode

在实际使用案例中,经常会发现图像、扫描件、缩略图或照片中的 BarCode 有瑕疵,可能包含数字噪音或有偏差。 本节演示如何从缩略图中读取 BarCode 数据。

缩略图

IronBarcode库使用C# Barcode Generator,甚至能够读取损坏的条码缩略图。

![如何在C# Windows应用程序中使用条码扫描器,图5:自动条码缩略图大小校正。] 使用 IronBarcode 在 C# 中可读取文件](/static-assets/barcode/blog/how-to-use-barcode-scanners-in-csharp-windows-application/how-to-use-barcode-scanners-in-csharp-windows-application_5.webp)

Automatic barcode thumbnail size correction. 文件可通过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 是一个多功能的 .NET 软件库和 C# QR 码生成器,用于扫描和读取多种条形码图像格式,无论这些条形码是完美的屏幕截图,还是实际上的照片、扫描或其他不完美的真实世界图像。 此外,IronBarcode 提供了多种自定义选项来提高条形码读取速度,例如裁剪区域多线程,以及ML 模型的准确性。 访问官方文档页面以获取有关IronBarcode的更多信息。

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

乔尔迪·巴尔迪亚
乔尔迪·巴尔迪亚
软件工程师
Jordi 最擅长 Python、C# 和 C++,当他不在 Iron Software 运用技能时,他会进行游戏编程。作为产品测试、产品开发和研究的负责人之一,Jordi 为持续的产品改进增添了极大的价值。多样化的经验让他充满挑战和参与感,他说这是他在 Iron Software 工作中最喜欢的方面之一。Jordi 在佛罗里达州迈阿密长大,并在佛罗里达大学学习计算机科学和统计学。
< 前一页
条形码生成器 .NET 教程
下一步 >
如何在ASP.NET中使用C#生成条形码