Extract MICR from Image - Java

This tutorial shows how to extract bank check data from an image using the LEADTOOLS Cloud Services in a Java application.

Overview  
Summary This tutorial covers how to make ExtractCheck requests and process the results using the LEADTOOLS Cloud Services in a Java application.
Completion Time 30 minutes
Project Download tutorial project (6 KB)
Platform LEADTOOLS Cloud Services API
IDE IntelliJ
Language Java
Development License Download LEADTOOLS

Required Knowledge

Be sure to review the following sites for information about LEADTOOLS Cloud Services API.

Application ID and Password

Create an Account with LEADTOOLS Hosted Cloud Services to obtain both Application ID and Password strings.

Service Plans

LEADTOOLS Service Plan offerings:

Service Plan Description
Free Trial Free Evaluation
Page Packages Prepaid Page Packs
Subscriptions Prepaid Monthly Processed Pages

To further explore the offerings, refer to the LEADTOOLS Hosted Cloud Services page.

To obtain the necessary Application ID and Application Password, refer to Create an Account and Application with the LEADTOOLS Hosted Cloud Services.

Create the Project and Add the Maven Dependency

In the IDE, create a new Java project with Maven, and add the following required Maven dependency to the pom.xml file:

<dependency> 
    <groupId>org.json</groupId> 
    <artifactId>json</artifactId> 
    <version>20210307</version> 
</dependency> 

Add the ExtractCheck Code

With the project created and the package added, coding can begin.

In Project Files, open App.java. Add the following import statements at the top.

Java
import org.json.JSONArray; 
import org.json.JSONObject; 
 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.net.URI; 
import java.net.http.HttpClient; 
import java.net.http.HttpRequest; 
import java.net.http.HttpRequest.BodyPublisher; 
import java.net.http.HttpResponse; 
import java.nio.charset.StandardCharsets; 
import java.util.Base64; 
import java.util.concurrent.CompletableFuture; 
import java.util.concurrent.ExecutionException; 
import java.util.concurrent.TimeUnit; 
import java.util.concurrent.TimeoutException; 

Add a method named extractCheck(). The extractCheck() method sends a ExtractCheck request to the LEADTOOLS Cloud Services API, if successful a unique identifier (GUID) will be returned and then a query using this GUID will be made.

Add the code below to the extractCheck() method.

Java
private static void extractCheck() { 
    HttpClient client = HttpClient.newHttpClient(); 
 
    //The first page in the file to mark for processing 
    int firstPage = 1; 
 
    //Sending a value of -1 will indicate to the service that all pages in the file should be processed. 
    int lastPage = -1; 
 
    // If using URL to the file 
    String fileURL = "http://demo.leadtools.com/images/cloud_samples/micr_sample.jpg"; 
    String conversionUrl = String.format("Recognition/ExtractCheck?firstPage=%s&lastPage=%s&fileurl=%s", firstPage, lastPage, fileURL); 
    Results results = postAsync(conversionUrl, null, client); 
 
    //If uploading a file as multi-part content: 
    // File uploadFile = new File("path/to/file"); 
    // String conversionUrl = String.format("Recognition/ExtractCheck?firstPage=%s&lastPage=%s", firstPage, lastPage); 
    // Results results = postAsync(conversionUrl, uploadFile, client); 
 
    if (results.getStatusCode() == 200) { 
        //Unique ID returned by the services 
        System.out.println("Unique ID returned by the services: " + results.getData()); 
        query(results.getData(), client); 
    } else { 
        System.out.println("Request failed with the following response: " + results.getStatusCode()); 
    } 
} 

Next, create a new method called query(String id, HttpClient client) that utilizes the GUID provided by the extractBusinessCard() method. If successful the response body will contain all the request data in JSON format. Be sure to call the query() method inside the extractBusinessCard() method, as shown above. Add the code below to the query() method.

Java
private static void query(String id, HttpClient client) { 
    String queryUrl = String.format("Query?id=%s", id); 
    Results results; 
    JSONObject returnedData = new JSONObject(); 
    int fileStatus = 0; 
 
    do { 
        try{ 
            results = postAsync(queryUrl, null, client); 
            returnedData = new JSONObject(results.getData()); 
            fileStatus = returnedData.getInt("FileStatus"); 
            if (fileStatus != 200) Thread.sleep(5000); 
        } catch (InterruptedException e) { 
            //e.printStackTrace(); 
        } 
    } while (fileStatus == 0 || fileStatus == 100 || fileStatus == 123); 
 
    System.out.println("File has finished processing with return code: " + fileStatus); 
    if (fileStatus != 200) { 
        return; 
    } 
 
    parseJson(returnedData.get("RequestData").toString()); 
} 

Then, create two new methods named parseJson(String json) and postAsync(String path, File file, HttpClient client), which will both be called inside the query() method, as shown above.

Add the code below to the parseJson() method to process the returned JSON data.

Java
private static void parseJson(String json) { 
    JSONArray requestArray = new JSONArray(json); 
    for (Object requestObject : requestArray) { 
        if (requestObject instanceof JSONObject) { 
            JSONObject requestReturn = (JSONObject) requestObject; 
            System.out.println("Service Type: " + requestReturn.getString("ServiceType")); 
            System.out.println("Returned Data:"); 
            JSONArray objArray = new JSONArray(requestReturn.get("data").toString()); 
            for (Object object : objArray) { 
                if (object instanceof JSONObject) { 
                    JSONObject subObject = (JSONObject) object; 
                    for (String entry : subObject.keySet()) { 
                        System.out.println(entry + ": " + subObject.get(entry)); 
                    } 
                    System.out.println("-----------------------------------"); 
                } 
            } 
        } 
    } 
} 

Add the code below to the postAsync() method to create a client connection to request the GUID and JSON data through. Where it states Replace with Application ID and Replace with Application Password be sure to place your Application ID and Password accordingly.

Java
private static Results postAsync(String path, Object body, HttpClient client) { 
    String AppId = "Replace with Application ID"; 
    String Password = "Replace with Application Password"; 
    String authHeader = "Basic " + new String(Base64.getEncoder().encode((AppId + ":" + Password).getBytes(StandardCharsets.UTF_8))); 
    String hostedServicesUrl = "https://azure.leadtools.com/api/"; 
 
    BodyPublisher thisBody = HttpRequest.BodyPublishers.ofString("null"); 
    if (body instanceof BodyPublisher) { 
        thisBody = (BodyPublisher) body; 
    } else if (body instanceof File) { 
        try { 
            thisBody = HttpRequest.BodyPublishers.ofFile(((File) body).toPath()); 
        } catch (FileNotFoundException e) { 
            //e.printStackTrace(); 
        } 
    } 
 
    HttpRequest request = HttpRequest.newBuilder() 
        .POST(thisBody) 
        .uri(URI.create(hostedServicesUrl + path)) 
        .header("Accept", "application/json") 
        .header("Authorization", authHeader) 
        .header("Content-Type", "text/plain") 
        .build(); 
 
    CompletableFuture<HttpResponse<String>> result = client.sendAsync(request, HttpResponse.BodyHandlers.ofString()); 
    Results results = new Results(); 
    results.setData(""); 
    results.setStatusCode(0); 
 
    try{ 
        results.setData(result.thenApply(HttpResponse::body).get(5, TimeUnit.SECONDS)); 
        results.setStatusCode(result.thenApply(HttpResponse::statusCode).get(5, TimeUnit.SECONDS)); 
    } catch (InterruptedException | ExecutionException | TimeoutException e) { 
        e.printStackTrace(); 
    } 
 
    return results; 
} 

The application will require a helper class to store the results of the POST requests. Create a new class called Results and add the code below to it.

Java
private static class Results { 
    private String data; 
    private int statusCode; 
 
    public String getData() { 
        return data; 
    } 
 
    public void setData(String data) { 
        this.data = data; 
    } 
 
    public int getStatusCode() { 
        return statusCode; 
    } 
 
    public void setStatusCode(int statusCode) { 
        this.statusCode = statusCode; 
    } 
} 

Run the Project

In order to test run this code be sure to add extractCheck(); to the static void main section.

Run the project by pressing Alt + F5, or by selecting Run -> Debug App.

If the steps were followed correctly, the console appears and the application displays the parsed check information from the returned JSON data.

Extracted Check Information

Wrap-up

This tutorial showed how to extract check information via the LEADTOOLS Cloud Services API in a Java application.

See Also

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

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