IronBarcode nie rozpoznał kodu MSI
Problem
Podczas tworzenia kodu kreskowego MSI przy użyciu biblioteki IronBarcode, zdarzają się przypadki, gdy metoda BarcodeReader.Read nie może odczytać wygenerowanego kodu kreskowego MSI, co skutkuje pustym skanem i oczekiwaną wartością z kodu kreskowego.
Rozwiązanie
Aby upewnić się, że BarcodeReader.Read może odczytać kod kreskowy MSI, musimy podać opcjonalny drugi parametr BarcodeReaderOptions dla metody Read. W ramach BarcodeReaderOptions, wyraźnie określamy rodzaj kodu kreskowego, który próbujemy odczytać, przypisując wartość ExpectedBarcodeTypes. W ten sposób metoda Read rozpozna kod kreskowy MSI wygenerowany z IronBarcode, jak również zewnętrzne kody kreskowe MSI.
Poniżej znajduje się szybki przykład, jak zastosować BarcodeReaderOptions do BarcodeReader.Read.
Przykład kodu
// 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
W przykładowym kodzie najpierw tworzymy nową zmienną BarcodeReaderOptions, a następnie przypisujemy ExpectedBarcodeTypes z enumem BarcodeEncoding.MSI, instruując IronBarcode, aby oczekiwał kodu kreskowego MSI. Następnie drukujemy wartość kodu kreskowego, która wynosiłaby 12345, w pętli for, ponieważ barcodeResults zwraca tablicę BarcodeResults i przechodzimy przez każdy wynik w poszukiwaniu wartości tekstowej.

