Detect and Extract MICR - Java

This tutorial shows how to create a Java 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 Java application.
Completion Time 30 minutes
Eclipse Project Download tutorial project (2 KB)
Platform Java Application
IDE Eclipse
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial and Load and Save Images tutorial, before working on the Detect and Extract MICR - Java tutorial.

Create the Project and Add the LEADTOOLS References

Start with a copy of the project created in the Load and Save Images tutorial. If that project is unavailable, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. References can be added by local .jar files located at <INSTALL_DIR>\LEADTOOLS21\Bin\Java.

For this project, the following references are needed:

For a complete list of which JAR files are required for your application, refer to Files to be Included with your Java Application

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 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

With the project created, the references added, the license set, and the load image code added, coding can begin. The code pertaining to saving the image is not necessary for this tutorial and can be commented out.

Open the Main.java class in the Project Explorer. Add the following statements to the import block at the top.

Java
// import block at the top 
import java.io.IOException; 
import java.nio.file.*; 
 
import leadtools.*; 
import leadtools.codecs.*; 
import leadtools.forms.commands.*; 
import leadtools.imageprocessing.core.*; 
import leadtools.ocr.*; 

Inside the main() method add the following to set the library path to where the CDLL files are located, as well as load the LEADTOOLS library that was previously imported. Also, change the inputFile string, containing the file path passed into the LoadImage() method to "C:\\LEADTOOLS21\\Resources\\Images\\bankcheck.jpg".

Java
public static void main(String[] args) throws IOException  
{ 
    Platform.setLibPath("C:\\LEADTOOLS21\\Bin\\CDLL\\x64"); 
    Platform.loadLibrary(LTLibrary.LEADTOOLS); 
    Platform.loadLibrary(LTLibrary.CODECS); 
    Platform.loadLibrary(LTLibrary.OCR); 
 
	SetLicense(); 
		 
	RasterCodecs codecs = new RasterCodecs(); 
	// e13b check image file 
	String inputFile = "C:\\LEADTOOLS21\\Resources\\Images\\bankcheck.jpg"; 
    // CMC7 check image file, uncomment for implementation 
	//String inputFile = "C:\\LEADTOOLS21\\Resources\\Images\\cmc7.jpg"; 
	//String outputFile = "C:\\Temp\\test.jpg"; 
	RasterImage image = LoadImage(inputFile, codecs); 
    RunMICRDetectionRecognition(image); 
	//SaveImage(image, outputFile, codecs); 
} 

Add a new method to the _Main class called RunMICRDetectionRecognition(RasterImage image). Call this method inside the main method below the call to the LoadImage() method, as shown above. Add the following code inside the new method to detect and extract the MICR text from the loaded image. This tutorial uses E13b check sample image and CMC7 check sample image images.

Java
static void RunMICRDetectionRecognition(RasterImage image) throws IOException  
{ 
    StringBuilder sb = new StringBuilder(); 
 
    BankCheckReader micrReader = new BankCheckReader(); 
 
    OcrEngine ocrEngine = OcrEngineManager.createEngine(OcrEngineType.LEAD); 
    ocrEngine.startup(null, null, null, "C:\\LEADTOOLS21\\Bin\\Common\\OcrLEADRuntime"); 
    micrReader.setOcrEngine(ocrEngine); 
 
    // MICR Code Detection searches for E13b MICR font type 
    MICRCodeDetectionCommand e13bCmd = new MICRCodeDetectionCommand(); 
    e13bCmd.setSearchingZone(new LeadRect(0, 0, image.getWidth(), image.getHeight())); 
    e13bCmd.run(image); 
 
    // Run CMC7 Detection 
    CMC7CodeDetectionCommand cmc7Cmd = new CMC7CodeDetectionCommand(); 
    cmc7Cmd.run(image); 
 
    // If E13b MICR code found 
    if (e13bCmd.getMICRZone() != LeadRect.getEmpty()) 
    { 
        micrReader.setMicrFontType(BackCheckMicrFontType.E13B); 
        micrReader.processImage(image); 
 
        for (var value : micrReader.getResults().entrySet()) 
        { 
            if (value.getKey() != "Signature") 
            { 
                sb.append("\n"); 
                sb.append("Field Name: " + value.getKey() + "\n"); 
                sb.append("Field Value: " + value.getValue().getText() + "\n"); 
            } 
        } 
    } 
    // If CMC7 MICR code found 
    else if (cmc7Cmd.getCMC7Zone() != LeadRect.getEmpty()) 
    { 
        micrReader.setMicrFontType(BackCheckMicrFontType.CMC7); 
        micrReader.processImage(image); 
 
        for (var value : micrReader.getResults().entrySet()) 
        { 
            if (value.getKey() != "Signature") 
            { 
                sb.append("\n"); 
                sb.append("Field Name: " + value.getKey() + "\n"); 
                sb.append("Field Value: " + value.getValue().getText() + "\n"); 
            } 
        } 
    } 
    else 
        System.out.println("No MICR text detected!"); 
 
    System.out.println(sb.toString()); 
    System.out.println("Press 'Enter' to exit..."); 
    System.in.read(); 
    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 Ctrl + F11, or by selecting Run -> Run.

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.2021.5.11
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.