Tuesday, April 7, 2009

Full process of scanning the document and extracting data

Now the full process of scanning the document and extracting data from it is mostly finished. It does not look too complicated now, unlike it was just a few weeks earlier.

First, I create the instances of a class that will keep the extracted data (AIT3MDocument) and the class that will communicate with the scanner (AIT3MDocumentScanner) through a ReaderClass object. I explained it in some more detail in one of the earlier blog entries.

AIT3MDocument document = new AIT3MDocument();
AIT3MDocumentScanner docScanner = new AIT3MDocumentScanner();
document = docScanner.ScanDocument(_Filepath, useOCRCheckBox.Checked);

documentReader = new ReaderClass();
documentItem = new DocumentItemClass();

Next, I will extract some data that lets me identify what kind of document is in the scanner.

string documentID = documentReader.RetrieveTextItem(DOCUMENT_ID);
string documentSpecific = documentReader.RetrieveTextItem(DOCUMENT_SPECIFIC);
string mrzDocumentType = documentReader.RetrieveTextItem(MRZ_DOCUMENT_TYPE);


I can extract data such as the english spelling of the person's name through the ReaderClass object.

string name = documentReader.RetrieveTextItem(NAME);

If, for example, the document was identified as a Hong Kong ID, I know that it has a special sequence of digits on it which can be extracted too.

string nameAsNum = documentReader.RetrieveTextItem(MRZ_NAME_AS_NUMBER_1);

This sequence of digits encodes the chinese spelling of the person's name. To be able to extract that spelling was the only reason I spent all that time and effort writing my glorious wrapper for the unmanaged dll.

So, after I found out the type of the document and the sequence of digits that encodes the name, I can call the proper function:

if (!String.IsNullOrEmpty(nameAsNum))
{
TSSLWrapper.RECO_DATA recoData = new TSSLWrapper.RECO_DATA();
IntPtr ptr = TSSLWrapper.SDKCreate();
bool res = TSSLWrapper.CcnOCRsdk_HKID(ptr, nameAsNum, out recoData);

if (res)
{
document.FirstNameExtended = recoData.FirstName;
document.LastNameExtended = recoData.Surname;
}
TSSLWrapper.SDKDelete(ptr);
}

and observe the result.

Now I just populate the names into the proper fields and my job is done.

This is the simplest example. Some other types of documents I work with do not contain a sequence of digits, but rather operate with the image directly. In this case a path to the saved image has to be provided, but the output is more or less similar anyway, so there is not much difference from the development point of view.

by . Also posted on my website

No comments: