跳過到頁腳內容
使用IRONBARCODE
如何在C# Windows應用程式中讀取條碼掃描器

如何在C# Windows應用程式中使用條碼掃描器

本教程將演示如何在 C# 控制台應用程序和 .NET Windows Forms 應用程序中掃描 QR 碼和條碼,以 IronBarcode 庫為例。

使用 IronBarcode 庫,可以同時掃描和閱讀多個條碼,並且它也可以成功掃描不完美的圖像。 首先讓我們澄清什麼是條碼掃描器。

什麼是條碼掃描器?

條碼 是一個由一系列平行黑線和不同寬度的白色間隔組成的方形或矩形圖像。 條碼掃描器或條碼讀取器是一種可以閱讀印刷條碼、解碼條碼中包含的信息並將數據傳送到電腦的設備。

以下步驟將介紹如何在 IronBarcode 庫的幫助下創建條碼掃描器。

如何在 C&#35 中閱讀條碼

  • 在 Microsoft Visual Studio 中創建 .NET Windows Forms 應用程序專案
  • 安裝條碼程式庫
  • 讀取任何條碼或 QR 碼
  • 在一次掃描中讀取多個條碼或 QR 碼
  • 允許 IronBarcode 從不完美的掃描和照片中讀取

1. 在 Microsoft Visual Studio 中創建 Windows Forms 應用程序

打開 Visual Studio > 點擊 創建新項目 > 選擇 Windows Forms 應用程序模板 > 按 下一步 > 命名專案 > 按 下一步 > 選擇你的目標 .NET Framework > 點擊 創建 按鈕。

創建專案後,從 Visual Studio 工具箱中設計以下控件:PictureBox、Label、TextBox 和 Button 控件。

在 C# Windows 應用程序中如何使用條碼掃描器,圖1:條碼掃描器 條碼掃描器

2. 在 C&#35 中安裝條碼 .NET 庫

條碼庫可以通過以下三種方法之一安裝:

1. 套件管理器控制台

在包管理器控制台中寫入以下命令。 它將為您下載並安裝該包。

Install-Package BarCode

2. NuGet Packages Manager 解決方案

您還可以使用 NuGet Package 解決方案安裝條碼庫。 只需按照以下步驟進行:

點擊工具 > NuGet包管理器 > 管理解決方案的NuGet包

這將為您打開NuGet包管理器。 點擊“瀏覽”並搜索 Barcode,然後安裝類庫。

3. 從鏈接中下載

作為替代方案,可以下載 IronBarCode.Dll 並添加到您的專案中作為參考。

下載後,將下列引用添加到您的條碼讀取器專案中。

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

3. 讀取任何條碼或 QR 碼

在 .NET 中使用 IronBarcode 庫和 .NET Barcode Reader 讀取條碼或 QR 碼非常簡單。

條碼掃描器

在您的專案中,瀏覽您想要讀取的圖像。 它將在 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:从 PDF 读取条码的 C# 结果 从 PDF 读取条码的 C# 结果

从不完美图像读取条码

在现实使用案例中,条码通常存在于图像、扫描、缩略图或照片中不完美,可能含有数字噪点或被倾斜。 本节演示如何从缩略图读取条码数据。

缩略图

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

![在 C# Windows 应用程序中如何使用条码扫描器,图5:自动条码缩略图尺寸校正。 在 C# 中使用 IronBarcode 可读取的文件] 自动条码缩略图尺寸校正 在 C# 中使用 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")
$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 Forms Application?

要使用C#創建一個條碼掃描的Windows Forms Application,請打開Visual Studio,使用「Windows Forms Application Template」創建一個新項目,配置目標.NET框架,並使用PictureBox、Label、TextBox和Button等控件來設計表單。

在C#項目中安裝條碼庫的推薦方法是什麼?

您可以通過Package Manager Console使用Install-Package IronBarCode,通過NuGet Package Manager,或下載DLL並將其作為引用添加到C#項目中來安裝條碼庫如IronBarcode。

使用C#庫是否可以在一次掃描中讀取多個條碼?

是的,使用IronBarcode,您可以使用BarcodeReader.ReadPdf方法在一次掃描中讀取多個條碼,即使是在PDF文檔中。

該庫如何管理從低質量圖像中讀取條碼?

IronBarcode可以通過應用圖像濾鏡和放大技術來減少數字噪音,確保準確讀取條碼。

像IronBarcode這樣的C#庫支持什麼條碼格式?

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 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。