This tutorial shows how to perform MICR detection and recognition in an Android application using the LEADTOOLS SDK.
Overview | |
---|---|
Summary | This tutorial covers how to use LEADTOOLS MICR SDK technology in an Android Java application. |
Completion Time | 20 minutes |
Android Studio Project | Download tutorial project (150 KB) |
Platform | Android (Java) |
IDE | Android Studio |
Runtime License | Download LEADTOOLS |
Try it in another language |
|
Get familiar with the basic steps of creating a project and working with the OcrEngine by reviewing the Add References and Set a License and Recognize-Text-from-Images with OCR tutorials, before working on the Detect and Extract MICR - Android Java tutorial.
Start with a copy of the project created in the Display Images in an Image Viewer tutorial. If you do not have that project, follow the steps in that tutorial to create it.
Ensure that you add the needed Artifacts to the project.
For a complete list of which LEADTOOLS libraries are required for your application, refer to Files to be Included With Your Application.
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:
In the Project Explorer window, open the activity_main.xml
file found in the app/src/main/res/layout
directory. Below the RasterImageViewer XML code add a new RUN OCR button.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1.0"
android:background="@android:color/black">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Image From Gallery"
android:onClick="onSelectImage"/>
<leadtools.controls.RasterImageViewer
android:id="@+id/rasterimageviewer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".96"
android:background="@android:color/white"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Read MICR Code"
android:onClick="readMicr"/>
</LinearLayout>
In MainActivity.java
, add the following import statements before the MainActivity
class:
import leadtools.forms.commands.BackCheckMicrFontType;
import leadtools.forms.commands.BankCheckField;
import leadtools.forms.commands.BankCheckReader;
import leadtools.imageprocessing.core.CMC7CodeDetectionCommand;
import leadtools.imageprocessing.core.MICRCodeDetectionCommand;
Add the following member variables to the MainActivity
class:
private BankCheckReader bcReader;
private MICRCodeDetectionCommand e13b;
private CMC7CodeDetectionCommand cmc7;
Update the onCreate()
function as shown below:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bcReader = new BankCheckReader();
e13b = new MICRCodeDetectionCommand();
cmc7 = new CMC7CodeDetectionCommand();
}
Create a readMicr(View v)
function and add the code below to detect if MICR text is present. If so, parse the value.
public void readMicr(View v ){
RasterImage image = mViewer.getImage();
if(image != null){
try{
e13b.setSearchingZone(new LeadRect(0, 0, image.getWidth(), image.getHeight()));
e13b.run(image); // check if E13B MICR Codes exist
CMC7CodeDetectionCommand cmc7Cmd = new CMC7CodeDetectionCommand();
cmc7Cmd.run(image); // check if CMC7 MICR Codes exist
if(!Objects.equals(e13b.getMICRZone(), LeadRect.getEmpty()) || !Objects.equals(cmc7.getCMC7Zon(),LeadRect.getEmpty()))
{
bcReader.setOcrEngine(ocrEngine);
bcReader.setMicrFontType(BackCheckMicrFontType.UNKNOWN);
bcReader.processImage(image);
HashMap<String, BankCheckField> info = bcReader.getResults();
for(BankCheckField val : info.values())
{
if(info.containsKey("MICR"))
{
BankCheckField a = info.get("MICR");
Toast.makeText(this, a.getText(), Toast.LENGTH_LONG).show();
}
}
}
else
Toast.makeText(this, "MICR not found", Toast.LENGTH_LONG).show();
}
catch(Exception ex){
Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
}
}
else{
Toast.makeText(this, "No File is loaded", Toast.LENGTH_LONG).show();
}
}
Press Shift + F10 to run the application. Follow the steps below to test the application.
Select READ MICR CODE to read the image and output the results.
This tutorial showed how to run MICR Recognition using BankCheckReader
.