使用IRONBARCODE

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

更新 2024年一月20日
分享:

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

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

什么是条形码扫描仪?

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

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

如何在 C&num 中读取条形码;

  • 在 Microsoft Visual Studio 中创建 .NET Windows Forms 应用程序项目
  • 安装条码库
  • 读取任何条形码或 QR 码
  • 一次扫描即可读取多个条形码或 QR 码

  • 允许 IronBarcode 读取不完美的扫描结果和照片

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

打开 Visual Studio > 点击 Create New Project > 选择 Windows Forms Application Template > 按 Next > 给项目命名 > 按 Next > 选择目标 .NET Framework > 点击 Create 按钮。

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

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

条码扫描仪

2.在 C&num 中安装条形码 .NET 库;

可使用以下三种方法之一安装条码库:

1.软件包管理器控制台

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

Install-Package BarCode

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

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

点击 工具 > NuGet软件包管理器 > 管理解决方案的 NuGet 软件包

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

3.从链接下载

作为替代方案,您可以 IronBarCode.Dll 可下载并添加到项目中作为参考。

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

using IronBarCode;
using IronBarCode;
Imports IronBarCode
VB   C#

3.读取任何条形码或 QR 码

使用带有以下功能的 IronBarcode 库,在 .NET 中读取条形码或 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 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
VB   C#

扫描代码 "按钮的代码:

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
VB   C#

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

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

*使用 C## 扫描条形码图像***

QR 码扫描器

在本节中,IronBarcode 库将有效处理现实世界中涉及倾斜 QR 码的情况。虽然斜角 QR 码可以通过 阅读 方法,但解决起来可能需要更多时间。IronBarcode 库提供了一种定制的方法来使用 条码阅读器选项 作为额外参数,以处理此类图像输入。代码如下

// 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
VB   C#

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

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

倾斜的 QrCode 图像

一次扫描读取多个条形码

PDF 文件

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

// 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
VB   C#

PDF 文件中的条形码和 QR 码:

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

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

从不规则图像中读取条形码

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

缩略图

IronBarcode 库使用 C# 条码生成器它甚至能够读取损坏的条形码缩略图。

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

自动校正条形码缩略图尺寸。在 C# 中使用 IronBarcode 读取文件

IronBarcode 是一款功能强大的条形码软件,它能自动检测因太小而无法合理表示实际条形码的条形码图像,然后将其放大并清除所有与缩略图相关的数字噪声,从而使其再次可读。

// 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")
VB   C#

摘要

IronBarcode 是一个多功能的 .NET 软件库和 C#二维码生成器 它可以扫描和读取各种条形码图像格式,无论这些条形码是完美的屏幕截图,还是照片、扫描件或其他格式的图像。 不完美的真实世界图像.此外,IronBarcode 还提供多种自定义选项,以提高条形码读取速度,例如 作物区多线程ML 模型的准确性.访问 正式文件页面 了解有关 IronBarcode 的更多信息。

目前,如果您购买 全套铁艺套件您只需花两个图书馆的钱,就可以得到五个图书馆。

< 前一页
条形码生成器 .NET 教程
下一步 >
如何在ASP.NET中使用C#生成条形码

准备开始了吗? 版本: 2024.8 刚刚发布

免费NuGet下载 总下载量: 1,167,541 查看许可证 >