在生產環境中測試,無水印。
在任何需要的地方都能運行。
獲得 30 天的全功能產品。
在幾分鐘內上手運行。
試用產品期間完全訪問我們的支援工程團隊
條形碼提供了一種清晰且機器可讀的方式來呈現數據。 最初,條碼由不同寬度和間距的平行線組成,用來表示數據。 這些傳統的線性或一維(1D)條碼可以由專門的光學設備稱為條碼閱讀器掃描。 然而,條碼的演變促使發明了二維(2D)條碼,也稱為矩陣碼。 與傳統條碼不同,2D條碼使用矩形、點和六邊形等圖案,而非條狀圖案。 要读取这些二维条码,可以使用特定的光学扫描仪设置,或者选择使用连接到运行解码软件的计算机的数码相机作为替代方法。 此外,像智慧型手機這樣的行動裝置可以利用其內建的相機和專用應用程式來作為2D條碼掃描器。
Zebra Crossing,通常稱為 ZXing,是一個開源的多格式 1D/2D 條碼圖像處理工具包,最初用 Java 開發,並具有其他語言的移植版本。 核心圖像解碼庫、Java特定的客户端代碼以及Android客户端條碼扫描器只是構成ZXing的幾個模塊。 有許多獨立的開源專案是基於它構建的。
打開 Visual Studio,從檔案選單中選擇「新專案」,然後選擇「主控台應用程式」。 在本文中,我們選擇了 C# 控制台應用程式。
在相应的文本框中輸入專案名稱和文件路徑。 接下來,點擊建立按鈕以選擇所需的 .NET Framework。
如果您選擇了控制台應用程式,專案將會建立其結構並打開 program.cs 檔案,讓您可以輸入程式碼並建立或執行它。
在 NuGet 封裝管理器主控台輸入下一個命令。
Install-Package ZXing.Net.Bindings.Windows.Compatibility
或者,我們可以使用 NuGet 套件管理工具獲取項目。 如下圖所示。 嘗試安裝您選擇的第一個結果。
我們可以使用以下範例代碼來創建條碼。 ZXing允許我們創建超過10種條碼格式。
using ZXing.Windows.Compatibility;
var options = new QrCodeEncodingOptions
{
Width = 250,
Height = 250,
};
var writer = new BarcodeWriter();
writer.Format= BarcodeFormat.QR_CODE;
writer.Options = options;
System.Drawing.Bitmap _bitmap=writer.Write("Hello world");
_bitmap.Save("Demo1.png");
using ZXing.Windows.Compatibility;
var options = new QrCodeEncodingOptions
{
Width = 250,
Height = 250,
};
var writer = new BarcodeWriter();
writer.Format= BarcodeFormat.QR_CODE;
writer.Options = options;
System.Drawing.Bitmap _bitmap=writer.Write("Hello world");
_bitmap.Save("Demo1.png");
Imports ZXing.Windows.Compatibility
Private options = New QrCodeEncodingOptions With {
.Width = 250,
.Height = 250
}
Private writer = New BarcodeWriter()
writer.Format= BarcodeFormat.QR_CODE
writer.Options = options
Dim _bitmap As System.Drawing.Bitmap=writer.Write("Hello world")
_bitmap.Save("Demo1.png")
上面,我們正在設置QrCodeEncodingOptions
的高度和寬度。 然後我們為BarcodeWriter
創建一個對象。 對於BarcodeWriter
,我們將條碼格式設置為QR_Code。 然後我們分配之前創建的qrcode
選項。 BarcodeWriter
中的 write 函數將給定的字串編碼成條碼,並以點陣圖圖像的形式返回條碼。 然後,在位圖中的保存功能的幫助下,我們可以保存影像。 以下是程式碼的結果。
using ZXing.Windows.Compatibility;
var barcodeBitmap = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile("demo.png");
var reader = new BarcodeReader();
var result = reader.Decode(barcodeBitmap);
if (result != null)
{
Console.WriteLine(result.Text);
Console.ReadKey();
}
using ZXing.Windows.Compatibility;
var barcodeBitmap = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile("demo.png");
var reader = new BarcodeReader();
var result = reader.Decode(barcodeBitmap);
if (result != null)
{
Console.WriteLine(result.Text);
Console.ReadKey();
}
Imports ZXing.Windows.Compatibility
Private barcodeBitmap = CType(System.Drawing.Bitmap.FromFile("demo.png"), System.Drawing.Bitmap)
Private reader = New BarcodeReader()
Private result = reader.Decode(barcodeBitmap)
If result IsNot Nothing Then
Console.WriteLine(result.Text)
Console.ReadKey()
End If
在上述代碼中,我們首先將圖像加載到位圖中。然後,我們創建一個BarcodeReader
對象。 Decode
函數允許我們將位圖作為參數傳遞,這可以將結果返回為多種類型。接下來,我們使用 Text
屬性來獲取條碼中編碼的文本。
借助這個條碼庫,讀取和創建條碼變得簡單明瞭。 使用 IronBarcode 的程式庫製作動態條碼很容易。 只需幾行程式碼,這個簡單的程式庫就可以生成條碼,從而幫助我們編碼條碼圖像。 IronBarcode 可以讓我們使用 C# 和 VB.NET 等語言生成條形碼。
要在解決方案中使用IronBarcode庫,您必須下載所需的套件。 要做到這一點,請利用下面描述的套件管理器代碼:
:PackageInstall
或者,您可以使用 NuGet 套件管理器,它會顯示所有搜索結果,來尋找和下載 "Barcode" 套件。 然後,您可以從中選擇要下載到程序中的基本套件。
只需短短幾行代碼,我們就可以使用IronBarcode庫快速製作條碼圖像。 此外,它使我們能夠將生成的條形碼另存為一個單獨的圖片文件。以下是一段使用 Console 程式建立條形碼標籤的 Visual Basic 範例代碼。
using IronBarCode;
QRCodeWriter.CreateQrCode(textBox1.Text, 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0).SaveAsPng("demo.png");
using IronBarCode;
QRCodeWriter.CreateQrCode(textBox1.Text, 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0).SaveAsPng("demo.png");
Imports IronBarCode
QRCodeWriter.CreateQrCode(textBox1.Text, 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0).SaveAsPng("demo.png")
使用中等級別的錯誤校正生成 500 x 500 像素的圖形,然後使用SaveAsPng
方法保存到檔案位置。
以下的程式碼範例會讀取我們在前一個範例中創建的 QR 碼中編碼的文字。
var barcodeBitmap = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile("demo.png");
var reader=IronBarCode.BarcodeReader.Read(barcodeBitmap);
Console.WriteLine(reader.Values()[0]);
Console.ReadKey();
var barcodeBitmap = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile("demo.png");
var reader=IronBarCode.BarcodeReader.Read(barcodeBitmap);
Console.WriteLine(reader.Values()[0]);
Console.ReadKey();
Dim barcodeBitmap = CType(System.Drawing.Bitmap.FromFile("demo.png"), System.Drawing.Bitmap)
Dim reader=IronBarCode.BarcodeReader.Read(barcodeBitmap)
Console.WriteLine(reader.Values()(0))
Console.ReadKey()
我們首先將圖像加載到位圖中,然後使用BarcodeReader
類的Read
方法讀取圖像。 我們使用從Read
方法返回的BarcodeResults
物件上的Values
屬性來獲取從 QR 碼讀取的內容。
如需了解更多關於ZXing的信息及其與IronBarcode的比較,請閱讀這篇下一篇博客文章。
我們的閱讀條碼教程還提供有關如何使用IronBarcode來讀取條碼和QR碼的更多信息。 更多有關 IronBarcode 的程式教學。
ZXing 條碼掃描器可以創建高質量的條碼,但它過時且僅支持少數條碼格式。 它也有有限的文件和產品支持。
另一方面,IronBarcode 非常高效且靈活,能夠在許多操作系統上運行。 IronBarcode 可以更改條碼中的顏色、大小、間距和字母樣式。 它還支持 Crystal Reports。
開發人員可以免費使用 IronBarcode。 用戶可以購買授權以獲得額外功能,並享受一整年的支援和產品更新。