|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
This article is in the Product Showcase section for our sponsors at The Code Project. These reviews are intended to provide you with information on products and services that we consider useful and of value to developers.
IntroductionWithout proper preparation, converting a scanned image to text via OCR can produce substandard results. Without the proper tools, preparing a scanned image for OCR can be a complex, if not an impossible task. LEADTOOLS Document Imaging Suite solves both of these problems by providing functions that get the image from the scanner, removes nonessential elements that could cause problems for OCR and finally converts the image to a text or document format with the industry leader OCR engine. In this article I’ll create an application that allows you to choose a scanner, adjust the scanner’s settings to produce images optimal for OCR, scan from the device, OCR the documents, and save recognized text out to disk as a searchable PDF. EnvironmentThe sample in this article was compiled with Visual Studio 2005 using C# and LEADTOOLS Document Imaging Suite version 15 with the OCR PDF plug-in or the LEADTOOLS v15 .NET evaluation with OCR runtime. The CodeI’ve created a Windows application with three buttons to keep this simple.
Under the hood, the LEADTOOLS .NET classes perform the bulk of the work. We’ll walk through the code in the order that it’s executed, starting with the form load event. private void MainFrm_Load(object sender, EventArgs e)
{
// Unlock Support for features
RasterSupport.Unlock(RasterSupportType.Document, "");
RasterSupport.Unlock(RasterSupportType.Ocr, "");
RasterSupport.Unlock(RasterSupportType.OcrPdfOutput, "");
// Create objects
_twSession = new TwainSession();
_OCR = RasterDocumentEngine.Instance;
_Deskew = new DeskewCommand();
_Despeckle = new DespeckleCommand();
_HoleRemove = new HolePunchRemoveCommand();
_BorderRemove = new BorderRemoveCommand();
_LineRemove = new LineRemoveCommand();
_SmoothCharacters = new SmoothCommand();
_InvertText = new InvertedTextCommand();
// Initialize Twain Object
_twSession.Startup(this, "LEAD Technologies, Inc", "Tutorials",
"1.0.0.0", "ScanOCRSavePDF", TwainStartupFlags.None);
_twSession.AcquirePage += new EventHandler
I first unlock the support for some of the Document Imaging Suite features. These functions only have to be called once (typically in a startup routine) and the features they unlock are then available for the life of the process. If you are using the LEADTOOLS evaluation, you do not have to call these functions, as all functionality is available. Next we create each object. The OCR object ( For both the scanning object and OCR object, you must call the I link a function to the In this sample, we are saving out the text that the OCR object has generated from the images (recognized text) as a searchable PDF (PDF Image with text underneath). We also set the Each document clean object is then initialized to values that are optimal for most scanned bitonal images. private void btnSelectScanner_Click(object sender, EventArgs e)
{
_twSession.SelectSource(string.Empty);
}
In the
If you would like to select a scanning device without showing this dialog, simply pass the name of the device for the parameter in the Before we begin the scan, you'll want to set up the scanner to produce images that are optimal for OCR. We set the X and Y resolution to 300 and set the bits per pixel to one, which essentially tells the scanning device to scan in black and white. Next, Here is the code that does what was just described: private void btnScan_Click(object sender, EventArgs e)
{
try
{
// Change Cursor to Wait
this.Cursor = Cursors.WaitCursor;
// Set scanner to acquire with optimal settings for OCR
_twSession.Resolution = new SizeF(300.0f, 300.0f);
_twSession.ImageBitsPerPixel = 1;
// Scan from scanner.
_twSession.Acquire(TwainUserInterfaceFlags.None);
// AutoOrient each page
Console.WriteLine("AutoOrientPage");
for (int i = 0; i < _OCR.PageCount; i++)
{
_OCR.AutoOrientPage(i);
}
// Delete RDF file so we do not constantly append to it.
if (System.IO.File.Exists(_OCR.RecognitionDataFileName))
System.IO.File.Delete(_OCR.RecognitionDataFileName);
// OCR all the pages
Console.WriteLine("Recognize");
_OCR.Recognize(0, _OCR.PageCount, null);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
// Change cursor to arrow
this.Cursor = Cursors.Arrow;
}
}
At this point, the images are being scanned and the The void _twSession_AcquirePage(object sender, TwainAcquirePageEventArgs e)
{
// Clean image before adding to OCR object
// Deskew
_Deskew.Run(e.Image);
// Despekle
_Despeckle.Run(e.Image);
// Hole Punch Remove
_HoleRemove.Run(e.Image);
// Inverted Text
_InvertText.Run(e.Image);
// Border Remove
_BorderRemove.Run(e.Image);
// Line Remove
_LineRemove.Type = LineRemoveCommandType.Vertical;
_LineRemove.Run(e.Image);
_LineRemove.Type = LineRemoveCommandType.Horizontal;
_LineRemove.Run(e.Image);
// Smooth Characters
_SmoothCharacters.Run(e.Image);
// Add Page to OCR
_OCR.AddPage(e.Image, -1);
}
Lastly, I save the results from the OCR to disk. As you remember, I set up the OCR to output the results as a PDF file. The OCR will take the data in the recognition data file and convert it to a searchable PDF file. private void btnSave_Click(object sender, EventArgs e)
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "PDF (*.pdf)|*.pdf";
dlg.FilterIndex = 0;
if (dlg.ShowDialog() == DialogResult.OK)
{
_OCR.SaveResultsToFile(dlg.FileName);
}
}
ResultsBelow are the results of an image before cleaning versus a cleaned image from the LEADTOOL’s OCR:
ConclusionThis is just one way you can implement a workflow of scanning, cleaning, OCRing and archiving. Should you have barcodes or patch codes on the scanned document, you can use LEADTOOL’s barcode functionality to detect them in the Required Software to Build this SampleIn order to build an application such as this, you will need the following products: In order to run this sample on your machine, you can download LEADTOOLS 60-day fully functional evaluation. SupportHave questions about this sample? Contact our expert support team for free evaluation support!
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||