IronBarCode 無法辨識 MSI 條碼

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 bots,將他對技術的熱愛與創意結合。

準備好開始了嗎?
Nuget 下載 2,070,733 | 版本: 2026.2 剛剛發布