IronBarcode konnte MSI-Barcode nicht erkennen
Problem
Beim Erstellen eines MSI-BarCodes mit der IronBarcode-Bibliothek gibt es Fälle, in denen die Methode BarcodeReader.Read den generierten MSI-BarCode nicht lesen kann, was zu einem leeren Scan und dem erwarteten Wert aus dem BarCode führt.
Lösung
Um sicherzustellen, dass BarcodeReader.Read den MSI-BARCODE lesen kann, müssen wir den sekundären optionalen Parameter BarcodeReaderOptions für die Methode Read angeben. Innerhalb von BarcodeReaderOptions geben wir den BarCode-Typ, den wir lesen möchten, explizit an, indem wir den Wert ExpectedBarcodeTypes zuweisen. Auf diese Weise würde die Read-Methode sowohl den von IronBarcode generierten MSI-BarCode als auch alle externen MSI-BarCodes erkennen.
Nachfolgend finden Sie ein kurzes Beispiel dafür, wie BarcodeReaderOptions auf BarcodeReader.Read angewendet wird.
Codebeispiel
// 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
Im Beispiel instanziieren wir zunächst eine neue Variable BarcodeReaderOptions und weisen dann ExpectedBarcodeTypes die Enumeration BarcodeEncoding.MSI zu, wodurch IronBarcode angewiesen wird, stattdessen den MSI-BarCode zu erwarten. Anschließend PRINTen wir den Wert des BarCodes, der 12345 lauten würde, in einer for-Schleife, da barcodeResults ein Array von BarcodeResults zurückgibt, und durchlaufen jedes Ergebnis, um den Textwert zu ermitteln.

