Working with Barcode Recognition State in XML

Overview

In addition to barcode recognition functionality, class BarCodeReader allows performing the serialization of the barcode recognition state and uploading it in the XML format. The recognition state can be saved both to a file ExportToXml(String) or a stream ExportToXml(Stream). Moreover, it can be imported in the XML format from a file or a stream using functions ImportFromXml(String) and ImportFromXml(Stream).

It should be noted that the serialization of the information about a barcode image, the link to a source image file, or the image itself cannot be performed automatically. In the case of such a need, this functionality has to be set explicitly using the family of functions called SetBarCodeImage.

Saving Barcode Recognition State to XML

Aspose.BarCode for .NET provides two ways to obtain the current state of class BarCodeReader: to an XML file through the ImportToXml(String) function or a stream using the ImportToXml(Stream) function. The code snippet below illustrates how to perform the serialization of the barcode recognition state to an XML file.

//init barcode reader
using (BarCodeReader read = new BarCodeReader())
{
    read.SetBarCodeReadType(DecodeType.Pdf417);
    read.BarcodeSettings.StripFNC = true;
    read.QualitySettings.MedianSmoothingWindowSize = 5;
    ////serialize BarCodeReader to file
    read.ExportToXml($"{path}readerPdf417.xml");
}

Loading Barcode Recognition State from XML

The current state of class BarCodeReader can be loaded from an XML file through the ImportFromXml(String) function or from a stream through the ImportFromXml(Stream) function. The target barcode image needs to be specified using the group of properties called SetBarCodeImage. The following code snippet explains how to load the state of a BarCodeReader object from an XML file.

//load BarCodeReader from file
Console.WriteLine("BarCodeReaderSerialization:");
using (BarCodeReader read = BarCodeReader.ImportFromXml($"{path}readerPdf417.xml"))
{
    //set the recognized file because it is not stored
    read.SetBarCodeImage($"{recpath}many_pdf417.png");
    //initialized data
    Console.WriteLine($"StripFNC:{read.BarcodeSettings.StripFNC}");
    Console.WriteLine($"MedianSmoothingWindowSize:{read.QualitySettings.MedianSmoothingWindowSize}");
    //read
    Console.WriteLine($"Barcodes read: {read.ReadBarCodes().Length}");
    foreach (BarCodeResult result in read.FoundBarCodes)
        Console.WriteLine($"{result.CodeTypeName}:{result.CodeText}");
}

Loading and Saving Barcode Recognition State from Stream

Aspose.BarCode for .NET enables saving the barcode recognition state or loading it from streams using special functions ExportToXml(Stream) and ImportFromXml(Stream), respectively. The target barcode image needs to be set explicitly using the group of properties called SetBarCodeImage. The following code snippet demonstrates how to save the barcode recognition state and load it from a stream.

//stream 
MemoryStream ms = new MemoryStream();

//init barcode reader
using (BarCodeReader read = new BarCodeReader())
{
    read.SetBarCodeReadType(DecodeType.Pdf417);
    read.BarcodeSettings.StripFNC = true;
    read.QualitySettings.MedianSmoothingWindowSize = 5;
    ////serialize BarCodeReader to stream
    read.ExportToXml(ms);
    ms.Position = 0;
}

//load an instance of BarCodeReader from stream
Console.WriteLine("BarCodeReaderStreamSerialization:");
using (BarCodeReader read = BarCodeReader.ImportFromXml(ms))
{
    //set the recognized file because it is not stored
    read.SetBarCodeImage($"{recpath}many_pdf417.png");
    //initialized data
    Console.WriteLine($"StripFNC:{read.BarcodeSettings.StripFNC}");
    Console.WriteLine($"MedianSmoothingWindowSize:{read.QualitySettings.MedianSmoothingWindowSize}");
    //read
    Console.WriteLine($"Barcodes read: {read.ReadBarCodes().Length}");
    foreach (BarCodeResult result in read.FoundBarCodes)
        Console.WriteLine($"{result.CodeTypeName}:{result.CodeText}");
}