使用C# 讀取條碼
如何在C#和VB.NET中讀取條碼
- 從NuGet或者通過DLL下載安裝IronBarcode
- 使用
BarcodeReader.QuicklyReadOneBarcode
讀取任何條碼或 QR 的方法 - 在單次掃描中、PDF 或多幀 Tiff 文件中讀取多個條碼或 QR 碼
- 允許Iron Barcode從不完美的掃描和照片中讀取
- 下載教程專案,立即開始掃描
安裝條碼庫到您的 Visual Studio 專案
立即開始在您的專案中使用IronPDF,並享受免費試用。
查看 IronBarcode 上 Nuget 快速安裝和部署。已被下載超過800萬次,它正用C#改變。
Install-Package BarCode
請考慮安裝 IronBarcode DLL 直接下載並手動安裝到您的專案或GAC表單: IronBarCode.zip
手動安裝到您的項目中
下載DLLIronBarcode 提供了一個多功能、高級且高效的庫,用於在 .NET 中讀取條碼。
第一步將是安裝 IronBarcode,最簡單的方法是使用我們的 NuGet 套件,當然你也可以選擇手動安裝 DLL 添加到您的專案或全域組件快取。IronBarcode 適用於製作 C# 條碼掃描應用程式。
Install-Package BarCode
閱讀您的第一個條碼
在 .NET 中使用 IronBarcode 類庫來讀取條碼或 QR 碼非常容易。 .NET 條碼讀取器在我們的第一個示例中,我們可以看到如何用一行代碼讀取這個 Barcode。
我們可以提取其值、其影像、其編碼類型、其二進位數據 (如果有),然後我們可以將其輸出到控制台。
using IronBarCode;
using System;
BarcodeResult Result = BarcodeReader.QuicklyReadOneBarcode("GetStarted.png");
if (Result !=null && Result.Text == "https://ironsoftware.com/csharp/barcode/")
{
Console.WriteLine("GetStarted was a success. Read Value: " + Result.Text);
}
using IronBarCode;
using System;
BarcodeResult Result = BarcodeReader.QuicklyReadOneBarcode("GetStarted.png");
if (Result !=null && Result.Text == "https://ironsoftware.com/csharp/barcode/")
{
Console.WriteLine("GetStarted was a success. Read Value: " + Result.Text);
}
Imports IronBarCode
Imports System
Private Result As BarcodeResult = BarcodeReader.QuicklyReadOneBarcode("GetStarted.png")
If Result IsNot Nothing AndAlso Result.Text = "https://ironsoftware.com/csharp/barcode/" Then
Console.WriteLine("GetStarted was a success. Read Value: " & Result.Text)
End If
努力嘗試並具體說明
在下一個例子中,我們將在 QuicklyReadOneBarcode 方法中添加 TryHarder 變數。這使得它更努力地嘗試,實際上花費更多時間,但更深入地掃描可能被遮蔽、損壞或傾斜角度的 QR 碼。
BarcodeResult Result = BarcodeReader.QuicklyReadOneBarcode("TryHarderQR.png", BarcodeEncoding.QRCode
BarcodeEncoding.Code128 , true);
BarcodeResult Result = BarcodeReader.QuicklyReadOneBarcode("TryHarderQR.png", BarcodeEncoding.QRCode
BarcodeEncoding.Code128 , true);
Dim Result As BarcodeResult = BarcodeReader.QuicklyReadOneBarcode("TryHarderQR.png", BarcodeEncoding.QRCode BarcodeEncoding.Code128, True)
這將讀取這個傾斜的 QR 碼:
在我們的示例中,您可以看到我們可以指定條碼編碼(s) 我們正在尋找或指定多種格式。這樣做可以大大提高條碼讀取的性能和準確性。 管道字符或“按位 OR”用於同時指定多種格式。
如果我們進一步使用,可以達到更高的特定性。 BarcodeReader.ReadASingleBarcode
方法。
BarcodeResult Result = IronBarCode.BarcodeReader.ReadASingleBarcode("TryHarderQR.png", BarcodeEncoding.QRCode
BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.High, BarcodeReader.BarcodeImageCorrection.MediumCleanPixels);
BarcodeResult Result = IronBarCode.BarcodeReader.ReadASingleBarcode("TryHarderQR.png", BarcodeEncoding.QRCode
BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.High, BarcodeReader.BarcodeImageCorrection.MediumCleanPixels);
Dim Result As BarcodeResult = IronBarCode.BarcodeReader.ReadASingleBarcode("TryHarderQR.png", BarcodeEncoding.QRCode BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.High, BarcodeReader.BarcodeImageCorrection.MediumCleanPixels)
讀取多個條碼
PDF 文件
在我們的下一個範例中,我們將會觀察如何閱讀一個 掃描的PDF文件 並且在極少的代碼行中找到所有一維格式的條碼。
如您所見,這與從單個文件讀取單個條碼非常相似,只是我們現在有關於條碼所在頁碼的新信息。
using IronBarCode;
using System;
using System.Drawing;
// Multiple barcodes may be scanned up from a single document or image. A PDF document may also used as the input image
PagedBarcodeResult [] PDFResults = BarcodeReader.ReadBarcodesFromPdf("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);
}
using IronBarCode;
using System;
using System.Drawing;
// Multiple barcodes may be scanned up from a single document or image. A PDF document may also used as the input image
PagedBarcodeResult [] PDFResults = BarcodeReader.ReadBarcodesFromPdf("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);
}
Imports IronBarCode
Imports System
Imports System.Drawing
' Multiple barcodes may be scanned up from a single document or image. A PDF document may also used as the input image
Private PDFResults() As PagedBarcodeResult = BarcodeReader.ReadBarcodesFromPdf("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
我們在不同的頁面上發現以下條碼。
掃描和TIFF
在我們的下一個示例中,我們可以看到從多幀的TIFF中可以找到相同的結果,這在這方面將類似於PDF。
// Multi frame TIFF and GIF images can also be scanned, and multiple threads will be used automatically in the background for improved performance
PagedBarcodeResult [] MultiFrameResults = BarcodeReader.ReadBarcodesFromMultiFrameTiff("Multiframe.tiff", BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.High, BarcodeReader.BarcodeImageCorrection.MediumCleanPixels);
foreach (var PageResult in MultiFrameResults)
{
//...
}
// Multi frame TIFF and GIF images can also be scanned, and multiple threads will be used automatically in the background for improved performance
PagedBarcodeResult [] MultiFrameResults = BarcodeReader.ReadBarcodesFromMultiFrameTiff("Multiframe.tiff", BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.High, BarcodeReader.BarcodeImageCorrection.MediumCleanPixels);
foreach (var PageResult in MultiFrameResults)
{
//...
}
' Multi frame TIFF and GIF images can also be scanned, and multiple threads will be used automatically in the background for improved performance
Dim MultiFrameResults() As PagedBarcodeResult = BarcodeReader.ReadBarcodesFromMultiFrameTiff("Multiframe.tiff", BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.High, BarcodeReader.BarcodeImageCorrection.MediumCleanPixels)
For Each PageResult In MultiFrameResults
'...
Next PageResult
多執行緒
為了讀取多個文件,我們通過創建文件列表並使用 IronBarcode 可以獲得更好的結果。 BarcodeReader.ReadBarcodesMultithreaded
方法。這個方法使用多個執行緒,並可能使用 CPU 的所有核心進行條碼掃描過程,速度比逐個讀取條碼快了數倍。
// The BarcodeResult.ReadBarcodesMultiThreaded method allows for faster barcode scanning of multiple images or PDFs. All threads are automatically managed by IronBarCode.
var ListOfDocuments = new [] { "Image1.png", "image2.JPG", "image3.pdf" };
PagedBarcodeResult [] BatchResults = BarcodeReader.ReadBarcodesMultiThreaded(ListOfDocuments);
// Work with the results
foreach (var Result in BatchResults)
{
string Value = Result.Value;
//...
}
// The BarcodeResult.ReadBarcodesMultiThreaded method allows for faster barcode scanning of multiple images or PDFs. All threads are automatically managed by IronBarCode.
var ListOfDocuments = new [] { "Image1.png", "image2.JPG", "image3.pdf" };
PagedBarcodeResult [] BatchResults = BarcodeReader.ReadBarcodesMultiThreaded(ListOfDocuments);
// Work with the results
foreach (var Result in BatchResults)
{
string Value = Result.Value;
//...
}
' The BarcodeResult.ReadBarcodesMultiThreaded method allows for faster barcode scanning of multiple images or PDFs. All threads are automatically managed by IronBarCode.
Dim ListOfDocuments = { "Image1.png", "image2.JPG", "image3.pdf" }
Dim BatchResults() As PagedBarcodeResult = BarcodeReader.ReadBarcodesMultiThreaded(ListOfDocuments)
' Work with the results
For Each Result In BatchResults
Dim Value As String = Result.Value
'...
Next Result
從不完美的圖像中讀取條碼
在現實世界的使用場景中,我們可能希望讀取不完美的條碼。它們可能是不完美的圖像、掃描或照片,並且可能包含數位噪音或歪斜。使用大多數傳統的開源 .NET 條碼生成器和讀取庫,這將是不可行的。然而, C# 條碼掃描器 使這變得非常簡單。
在我們的下一個例子中,我們將看一下 QuicklyReadOneBarcode 的 TryHarder 方法。這個單一參數會使 IronBarcode 嘗試去除傾斜並從不完美的數位樣本中讀取條碼。
照片
在照片的例子中,我們將設置具體的條碼旋轉校正和條碼圖像校正,以校正數位噪音以及我們可能會合理預期來自手機相機的傾斜、透視和旋轉。
using IronBarCode;
using System;
using System.Drawing;
// All BarcodeResult.Read methods provide the developer with control to correct image and photograph correction and straightening rotation and perspective from skewed images
// * RotationCorrection e.g BarcodeReader.BarcodeRotationCorrection.Extreme un-rotates and removes perspective from barcode images.
// * ImageCorrection e.g BarcodeReader.BarcodeImageCorrection.DeepCleanPixels separates Barcodes from background imagery and digital noise.
// * BarcodeEncoding e.g. BarcodeEncoding.Code128 Setting a specific Barcode format improves speed and reduces the risk of false positive results
// Example with a photo image
var PhotoResult = BarcodeReader.ReadASingleBarcode("Photo.png", BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.Medium, BarcodeReader.BarcodeImageCorrection.DeepCleanPixels);
string Value = PhotoResult.Value;
System.Drawing.Bitmap Img = PhotoResult.BarcodeImage;
BarcodeEncoding BarcodeType = PhotoResult.BarcodeType;
byte [] Binary = PhotoResult.BinaryValue;
Console.WriteLine(PhotoResult.Value);
using IronBarCode;
using System;
using System.Drawing;
// All BarcodeResult.Read methods provide the developer with control to correct image and photograph correction and straightening rotation and perspective from skewed images
// * RotationCorrection e.g BarcodeReader.BarcodeRotationCorrection.Extreme un-rotates and removes perspective from barcode images.
// * ImageCorrection e.g BarcodeReader.BarcodeImageCorrection.DeepCleanPixels separates Barcodes from background imagery and digital noise.
// * BarcodeEncoding e.g. BarcodeEncoding.Code128 Setting a specific Barcode format improves speed and reduces the risk of false positive results
// Example with a photo image
var PhotoResult = BarcodeReader.ReadASingleBarcode("Photo.png", BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.Medium, BarcodeReader.BarcodeImageCorrection.DeepCleanPixels);
string Value = PhotoResult.Value;
System.Drawing.Bitmap Img = PhotoResult.BarcodeImage;
BarcodeEncoding BarcodeType = PhotoResult.BarcodeType;
byte [] Binary = PhotoResult.BinaryValue;
Console.WriteLine(PhotoResult.Value);
Imports IronBarCode
Imports System
Imports System.Drawing
' All BarcodeResult.Read methods provide the developer with control to correct image and photograph correction and straightening rotation and perspective from skewed images
' * RotationCorrection e.g BarcodeReader.BarcodeRotationCorrection.Extreme un-rotates and removes perspective from barcode images.
' * ImageCorrection e.g BarcodeReader.BarcodeImageCorrection.DeepCleanPixels separates Barcodes from background imagery and digital noise.
' * BarcodeEncoding e.g. BarcodeEncoding.Code128 Setting a specific Barcode format improves speed and reduces the risk of false positive results
' Example with a photo image
Private PhotoResult = BarcodeReader.ReadASingleBarcode("Photo.png", BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.Medium, BarcodeReader.BarcodeImageCorrection.DeepCleanPixels)
Private Value As String = PhotoResult.Value
Private Img As System.Drawing.Bitmap = PhotoResult.BarcodeImage
Private BarcodeType As BarcodeEncoding = PhotoResult.BarcodeType
Private Binary() As Byte = PhotoResult.BinaryValue
Console.WriteLine(PhotoResult.Value)
扫描
以下示例向我们展示了如何从 QR 码和 PDF-417 条形码读取信息。 掃描 PDF請注意,我們已經設定了適當的條碼旋轉校正和條碼圖像校正的級別,以輕微清理文件,但不會因過度滿足需求而造成巨大的性能損失。
// Multi frame TIFF and GIF images can also be scanned, and multiple threads will be used automatically in the background for improved performance
var ScanResults = BarcodeReader.ReadBarcodesFromPdf("Scan.pdf", BarcodeEncoding.All, BarcodeReader.BarcodeRotationCorrection.Low, BarcodeReader.BarcodeImageCorrection.LightlyCleanPixels);
// Work with the results
foreach (var PageResult in ScanResults)
{
string Value = PageResult.Value;
///...
}
// Multi frame TIFF and GIF images can also be scanned, and multiple threads will be used automatically in the background for improved performance
var ScanResults = BarcodeReader.ReadBarcodesFromPdf("Scan.pdf", BarcodeEncoding.All, BarcodeReader.BarcodeRotationCorrection.Low, BarcodeReader.BarcodeImageCorrection.LightlyCleanPixels);
// Work with the results
foreach (var PageResult in ScanResults)
{
string Value = PageResult.Value;
///...
}
' Multi frame TIFF and GIF images can also be scanned, and multiple threads will be used automatically in the background for improved performance
Dim ScanResults = BarcodeReader.ReadBarcodesFromPdf("Scan.pdf", BarcodeEncoding.All, BarcodeReader.BarcodeRotationCorrection.Low, BarcodeReader.BarcodeImageCorrection.LightlyCleanPixels)
' Work with the results
For Each PageResult In ScanResults
Dim Value As String = PageResult.Value
'''...
Next PageResult
縮略圖
在最後一個範例中,我們將看到這個 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.QuicklyReadOneBarcode("ThumbnailOfBarcode.gif", BarcodeEncoding.Code128);
// 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.QuicklyReadOneBarcode("ThumbnailOfBarcode.gif", BarcodeEncoding.Code128);
' 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.QuicklyReadOneBarcode("ThumbnailOfBarcode.gif", BarcodeEncoding.Code128)
摘要
總而言之,Iron Barcode 是一個多功能的 .NET 軟體庫,並且 C# QR碼生成器 用於讀取各種條碼格式,無論這些條碼是完美的螢幕截圖,還是實際的照片、掃描或其他不完美的現實世界圖像,都能讀取。
延伸閱讀
要了解更多有關使用 IronBarcode 的資訊,您可以查看此部分中的其他教程,以及我們首頁上的示例; 大部分開發人員發現這些資訊足夠他們開始使用。 API 參考文獻 特別參考 條碼讀取器
類別 and the 條碼編碼
枚舉會詳細展示使用此功能可以實現的內容 C# 條碼庫### 原始碼下載
我們也非常鼓勵您下載此教程並自行運行。您可以通過下載原始碼或者在 GitHub 上分支我們來做到這一點。本文教程的原始碼 .NET 條碼讀取器 教學範本可以作為使用C#撰寫的Visual Studio 2017主控台應用程式專案。 教學 GitHub 儲存庫