IronBarcode nie rozpoznał kodu MSI
Problem
Podczas tworzenia kodu kreskowego MSI przy użyciu biblioteki IronBarcode zdarzają się sytuacje, w których metoda BarcodeReader.Read nie jest w stanie odczytać wygenerowanego kodu kreskowego MSI, co skutkuje pustym skanem i brakiem oczekiwanej wartości z kodu kreskowego.
Rozwiązanie
Aby zapewnić, że BarcodeReader.Read może odczytać BARCODE MSI, musimy podać dodatkowy opcjonalny parametr BarcodeReaderOptions dla metody Read. W ramach BarcodeReaderOptions wyraźnie określamy typ BARCODE'a, który próbujemy odczytać, przypisując wartość ExpectedBarcodeTypes. W ten sposób metoda Read rozpoznałaby BarCode MSI wygenerowany przez IronBarcode, a także wszelkie zewnętrzne BarCodes MSI.
Poniżej znajduje się krótki 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 tworzymy instancję nowej zmiennej BarcodeReaderOptions, a następnie przypisujemy ExpectedBarcodeTypes do wyliczenia BarcodeEncoding.MSI, instruując IronBarcode, aby oczekiwał zamiast tego kodu kreskowego MSI. Następnie drukujemy wartość BarCODE, która wynosi 12345, w pętli for, ponieważ barcodeResults zwraca tablicę BarcodeResults i przechodzi przez każdy wynik w poszukiwaniu wartości tekstowej.

