IronBarCode Could Not Recognize MSI Barcode

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

問題

在使用 IronBarCode 庫創建 MSI 條碼時,有時會出現 BarcodeReader.Read 方法無法讀取生成的 MSI 條碼的情況,導致掃描結果為空以及條碼的預期值。

解決方案

為了確保 BarcodeReader.Read 能夠讀取 MSI 條碼,我們必須為 Read 方法提供次要的可選 BarcodeReaderOptions 參數。 在 BarcodeReaderOptions 中,我們通過賦予值 ExpectedBarcodeTypes 明確指定我們嘗試讀取的條碼類型。 這樣,Read 方法就可以識別由 IronBarCode 生成的 MSI 條碼以及任何外部的 MSI 條碼。

以下是一個快速示例,說明如何將 BarcodeReaderOptions 應用於 BarcodeReader.Read

代碼示例

// Creating MSI Barcode with the value "12345"
var myBarcode = BarcodeWriter.CreateBarcode("12345", BarcodeWriterEncoding.MSI);

// Save barcode as image
myBarcode.SaveAsImage("msi.png");

// Reading MSI
var barcodeReaderOptions = new BarcodeReaderOptions
{
     // Assigning BarcodeEncoding.MSI to ExpectBarcodeType to ensure that IronBarcode expects MSI type barcodes specifically 
  ExpectBarcodeTypes = BarcodeEncoding.MSI,
};

// Read barcode with additional barcodeReaderOptions from above
var barcodeResults = BarcodeReader.Read("msi.png", barcodeReaderOptions);

// Using a for loop and print out the result 
foreach (BarcodeResult result in barcodeResults)
{
  Console.WriteLine(result.Text);
    // Output: 12345 
}
// Creating MSI Barcode with the value "12345"
var myBarcode = BarcodeWriter.CreateBarcode("12345", BarcodeWriterEncoding.MSI);

// Save barcode as image
myBarcode.SaveAsImage("msi.png");

// Reading MSI
var barcodeReaderOptions = new BarcodeReaderOptions
{
     // Assigning BarcodeEncoding.MSI to ExpectBarcodeType to ensure that IronBarcode expects MSI type barcodes specifically 
  ExpectBarcodeTypes = BarcodeEncoding.MSI,
};

// Read barcode with additional barcodeReaderOptions from above
var barcodeResults = BarcodeReader.Read("msi.png", barcodeReaderOptions);

// Using a for loop and print out the result 
foreach (BarcodeResult result in barcodeResults)
{
  Console.WriteLine(result.Text);
    // Output: 12345 
}
' Creating MSI Barcode with the value "12345"
Dim myBarcode = BarcodeWriter.CreateBarcode("12345", BarcodeWriterEncoding.MSI)

' Save barcode as image
myBarcode.SaveAsImage("msi.png")

' Reading MSI
Dim barcodeReaderOptions As New BarcodeReaderOptions With {.ExpectBarcodeTypes = BarcodeEncoding.MSI}

' Read barcode with additional barcodeReaderOptions from above
Dim barcodeResults = BarcodeReader.Read("msi.png", barcodeReaderOptions)

' Using a for loop and print out the result 
For Each result As BarcodeResult In barcodeResults
  Console.WriteLine(result.Text)
	' Output: 12345 
Next result
$vbLabelText   $csharpLabel

在示例中,我們首先實例化一個新的 BarcodeReaderOptions 變量,然後將 ExpectedBarcodeTypes 賦予 BarcodeEncoding.MSI 枚舉,指示 IronBarcode 預期 MSI 條碼。 之後,我們在一個 for 循環中打印出條碼的值,該值將是 12345,因為 barcodeResults 返回一個 BarcodeResults 的數組,並通過每個結果進行循環以獲取文本值。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 1,935,276 | 版本: 2025.11 剛剛發布