IronBarcode 無法識別 MSI 條碼
問題
當使用 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
在範例中,我們首先實例化一個新的 BarcodeReaderOptions 變數,然後將 ExpectedBarcodeTypes 賦值為 BarcodeEncoding.MSI 列舉,指示 IronBarcode 預期 MSI 條碼。 然後,我們在 for 迴圈中列印出條碼的值,這值為 12345,因為 barcodeResults 返回 BarcodeResults 的陣列並逐一遍歷每個結果以取得文字值。

