文档图像虽然可以节约物理储存空间,但是某些情况下,它无法节约时间和省去麻烦。无纸化报表和手动扫描纸质文档是归档账单、发票和财务报表的比较好的方式。然而,工作人员需要花较多的时间和精力来整理这些数字文件夹。并且,随着数字文档的增多,即使整理归档的人具有很好的记忆力和习惯,也容易出现差错。
LEADTOOLS Forms Recognition and Processing拥有强大而灵活的图像库。通过LEADTOOLS构建出的应用程序可以自动对比扫描文档与已知模板,然后准确地对扫描文档进行归类。当文档被准确识别后,LEADTOOLS可以从表单上所定义的位置提取OCR、 OMR和Barcodes等。
处理文档库
解决文档归类问题的首要步骤就是处理和管理所有扫描文档归类时所存放的文档库。处理文档库的方法很多,本示例选择使用控制台应用程序来处理文档库。管理文档库的代码比较简单,因为它主要使用基本文件和带有System.IO命名空间的文件夹操作。而最关键的部分就是将应用程序传递到封装了LEADTOOLS Forms Recognition的DocumentClassifier,从而返回移动的数据并重命名文件。
// Check the scanned document repository for new documents
string[] newDocuments = Directory.GetFiles(docRepositoryNewDocs);
DocumentClassifier docClassifier = new DocumentClassifier(docRepositoryMasterForms);
string movedDocumentName, masterFormSubFolder;
foreach (string currentDoc in newDocuments)
{
movedDocumentName = null;
// Try to match this document against known document types
ClassifiedDocument classifiedDoc = docClassifier.ClassifyDocument(currentDoc);
if (classifiedDoc.MasterFormName != null)
{
// Add the subfolder for the master form if it doesn't exist
masterFormSubFolder = string.Format(@"{0}{1}\",
docRepositoryRoot,classifiedDoc.MasterFormName);
if (!Directory.Exists(masterFormSubFolder))
Directory.CreateDirectory(masterFormSubFolder);
// rename the file according to the date found
if (classifiedDoc.DocumentDate != DateTime.MinValue)
{
movedDocumentName = string.Format("{0}{1}{2}",
masterFormSubFolder,
classifiedDoc.DocumentDate.ToString("yyyyMMdd"),
currentDoc.Substring(currentDoc.LastIndexOf('.'),
currentDoc.Length - currentDoc.LastIndexOf('.')));
}
else
{
// Didn't find a date to rename with, so just move it
movedDocumentName = currentDoc.Replace(docRepositoryNewDocs, masterFormSubFolder);
}
}
else
{
movedDocumentName = currentDoc.Replace(docRepositoryNewDocs,
docRepositoryUnclassifiedDocs);
}
if (!string.IsNullOrEmpty(movedDocumentName))
File.Move(currentDoc, movedDocumentName);
}
使用LEADTOOLS表单识别功能
在 LEADTOOLS归类文档前,必须创建一个Master Form模板集,使LEADTOOLS知道如何对文档进行分类。LEADTOOLS自带一个Master Form编辑器演示,我们根据演示为包含了发票日期字段的两张不同发票添加一个Master Form。
我们定义好Master Form后,接下来准备处理文档。我们已经扫描了2张基于Master Form的发票和tax form,将每个文件放置到 "New" 中, LEADTOOLS会自动对比主模板。如果LEADTOOLS找到匹配文件,它会处理文档的字段,然后返回表单名称和日期字段。
// Create an OCR Engine for each processor on the machine. This
// allows for optimal use of thread during recognition and processing.
ocrEngines = new List<IOcrEngine>();
for (int i = 0; i < Environment.ProcessorCount; i++)
{
ocrEngines.Add(OcrEngineManager.CreateEngine(OcrEngineType.Advantage, false));
ocrEngines[i].Startup(formsCodec, null, String.Empty, String.Empty);
}
// Point repository to directory with existing master forms
formsRepository = new DiskMasterFormsRepository(formsCodec, _MasterFormFolder);
autoEngine = new AutoFormsEngine(formsRepository, ocrEngines, null, AutoFormsRecognitionManager.Default | AutoFormsRecognitionManager.Ocr, 30, 70, true);
// Run the forms recognition on this document
AutoFormsRunResult runResult = autoEngine.Run(document, null);
if (runResult != null)
{
// In this example we use two pieces of information to organize the classified forms:
// 1. Form name is used for the sub folder
// 2. "ClassificationRenameDate" field for the file name
retClassifiedDocument.MasterFormName = runResult.RecognitionResult.MasterForm.Name;
// Process the recognized form and extract desired info
foreach (FormPage formPage in runResult.FormFields)
{
foreach (FormField field in formPage)
{
if (field != null && field.Name == "ClassificationRenameDate")
{
retClassifiedDocument.DocumentDate = DateTime.Parse((
field.Result as TextFormFieldResult).Text);
}
}
}
}
你会看到两张发票完全与Master Form完全匹配,并根据日期重新命名。