IronBarCode konnte den MSI BarCode nicht erkennen
Problem
Beim Erstellen eines MSI-Barcodes mit Hilfe der IronBarcode -Bibliothek kommt es vor, dass die Methode BarcodeReader.Read den generierten MSI-Barcode nicht lesen kann, was zu einem leeren Scan und dem erwarteten Wert des Barcodes führt.
Lösung
Um sicherzustellen, dass die Methode BarcodeReader.Read den MSI-Barcode lesen kann, müssen wir den sekundären optionalen Parameter BarcodeReaderOptions für die Methode Read bereitstellen. Innerhalb von BarcodeReaderOptions geben wir explizit den Barcode-Typ an, den wir lesen möchten, 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 ein kurzes Beispiel, wie man BarcodeReaderOptions auf BarcodeReader.Read anwendet.
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
In diesem Beispiel instanziieren wir zunächst eine neue Variable vom Typ BarcodeReaderOptions und weisen dann ExpectedBarcodeTypes den Enumerationswert BarcodeEncoding.MSI zu, wodurch IronBarcode angewiesen wird, stattdessen den MSI-Barcode zu erwarten. Anschließend geben wir den Wert des Barcodes aus, der 12345 wäre, in einer for-Schleife, da barcodeResults ein Array von BarcodeResults zurückgibt und wir jedes Ergebnis nach dem Textwert durchsuchen.

