Home > Code > C# > How to Extract Barcode Data from a Scanned OMR Form inside .NET Apps

How to Extract Barcode Data from a Scanned OMR Form inside .NET Apps

by sher azam   on Mar 27, 2015   Category: C#   |  Views: 975    |  Points: 25   |  Gold 


This technical tip shows how to extract Barcode Data from a Scanned Image inside .NET applications. Aspose.OMR for .NET contains BarcodeElement which provides the functionality to extract barcode information from a scanned OMR form. Aspose.OMR API uses the Aspose.BarCode API for the extraction of barcode information from OMR forms. Therefore it is essential to add a reference to Aspose.BarCodeRecognition.dll in the project when barcode extraction is required.

Extracting Barcode Data

The BarcodeElement class is available in the Aspose.OMR.Elements namespace and can be used to recognize a barcode in a scanned image. The following steps are required to extract barcode data:

• Create an instance of the OmrTemplate class and initialize it with the path to the template file.
• Create an instance of the OmrImage class and initialize it with the path to the scanned OMR form image.
• Create an instance of the BarcodeElement class and initialize it by specifying the expected barcode location on the scanned image and it's approximate size.
• Add the BarcodeElement object to the OmrTemplate element collection.
• Create an instance of OmrEngine and initialize it by passing the OmrTemplate object.
• Initiate the OMR process by calling the OmrEngine class' ExtractData.
• Get all pages of extracted data and iterate over it to display the extracted information.

Required Resources

• A template (.amr file) that contains the graphical mapping of elements to be recognized from the scanned images. The template is loaded using OmrTemplate.
• The scanned pages with human marked data like surveys or tests that the barcode will be extracted from, and the images are loaded using the OmrImage class.
• Aspose.BarCodeRecognition.dll has to be referenced in the project where barcode extraction is required.

Sample Code for Extracting Barcode Data from a Scanned OMR Form

[C#  Code Sample]

string templateFile = "sample-template.amr";
string imageFile = "sample-image.jpg";

// do not forget to set the license for BarCodeRecognition in case BarCode elements are used
Aspose.BarCodeRecognition.License licenseBarCode = new Aspose.BarCodeRecognition.License();
licenseBarCode.SetLicense("Aspose.Total.lic");

Aspose.OMR.License licenseOmr = new Aspose.OMR.License();
licenseOmr.SetLicense("Aspose.Total.lic");

// Loading the template
OmrTemplate template = OmrTemplate.Load(templateFile);

// Adding BarCode element requires creation of BarcodeElement object
// While specifying the barcode display name, its position and size
BarcodeElement barcodeElement = new BarcodeElement("Aztec BarCode", new PointF(0, 0), new SizeF(205, 205));
// Add the BarCode element to the page element collection
template.Pages[0].Elements.Add(barcodeElement);

// Create an instance of OmrImage and load the image using file path
OmrImage image = OmrImage.Load(imageFile);
// Create an instance of OmrEngine and load the template using file path
OmrEngine engine = new OmrEngine(template);
// Extract OMR data and store the results in an instance of OmrProcessingResults
OmrProcessingResult result = engine.ExtractData(new OmrImage[]{image});
// Get all page data into an instance of Hashtable
Hashtable[] pages = result.PageData;
// Loop over all the pages
foreach(Hashtable page in pages)
{
// Display key and value
foreach (string key in page.Keys)
{
Console.WriteLine("key: " + key + ": " + "value: " + page[key]);
}
}

[VB.NET Code Sample]

Dim templateFile As String = "sample-template.amr"
Dim imageFile As String = "sample-image.jpg"

' do not forget to set the license for BarCodeRecognition in case BarCode elements are used
Dim licenseBarCode As New Aspose.BarCodeRecognition.License()
licenseBarCode.SetLicense("Aspose.Total.lic")

Dim licenseOmr As New Aspose.OMR.License()
licenseOmr.SetLicense("Aspose.Total.lic")

' Loading the template
Dim template As OmrTemplate = OmrTemplate.Load(templateFile)

' Adding BarCode element requires creation of BarcodeElement object
' While specifying the barcode display name, its position and size
Dim barcodeElement As New BarcodeElement("Aztec BarCode", New PointF(0, 0), New SizeF(205, 205))
' Add the BarCode element to the page element collection
template.Pages(0).Elements.Add(barcodeElement)

' Create an instance of OmrImage and load the image using file path
Dim image As OmrImage = OmrImage.Load(imageFile)
' Create an instance of OmrEngine and load the template using file path
Dim engine As New OmrEngine(template)
' Extract OMR data and store the results in an instance of OmrProcessingResults
Dim result As OmrProcessingResult = engine.ExtractData(New OmrImage(){image})
' Get all page data into an instance of Hashtable
Dim pages() As Hashtable = result.PageData
' Loop over all the pages
For Each page As Hashtable In pages
' Display key and value
For Each key As String In page.Keys
Console.WriteLine("key: " & key & ": " & "value: " & page(key))
Next key
Next page



Overview: Aspose.OCR for .NET

Aspose.OCR for .NET is a character recognition component built to allow developers to add OCR functionality in their ASP .NET web applications, web services and Windows applications. It provides a simple set of classes for controlling character recognition tasks. It helps developers to work with image (BMP, TIFF) files from within their own applications. It allows developers to extract text from images quickly & easily, saving time & effort involved in developing an OCR solution from scratch.

- Homepage of Aspose.OCR for .NET: http://www.aspose.com/categories/.net-components/aspose.ocr-for-.net/default.aspx

-Download Aspose.OCR for .NET: http://www.aspose.com/community/files/51/.net-components/aspose.ocr_for_.net/default.aspx



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts