Detect and Extract MICR - Console C#

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

Required Knowledge

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.

  1. Complete the Add References and Set a License tutorial.
  2. When completing the Load and Save Images tutorial, re-use sections of the Add References and Set a License tutorial wherever possible. Saving the image is not necessary for the Detect and Extract MICR - Console C# tutorial, so that portion can be commented out.

Create the Project and Add LEADTOOLS References

In Visual Studio, create a new C# Windows Console project, and add the below following necessary LEADTOOLS references.

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:

If local DLL references are used, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS21\Bin\Dotnet4\x64:

For a complete list of which Codec DLLs are required for specific formats, refer to File Format Support.

Set the License File

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:

Note

Adding LEADTOOLS NuGet and local references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Add the MICR Detection and Bank Check Reader Code

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:\LEADTOOLS21\Resources\Images\bankcheck.jpg");. For this tutorial the E13b sample check and CMC7 sample check are used.

C#
// 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; 
C#
RunMICRDetectionRecogntion(image); 
C#
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:\LEADTOOLS21\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.

Run the Project

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.

The application runs and the console displays the check image's MICR information

Wrap-up

This tutorial showed how to use the MICRCodeDetectionCommand and BankCheckReader classes.

See Also

Help Version 21.0.2023.3.1
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.