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 
}
$vbLabelText   $csharpLabel

在這個範例中,我們首先實例化一個新的BarcodeReaderOptions變量,然後將ExpectedBarcodeTypes賦值為BarcodeEncoding.MSI枚舉,指示 IronBarcode 期望 MSI 條碼。 之後,我們在 for 迴圈中列印出條碼的值,即 12345,因為barcodeResults傳回一個BarcodeResults數組,然後遍歷每個結果以取得文字值。

柯蒂斯·週
技術撰稿人

Curtis Chau擁有卡爾頓大學電腦科學學士學位,專長於前端開發,精通Node.js、TypeScript、JavaScript和React。他熱衷於打造直覺美觀的使用者介面,喜歡使用現代框架,並擅長撰寫結構清晰、視覺效果出色的使用者手冊。

除了開發工作之外,柯蒂斯對物聯網 (IoT) 也抱有濃厚的興趣,致力於探索硬體和軟體整合的創新方法。閒暇時,他喜歡玩遊戲和製作 Discord 機器人,將他對科技的熱愛與創造力結合。

準備好開始了嗎?
Nuget 下載 2,035,202 | 版本: 2025.12 剛剛發布