Click here to Skip to main content
15,879,074 members
Articles / Programming Languages / C#
Article

How to Automatically Index Scanned Forms Using Barcodes

25 Feb 2011CPOL4 min read 29.2K   8  
Did you know you can add barcodes to the forms your organization uses? Barcodes give scanned images a readily identifiable key, allowing for easy storage and retrieval in a database or content management system. Review the code samples and see how to add barcode reading and writing to applications.

This article is in the Product Showcase section for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers.

Index-Barcodes/image001.png

Barcodes are a common sight on consumer products. Almost every retail transaction in North America is driven by the scanning, recognition and lookup of barcode data. But did you know you can also add barcodes to the forms your organization creates? Doing so gives the scanned image a readily identifiable key (a patient, client or customer id), which allows for easy storage and retrieval in a database or content management system. Using a barcode to associate a scanned image with a unique key is called automatic indexing (or “auto-indexing”) of documents. Auto-indexing is enabled by an easy to use, read-write Barcode SDK, such as Barcode Xpress from Accusoft Pegasus.

In Barcode Xpress, adding barcode creation and recognition can be done in just a few lines of code. Here are a few use cases showing practical application.

Use Case: Medical Patient Registration Form

Let’s consider a simple application: patient registration forms for a doctor’s office. The forms are pre-printed, with explanatory text and a space for the patient’s name, date and signature. Add a barcode to the top of this page which contains the patient’s id and name and when the form is scanned, it can be automatically attached to the patient’s electronic record.

In this example, we will use a 2D barcode to maximize the amount of information which can be stored. Barcode Xpress supports writing of the 2D DataMatrix and PDF417 formats, both of which have compact footprints, which allow them to be easily integrated into the existing whitespace on a form. We’ll print the patient’s name and ID, separated by a colon, about 5/8 of an inch inset from the top and left hand edges of the pre-printed form. Next, encode the patient’s name ("Scott Wilson") along with an ID ("2011-0123456789-01-234-5678") with a colon separating the two fields. This produces a DataMatrix barcode that looks like this:

Index-Barcodes/image002.png

Here’s the C# code that produces this barcode image using the Barcode Xpress SDK:

C#
class Program
{
	static void Main(string[] args)
	{
		// These would come out of a database
		String patientName = "Scott Wilson";
		String patientId = "2011-0123456789-01-234-5678";
	
		PrintPatientCode(patientName, patientId);
	}
	
	static void PrintPatientCode(String patientName, String patientId)
	{
		// create and unlock the BarcodeXpress component
		BarcodeXpress bcx = new BarcodeXpress();
		bcx.Licensing.UnlockRuntime(12345, 12345, 12345, 12345);
		
		ImagXpress ix = new ImagXpress();
		
		WriterDataMatrix dataMatrix = new WriterDataMatrix(bcx);
		// Set the text value
		dataMatrix.BarcodeValue = patientName + ":" + patientId;
		// call Create and get resulting image
		ImageX img = Accusoft.ImagXpressSdk.ImageX.FromHdib(id, dataMatrix.Create());
		
		// Create an instance of our printer class
		PCPrint printer = new PCPrint();
		printer.bitmapToPrint = img.ToBitmap(false);
		// Issue print command
		printer.Print();
	}
}

public class PCPrint : System.Drawing.Printing.PrintDocument
{
	public Bitmap bitmapToPrint;
	
	protected override void OnPrintPage(System.Drawing.Printing.PrintPageEventArgs e)
	{
		// Run base code
		base.OnPrintPage(e);
		
		// Position the bitmap on the page
		e.Graphics.DrawImage(bitmapToPrint, new RectangleF(36, 36, 72, 72));
	}
}

This produces a form with the 2D Datamatrix barcode in the top left. When printed on the patient information form, it looks like this:

Index-Barcodes/image004.png

For redundancy, some customers add the barcode in two places (on the top left and bottom right of the form or document, for example). This will assist in recognition if the form is damaged. Adding redundant barcodes can be done by simply making another call to DrawImage().

Recognizing this image in a scanned document is a snap with the easy to use Barcode Xpress SDK. The form shown above was scanned on a Ricoh Aficio MP C4500 Multifunction printer into a TIF file.

We can process this image and extract the barcode as follows:

C#
class Program
{
	static void Main(string[] args)
	{
		// create and unlock the BarcodeXpress component
		BarcodeXpress bcx = new BarcodeXpress();
		bcx.Licensing.UnlockRuntime(12345, 12345, 12345, 12345);
		
		// load a 1BPP, black and white image into the ImageXpressT control
		ImagXpress ix = new ImagXpress();
		String filename = "C:\\Temp\\legal.tif"; // this would be a parameter
		ImageX imagex = Accusoft.ImageXpressSdk.ImageX.FromFile(ix, filename);
		
		// set barcode types to search for
		System.Array currentBarcodeTypes = new BarcodeType[1];
		currentBarcodeTypes.SetValue(BarcodeType.DataMatrixBarcode, 0);
		bcx.reader.BarcodeTypes = currentBarcodeTypes;
		
		// call AnalyzeBarcode to detect barcodes in image
		// all detected barcodes result, will be returned to the Result object array.
		Accusoft.BarcodeXpressSdk.Result[] results = bcx.reader.Analyze(imagex.ToHdib(false));
		
		// get some results into, if any
		for (short i = 0; i < results.Length; i++)
		{
			// get result for current barcode
			Accusoft.BarcodeXpressSdk.Result curResult = (Accusoft.BarcodeXpressSdk.Result)results.GetValue(i);
			Console.WriteLine("Value is " + curResult.BarcodeValue);
			Console.ReadLine();
			
			// At this point, update the database to parse the id
			// out of curResult.BarcodeValue,
			// and associate it with the file in filename
		}
		
		// dispose of the created components
		ix.Dispose();
		bcx.Dispose();
	}
}

When run, this program will output the expected patient information:

Value is Scott Wilson:2011-0123456789-01-234-5678

Use Case: Payment Agreement for Legal Services

This time let’s use a PDF417 barcode, and place it at the top of the page in the center. The number of changes needed is very small. Instead of creating a WriterDataMatrix, we’ll create a WriterPDF417 and change the position used in the DrawImage call. Finally, when reading the image, the BarCodeTypes array will be set to only look for PDF417.

Here’s the C# code to create the image:

C#
class Program
{
	static void Main(string[] args)
	{
		// These would come out of a database
		String patientName = "Scott Wilson";
		String patientId = "2011-0123456789-01-234-5678";
	
		PrintPatientCode(patientName, patientId);
	}
	
	static void PrintPatientCode(String patientName, String patientId)
	{
		// create and unlock the BarcodeXpress component
		BarcodeXpress bcx = new BarcodeXpress();
		bcx.Licensing.UnlockRuntime(12345, 12345, 12345, 12345);
		
		ImagXpress ix = new ImagXpress();
		
		WriterPDF417 pdf417 = new WriterPDF417(bcx);
		// Set the text value
		pdf417.BarcodeValue = clientName + ":" + clientId;
		// call Create and get result image
		ImageX img = Accusoft.ImagXpressSdk.ImageX.FromHdib(ix, pdf417.Create());
		
		// Create an instance of our printer class
		PCPrint printer = new PCPrint();
		printer.bitmapToPrint = img.ToBitmap(false);
		// Issue print command
		printer.Print();
		
		// dispose of the created components
		ix.Dispose();
		bcx.Dispose();
	}
}

public class PCPrint : System.Drawing.Printing.PrintDocument
{
	public Bitmap bitmapToPrint;
	
	protected override void OnPrintPage(System.Drawing.Printing.PrintPageEventArgs e)
	{
		// Run base code
		base.OnPrintPage(e);
		
		// Position the bitmap on the page
		e.Graphics.DrawImage(bitmapToPrint, new Rectangle(72*3, 0, 72*5, 72*2));
	}
}

And here is the C# code to read it back out:

C#
class Program
{
	static void Main(string[] args)
	{
		// create and unlock the BarcodeXpress component
		BarcodeXpress bcx = new BarcodeXpress();
		bcx.Licensing.UnlockRuntime(12345, 12345, 12345, 12345);
		
		// load a 1BPP, black and white image into the ImageXpressT control
		ImagXpress ix = new ImagXpress();
		String filename = "C:\\Temp\\legal.tif"; // this would be a parameter
		ImageX imagex = Accusoft.ImageXpressSdk.ImageX.FromFile(ix, filename);
		
		// set barcode types to search for
		System.Array currentBarcodeTypes = new BarcodeType[1];
		currentBarcodeTypes.SetValue(BarcodeType.PDF417Barcode, 0);
		bcx.reader.BarcodeTypes = currentBarcodeTypes;
		
		// call AnalyzeBarcode to detect barcodes in image
		// all detected barcodes result, will be returned to the Result object array.
		Accusoft.BarcodeXpressSdk.Result[] results = bcx.reader.Analyze(imagex.ToHdib(false));
		
		// get some results into, if any
		for (short i = 0; i < results.Length; i++)
		{
			// get result for current barcode
			Accusoft.BarcodeXpressSdk.Result curResult = (Accusoft.BarcodeXpressSdk.Result)results.GetValue(i);
			Console.WriteLine("Value is " + curResult.BarcodeValue);
			Console.ReadLine();
			
			// At this point, update the database to parse the id
			// out of curResult.BarcodeValue,
			// and associate it with the file in filename
		}
		
		// dispose of the created components
		ix.Dispose();
		bcx.Dispose();
	}
}

The printed page will now look like this:

Index-Barcodes/image008.jpg

Other Use Cases

We have just demonstrated two possibilities, but there are many others:

  • Real estate and property transaction forms
  • State and local government forms
  • Delivery receipts

Parental permission slips for schools

The possibilities for using barcodes on your forms are endless!

Conclusion

Barcode recognition is the most efficient way to facilitate the auto-indexing of documents, associating form images with a patient, client or account id. Third-party SDKs like Barcode Xpress from Accusoft Pegasus make it very easy to perform this task.

Find out more about the Barcode Xpress product features and download an unlimited trial version at http://www.accusoft.com/barcodexpress.htm . Try your own images on our latest web demo at http://demos.accusoft.com/barcodexpressdemo/. Please contact us at info@accusoft.com for more information.

About The Author

Scott Wilson is a Project Manager with Accusoft Pegasus. He has had over 20 years of software development and management experience. Scott graduated with a B.Sc. from McMaster University in Hamilton, Canada.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Accusoft provides a full spectrum of document, content, and imaging solutions as fully supported, enterprise-grade, client-server applications, mobile apps, cloud services, and software development kits. The company is focused on solving document lifecycle complexities through:


- A customer-focused approach
- Continuous product development
- Proactive support
- Forward-thinking leadership

Founded in 1991, Accusoft has grown through persistent product innovation and strategic mergers and acquisitions into the best-in-class solutions provider it is today. The company has been awarded 30 patents and is recognized as a thought leader in the industry.
This is a Organisation

1 members

Comments and Discussions