在生產環境中測試,無水印。
在任何需要的地方都能運行。
獲得 30 天的全功能產品。
在幾分鐘內上手運行。
試用產品期間完全訪問我們的支援工程團隊
本教程將演示如何在 C# 控制台應用程式和 .NET Windows Forms 應用程式中掃描 QR 碼和條形碼,以 IronBarcode 庫為例。
使用 IronBarcode 庫,可以同時掃描和讀取多個條碼,並且還能成功掃描不完美的圖像。 首先,我們來釐清什麼是條碼掃描器。
條碼是一種方形或矩形的圖像,由一系列平行的黑線和寬度不一的白色空隙組成。 條碼掃描器或條碼閱讀器是一種可以讀取印刷條碼、解碼條碼中包含的數據並將數據發送到計算機的設備。
以下步驟將介紹如何使用IronBarcode庫創建條形碼掃描器。
開啟 Visual Studio > 點選 建立新專案 > 選擇 Windows Forms 應用程式範本 > 按下 下一步 > 命名專案 > 按下 下一步 > 選擇您的目標 .NET Framework > 點擊 建立 按鈕。
創建專案後,從 Visual Studio 工具箱設計表單如下:PictureBox、Label、TextBox 和 Button 控件。
條碼掃描器
可以使用以下三種方法之一安裝 Barcode Library:
在套件管理器控制台中輸入以下命令。 它將為您下載並安裝套件。
Install-Package BarCode
您也可以使用 NuGet 套件方案安裝 Barcode Library。 只需遵循以下步驟:
按一下工具 > NuGet 套件管理員 > 管理方案的 NuGet 套件。
這將為您開啟 NuGet 套件管理器。 單擊“瀏覽”並搜尋 Barcode,然後安裝類別庫。
作為替代,您可以下載IronBarCode.Dll,並將其作為參考添加到您的專案中。
下載後,將以下引用新增到您的條碼掃描器專案中。
using IronBarCode;
using IronBarCode;
Imports IronBarCode
使用 IronBarcode 庫搭配 .NET Barcode Reader 在 .NET 中讀取條碼或 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 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
「掃描碼」按鈕的程式碼:
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
條碼掃描器將條碼數據顯示在文本框中,如下所示:
使用 C# 掃描條碼圖像
在本節中,IronBarcode 庫有效處理涉及傾斜 QR 碼的實際情況。 雖然傾斜角度的 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
開啟傾斜的 QR 碼圖像後,輸出將如下所示:
傾斜的 QrCode 圖像
可以從 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
PDF檔案中的條碼和QR碼:
C# - 從 PDF 讀取條碼的結果
在現實世界的應用場景中,條碼通常在圖像、掃描件、縮略圖或照片中發現時有缺陷,且可能包含數位噪聲或傾斜。 本節演示如何從縮略圖中讀取條碼數據。
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")
IronBarcode 是一個多功能的 .NET 軟體庫和 C# 二維碼生成器,用於掃描和讀取各種條碼圖像格式,無論這些條碼是否為完美的螢幕截圖或實際上的照片、掃描或其他 不完美的現實世界圖像,它都能做到。 此外,IronBarcode 提供了多種自訂選項來提高條碼讀取速度,例如裁剪區域或多執行緒,以及機器學習模型的準確性。 請造訪官方文件頁面以獲取有關IronBarcode的更多信息。
目前,如果您購買完整的Iron Suite,只需支付兩個的價格即可獲得五個庫。