IronBarcode 無法識別 MSI 條碼
問題
在使用 IronBarcode 程式庫建立 MSI 條碼時,有時 BarcodeReader.Read 方法無法讀取生成的 MSI 條碼,導致掃描結果為空,且無法取得條碼的預期值。
解決方案
為確保 BarcodeReader.Read 能讀取 MSI BARCODE,我們必須為 Read 方法提供次要的可選參數 BarcodeReaderOptions。 在 BarcodeReaderOptions 內,我們透過賦予 ExpectedBarcodeTypes 值,明確指定要讀取的 BARCODE 類型。 如此一來,Read 方法便能識別由 IronBarcode 生成的 MSI BARCODE,以及任何外部的 MSI BARCODE。
以下是一個快速範例,展示如何將 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
在此範例中,我們首先建立一個新的 BarcodeReaderOptions 變數,然後將 ExpectedBarcodeTypes 賦值為 BarcodeEncoding.MSI 枚舉值,以此指示 IronBarcode 改為預期 MSI 條碼。 隨後,我們在 for 迴圈中 PRINT BARCODE 的數值(即 12345),因為 barcodeResults 會傳回一個 BarcodeResults 陣列,我們需遍歷每個結果以取得其文字值。

