使用 IRONBARCODE

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

已更新 2024年1月20日
分享:

本教程將展示如何在 C# 控制台應用程序和 .NET Windows Forms 應用程序中使用 IronBarcode 庫來掃描 QR 碼和條碼。

使用 IronBarcode 庫,可以同時掃描和讀取多個條碼,並且它還可以成功地掃描不完美的影像。我們首先來澄清一下什麼是條碼掃描器。

條碼掃描器是什麼?

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

以下步驟將介紹如何使用IronBarcode庫創建條碼掃描器。

如何在 C# 中讀取條碼

  • 在 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. 安裝 Barcode .NET Library 到 C

Barcode Library 可以通過以下三種方法之一來安裝:

1. 套件管理器主控台

在套件管理器主控台中輸入以下命令。它將為您下載並安裝套件。

Install-Package BarCode

2. NuGet 套件管理器解決方案

你也可以使用 NuGet 套件解決方案安裝 Barcode Library。只需按照以下步驟:

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

這會為你打開 NuGet 套件管理器。點擊瀏覽並搜索 Barcode,然後安裝該類庫。

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 Code Scanner

在本節中,IronBarcode 庫能有效處理涉及傾斜 QR Code 的現實情況。雖然傾斜角度的 QR Code 可以被處理和讀取 讀取 方法,但它可能需要更多時間來解決。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
VB   C#

輸出將會如下所示,打開歪斜的 QR 碼圖像後:

如何在C# Windows應用程式中使用條碼掃描器,圖4:傾斜的二維碼圖片

傾斜的二維碼圖片

在一次掃描中讀取多個條碼

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 Code:

如何在 C# Windows 應用程式中使用條碼掃描器, 圖 3:C# - 從 PDF 中讀取條碼結果

C# - 從 PDF 讀取條碼的結果

從不完美的圖像讀取條碼

在現實中的應用場合中,條碼通常會在圖像、掃描、縮略圖或照片中出現不完美的情況,可能包含數字噪音或變形。本節展示如何從縮略圖讀取條碼數據。

縮略圖

IronBarcode Library 使用 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")
VB   C#

总结

IronBarcode 是一个多功能的 .NET 软件库 C# QR碼生成器 用於掃描和讀取各種條形碼圖像格式,無論這些條形碼是完美的截圖還是實際照片、掃描或其他。 不完美的真實世界圖像此外,IronBarcode 提供了多種自訂選項來提高條碼讀取速度,例如 裁切區域多線程,和 機器學習模型的準確性。訪問 官方文件頁面 欲了解更多關於IronBarcode的資訊。

目前,如果您購買 完整的Iron Suite您可以以兩套價格獲得五個庫。

< 上一頁
條碼生成器 .NET 教程
下一個 >
如何在 ASP.NET 中使用 C# 生成條碼

準備開始了嗎? 版本: 2024.10 剛剛發布

免費 NuGet 下載 總下載次數: 1,203,227 查看許可證 >