條碼和 QR 碼在 C# 和 VB.NET 應用程式中

This article was translated from English: Does it need improvement?
Translated
View the article in English

使用我們的 IronBarcode 軟體庫,在 C# 和所有其他 .NET 語言中讀取和寫入條碼是一個簡單的過程。

安裝 IronBarcode

旅程的第一步是安裝 IronBarcode,可以通過從 NuGet 下載來完成或通过 下載DLL.

要安裝 IronBarcode NuGet 套件,您可以使用 Visual Studio 的 NuGet 套件管理器:

Install-Package BarCode

或者,您也可以使用 dotnet CLI 進行安裝:

dotnet add package BarCode

您可以在以下網站找到有關NuGet套裝的更多資訊 NuGet網站.

讀取條碼或 QR 碼

使用 IronBarcode 讀取條碼只需一行代碼。

:path=/static-assets/barcode/content-code-examples/get-started/get-started-1.cs
using IronBarCode;

BarcodeResults results = BarcodeReader.Read("QuickStart.jpg");
if (results != null)
{
    foreach (BarcodeResult result in results)
    {
        Console.WriteLine(result.Text);
    }
}
Imports IronBarCode

Private results As BarcodeResults = BarcodeReader.Read("QuickStart.jpg")
If results IsNot Nothing Then
	For Each result As BarcodeResult In results
		Console.WriteLine(result.Text)
	Next result
End If
VB   C#

使用這一行代碼,您可以檢測並掃描輸入文件中的所有類型條碼,具有卓越的性能—所需的一切僅需一步驟。!此方法支持多種影像格式,包括JPEG、PNG和BMP等影像格式,以及PDF和GIF、TIFF等多幀格式。 對於需要更高速度、優化內存使用或提高讀取準確性的人來說,提供可自訂的配置選項以將性能微調至您的特定需求。

首先,為了提高閱讀速度,您可以添加一個設定了Speed設置的BarcodeReaderOptions對象。 預設情況下,設置為「平衡」,但也有「更快」選項,可以跳過某些條碼讀取檢查,例如檢查反轉顏色或條碼旋轉。

:path=/static-assets/barcode/content-code-examples/get-started/get-started-2.cs
using IronBarCode;

BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
    Speed = ReadingSpeed.Faster
};

BarcodeResults result = BarcodeReader.Read("QuickStart.jpg", myOptionsExample);
if (result != null)
{
    Console.WriteLine(result.First().Text);
}
Imports IronBarCode

Private myOptionsExample As New BarcodeReaderOptions() With {.Speed = ReadingSpeed.Faster}

Private result As BarcodeResults = BarcodeReader.Read("QuickStart.jpg", myOptionsExample)
If result IsNot Nothing Then
	Console.WriteLine(result.First().Text)
End If
VB   C#

您也可以將 ScanMode 設定為 OnlyBasicScan 以跳過條碼檢測算法掃描文件。

:path=/static-assets/barcode/content-code-examples/get-started/get-started-3.cs
using IronBarCode;

BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
    ScanMode = BarcodeScanMode.OnlyBasicScan
};

BarcodeResults result = BarcodeReader.Read("QuickStart.jpg", myOptionsExample);
if (result != null)
{
    Console.WriteLine(result.First().Text);
}
Imports IronBarCode

Private myOptionsExample As New BarcodeReaderOptions() With {.ScanMode = BarcodeScanMode.OnlyBasicScan}

Private result As BarcodeResults = BarcodeReader.Read("QuickStart.jpg", myOptionsExample)
If result IsNot Nothing Then
	Console.WriteLine(result.First().Text)
End If
VB   C#

可以嘗試的其他配置包括指定要掃描的條碼格式,這樣 IronBarcode 就不需要掃描所有條碼類型,以及裁剪圖像的特定區域,從而減少 IronBarcode 需要處理的圖像數據。 還有一個選項可以預期多個條碼,因此如果只預期一個條碼,IronBarcode 可以跳過某些檢測和解碼多個條碼的過程。

:path=/static-assets/barcode/content-code-examples/get-started/get-started-4.cs
using IronBarCode;

BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
    ExpectMultipleBarcodes = false,
    ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128,
    CropArea = new System.Drawing.Rectangle(100, 200, 300, 400),
};

BarcodeResults result = BarcodeReader.Read("QuickStart.jpg", myOptionsExample);
if (result != null)
{
    Console.WriteLine(result.First().Text);
}
Imports IronBarCode

Private myOptionsExample As New BarcodeReaderOptions() With {
	.ExpectMultipleBarcodes = False,
	.ExpectBarcodeTypes = BarcodeEncoding.QRCode Or BarcodeEncoding.Code128,
	.CropArea = New System.Drawing.Rectangle(100, 200, 300, 400)
}

Private result As BarcodeResults = BarcodeReader.Read("QuickStart.jpg", myOptionsExample)
If result IsNot Nothing Then
	Console.WriteLine(result.First().Text)
End If
VB   C#

生成條碼

要使用 IronBarcode 寫入條碼,我們使用 BarcodeWriter 類別。 使用 BarcodeWriter,編寫條形碼又是一個簡單的代碼行:只需輸入數據和條形碼格式,就可以開始了。! 然後,您可以將條碼圖像數據保存為內存或文件。

:path=/static-assets/barcode/content-code-examples/get-started/get-started-5.cs
using IronBarCode;

GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128);
myBarcode.SaveAsImage("myBarcode.png");
Imports IronBarCode

Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128)
myBarcode.SaveAsImage("myBarcode.png")
VB   C#

樣式條碼

IronBarcode 提供多種選項來操控條碼的視覺外觀。 支持的條碼樣式功能包括調整大小、設定輸出圖像的邊距、更改前景和背景顏色以及添加註釋。

:path=/static-assets/barcode/content-code-examples/get-started/get-started-7.cs
using IronBarCode;

GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128);
myBarcode.AddAnnotationTextAboveBarcode("Product URL:");
myBarcode.AddBarcodeValueTextBelowBarcode();
myBarcode.SetMargins(100);
myBarcode.ChangeBarCodeColor(IronSoftware.Drawing.Color.Purple);

// All major image formats supported as well as PDF and HTML
myBarcode.SaveAsPng("myBarcode.png");
Imports IronBarCode

Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128)
myBarcode.AddAnnotationTextAboveBarcode("Product URL:")
myBarcode.AddBarcodeValueTextBelowBarcode()
myBarcode.SetMargins(100)
myBarcode.ChangeBarCodeColor(IronSoftware.Drawing.Color.Purple)

' All major image formats supported as well as PDF and HTML
myBarcode.SaveAsPng("myBarcode.png")
VB   C#

將條碼導出為 HTML

還有一個方便的功能,可以從生成的條碼中導出HTML。 它可以將條碼導出為一個獨立的HTML文件,不包含外部資產,作為一個獨立的HTML標籤,或作為數據URI。

:path=/static-assets/barcode/content-code-examples/get-started/get-started-8.cs
using IronBarcode;

myBarcode = BarcodeWriter.CreateBarcode("12345", BarcodeWriterEncoding.EAN8);
myBarcode.SaveAsHtmlFile("myBarcode.html");
Imports IronBarcode

myBarcode = BarcodeWriter.CreateBarcode("12345", BarcodeWriterEncoding.EAN8)
myBarcode.SaveAsHtmlFile("myBarcode.html")
VB   C#

生成 QR 碼

在使用 IronBarcode 生成 QR 碼時,我們可以選擇使用 QRCodeWriter 類別。 此類別提供了一些專屬於 QR 碼的更多自訂配置,例如設定錯誤校正級別和在 QR 的中心添加圖像。

:path=/static-assets/barcode/content-code-examples/get-started/get-started-9.cs
using IronBarCode;
using IronSoftware.Drawing;

QRCodeLogo qrCodeLogo = new QRCodeLogo("visual-studio-logo.png");
GeneratedBarcode myQRCodeWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", qrCodeLogo);
myQRCodeWithLogo.ChangeBarCodeColor(Color.DarkGreen).SaveAsPdf("MyQRWithLogo.pdf");
Imports IronBarCode
Imports IronSoftware.Drawing

Private qrCodeLogo As New QRCodeLogo("visual-studio-logo.png")
Private myQRCodeWithLogo As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", qrCodeLogo)
myQRCodeWithLogo.ChangeBarCodeColor(Color.DarkGreen).SaveAsPdf("MyQRWithLogo.pdf")
VB   C#

支持的條碼格式

IronBarcode支持多種常用的條碼格式,可進行讀取和寫入:

  • QRMicro QRRectangular Micro QR(rMQR)** 代碼。
  • 其他二维条码如 AztecData MatrixMaxiCodePDF417
  • 堆疊線性條碼,例如 Databar
  • 傳統的一維條碼格式如 UPC-AUPC-EEAN-8EAN-13CodabarITFMSIPlessey

為什麼選擇IronBarcode?

IronBarcode 提供了一個友好且易於使用的 API,讓開發人員能夠為 .NET 閱讀和生成條碼,在真實使用情境中優化準確性、精確性和速度。

例如,BarcodeWriter 類別將自動驗證和更正 UPCA 和 UPCE 條碼的「校驗和」。 它還會對數字進行「零填充」,使其符合特定的數字格式。 如果您的數據不適合指定的數據格式,IronBarcode 將指導開發人員使用更合適的條碼格式。

IronBarcode在從文件中讀取條碼以及從實際拍攝的圖像中讀取條碼方面都很出色。 該庫包含多種影像預處理技術,以最大化條形碼被讀取的概率,例如自動旋轉和影像去噪。

向前邁進

若要充分利用 IronBarcode,我們鼓勵您閱讀本說明文件部分中的教程,並造訪我們的網站。GitHub.