This tutorial shows how to create a C# Windows Console application that uses the LEADTOOLS SDK to perform MICR detection and recognition.
Overview | |
---|---|
Summary | This tutorial covers how to use LEADTOOLS MICR SDK technology in a C# Windows Console application. |
Completion Time | 30 minutes |
Visual Studio Project | Download tutorial project (3 KB) |
Platform | C# Windows Console Application |
IDE | Visual Studio 2017, 2019 |
Development License | Download LEADTOOLS |
Try it in another language |
|
Before working on the Detect and Extract MICR - Console C# tutorial, complete the following tutorials, re-using code as needed. For instance, when completing the Load and Save Images tutorial, build upon what was done in the Add References and Set a License tutorial.
Start with a copy of the project created in the Load and Save Images tutorial. If you do not have that project, follow the steps in that tutorial to create it.
The references needed depend upon the purpose of the project. References can be added by one or the other of the following two methods (but not both). For this project, the following references are needed:
If using NuGet references, this tutorial requires the following NuGet packages:
Leadtools.Formats.Raster.Common
Leadtools.Document.Sdk
If local DLL references are used, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS22\Bin\Dotnet4\x64
:
Leadtools.dll
Leadtools.Codecs.dll
Leadtools.Codecs.Cmp.dll
Leadtools.Document.Writer.dll
Leadtools.Forms.Commands.dll
Leadtools.ImageProcessing.Core.dll
Leadtools.Ocr.dll
Leadtools.Ocr.LEADEngine.dll
For a complete list of which Codec DLLs are required for specific formats, refer to File Format Support.
The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details including tutorials for different platforms, refer to Setting a Runtime License.
There are two types of runtime licenses:
Now that the LEADTOOLS references have been added, and the license set, coding can begin.
In Solution Explorer, open Program.cs
. In the Program
class, add a new method called RunMICRDetectionRecogntion(image);
, and then add the below MICR detection and recognition code inside the new method. The method's parameter will be the RasterImage loaded in the RasterImage image = LoadImage(@"C:\LEADTOOLS22\Resources\Images\bankcheck.jpg");
. For this tutorial the E13b sample check and CMC7 sample check are used.
// Using block at the top
using System;
using System.IO;
using System.Text;
using Leadtools;
using Leadtools.Codecs;
using Leadtools.Forms.Commands;
using Leadtools.ImageProcessing.Core;
using Leadtools.Ocr;
static void Main(string[] args)
{
try
{
SetLicense();
RasterImage image = LoadImage(@"C:\LEADTOOLS22\Resources\Images\bankcheck.jpg");
// Uncomment for CMC7
// RasterImage image = LoadImage(@"C:\LEADTOOLS22\Resources\Images\cmc7.jpg");
RunMICRDetectionRecogntion(image);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.WriteLine("Press any key to exit...");
Console.ReadKey(true);
}
static void RunMICRDetectionRecogntion(RasterImage image)
{
using (RasterCodecs codecs = new RasterCodecs())
{
StringBuilder sb = new StringBuilder();
BankCheckReader micrReader = new BankCheckReader();
IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD);
ocrEngine.Startup(codecs, null, null, @"C:\LEADTOOLS22\Bin\Common\OcrLEADRuntime");
micrReader.OcrEngine = ocrEngine;
// MICR Code Detection searches for E13b MICR font type
MICRCodeDetectionCommand e13bCmd = new MICRCodeDetectionCommand
{
SearchingZone = new LeadRect(0, 0, image.Width, image.Height)
};
e13bCmd.Run(image);
// Run CMC7 Detection
CMC7CodeDetectionCommand cmc7Cmd = new CMC7CodeDetectionCommand();
cmc7Cmd.Run(image);
// If E13b MICR code found
if (e13bCmd.MICRZone != LeadRect.Empty)
{
micrReader.MicrFontType = BankCheckMicrFontType.E13b;
micrReader.ProcessImage(image);
foreach (var value in micrReader.Results)
{
if (value.Key != "Signature")
{
sb.Append("\n");
sb.Append($"Field Name: {value.Key}\n");
sb.Append($"Field Value: {value.Value.Text}\n");
}
}
Console.WriteLine(sb.ToString());
Console.ReadLine();
}
// If CMC7 MICR code found
else if (cmc7Cmd.CMC7Zone != LeadRect.Empty)
{
micrReader.MicrFontType = BankCheckMicrFontType.Cmc7;
micrReader.ProcessImage(image);
foreach (var value in micrReader.Results)
{
if (value.Key != "Signature")
{
sb.Append("\n");
sb.Append($"Field Name: {value.Key}\n");
sb.Append($"Field Value: {value.Value.Text}\n");
}
}
Console.WriteLine(sb.ToString());
}
else
{
Console.WriteLine("No MICR text detected!");
}
ocrEngine.Shutdown();
}
}
Note
The code snippet above supports functionality for gathering MICR information from an E13b check and a CMC7 check. To test CMC7 text detection and recognition, change the file path in the LoadImage() method to a file path pointing to a CMC7 check image.
To handle MemoryStream
add the following code into the Main()
method.
using (RasterCodecs codecs = new RasterCodecs())
{
string filename = @"C:\LEADTOOLS22\Resources\Images\bankcheck.jpg";
byte[] data = File.ReadAllBytes(filename);
MemoryStream ms = new MemoryStream(data);
using (RasterImage image = codecs.Load(ms))
RunMICRDetectionRecogntion(image);
}
Run the project by pressing F5, or by selecting Debug -> Start Debugging.
If the steps were followed correctly, the application runs and the console displays the check image's MICR information.
This tutorial showed how to use the MICRCodeDetectionCommand
and BankCheckReader
classes.