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數組,然後遍歷每個結果以取得文字值。






