IronBarcode nie rozpoznał BarCoda MSI
Problem
Podczas tworzenia kodu kreskowego MSI za pomocą biblioteki IronBarcode, istnieją przypadki, w których metoda BarcodeReader.Read nie jest w stanie odczytać wygenerowanego kodu kreskowego MSI, co skutkuje pustym skanem i oczekiwaną wartością z kodu kreskowego.
Rozwiązanie
Aby zapewnić, że BarcodeReader.Read może odczytać kod kreskowy MSI, musimy dostarczyć opcjonalny, drugorzędny parametr BarcodeReaderOptions dla metody Read. W ramach BarcodeReaderOptions wyraźnie określamy typ kodu kreskowego, który próbujemy odczytać, przypisując wartość ExpectedBarcodeTypes. W ten sposób metoda Read rozpozna zarówno kod kreskowy MSI wygenerowany z IronBarcode, jak i wszelkie zewnętrzne kody kreskowe MSI.
Poniżej znajduje się szybki przykład zastosowania 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ładzie najpierw inicjujemy nową zmienną BarcodeReaderOptions, a następnie przypisujemy ExpectedBarcodeTypes za pomocą wyliczenia BarcodeEncoding.MSI, instruując IronBarcode, aby oczekiwał kodu kreskowego MSI zamiast tego. Następnie wydrukujemy wartość kodu kreskowego, która wynosi 12345, w pętli for, ponieważ barcodeResults zwraca tablicę BarcodeResults i przechodzimy przez każdy wynik, aby uzyskać wartość tekstową.

