DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Alternative Structured Concurrency
  • Jakarta EE 12: Entering the Data Age of Enterprise Java
  • Zero-Downtime Deployments for Java Apps on Kubernetes
  • Rethinking Java CRUDs With Event Sourcing and CQRS Patterns

Trending

  • Implementing Secure API Gateways for Microservices Architecture
  • Exactly-Once Processing: Myth vs Reality
  • Introduction to Tactical DDD With Java: Steps to Build Semantic Code
  • 5 Common Security Pitfalls in Serverless Architectures
  1. DZone
  2. Coding
  3. Java
  4. Reading Text from Images Using Java

Reading Text from Images Using Java

We take a look at some code that can help you to read text from an image with your Java application. Possible uses? Making sure your Captcha is doing its job.

By 
Anurag Jain user avatar
Anurag Jain
·
Mar. 10, 17 · Tutorial
Likes (28)
Comment
Save
Tweet
Share
108.8K Views

Join the DZone community and get the full member experience.

Join For Free

This post will help read texts from your images. It makes use of the Tesseract library. 

You can also use the module below to check if the Captcha on your site is strong enough and cannot be easily broken.

References

  • https://github.com/tesseract-ocr/tessdata

  • http://stackoverflow.com/questions/18095708/tess4j-doesnt-use-its-tessdata-folder

Language Used

  • Java

Git Location

  • https://github.com/csanuragjain/extra/tree/master/ReadFromImages

POM Dependency

<dependency>  
    <groupId>net.sourceforge.tess4j</groupId>  
    <artifactId>tess4j</artifactId>  
    <version>3.2.1</version>  
</dependency> 


Prerequisites

  1. Let's assume you are running this program from c:\myprogram. Now, you can follow either of two methods, based on your requirements.

    Space saving method: Only download the language data you need. That only requires 30MB for an English dataset.

  2. Create a folder named tessdata inside c:\myprogram\

  3. Navigate to https://github.com/tesseract-ocr/tessdata

  4. Download eng.traineddata for breaking Captchas with English (trained data is available for other languages as well).

  5. Place the eng.traineddata inside the tessdata folder.

  6. Finally, your folder structure should look like: c:\myprogram\tessdata\eng.traineddata

    Time-saving method: Download trained data packages larger than 1GB from several languages.

  7. You can also skip Step 2 to Step 5 and simply download the tessdata-master folder from https://github.com/tesseract-ocr/tessdata

  8. Unzip the content of tessdata-master.zip file in your main project folder (for example, here, it is c:\myprogram\)

  9. Rename tessdata-master to tessdata

  10. Finally, your folder structure should look like c:\myprogram\tessdata\<Trained data from several languages>.

Program

ImageCracker Class, crackImage Method

public static String crackImage(String filePath) {  
    File imageFile = new File(filePath);  
    ITesseract instance = new Tesseract();  
    try {  
        String result = instance.doOCR(imageFile);  
        return result;  
    } catch (TesseractException e) {  
        System.err.println(e.getMessage());  
        return "Error while reading image";  
    }  
}  


How It Works

  1. First, crackImage takes the image that needs to be read.

  2. We point a file object to that image.

  3. We make a Tesseract object named instance.

  4. We call the predefined method doOCR of the Tesseract library, passing the file object from step 2.

  5. The doOCR method returns the text read from the image and returns the same.

  6. In case of failure, it prints the error message and returns an error string.

Driver Class, Main Method

public static void main(String[] args) {  
    // TODO Auto-generated method stub  
    System.out.println(ImageCracker.crackImage("testImage.PNG"));  
}  


How It Works

  1. We call the crackImage method, passing the image to be read from.

  2. We print the text read from the method on the console.

Input Image (testImage.PNG)

Output

Create a Youtube metadata crawler using Java.

Full Program

ImageCracker Class

package com.cooltrickshome;
import java.io.File;
import net.sourceforge.tess4j.*;
public class ImageCracker {
    public static String crackImage(String filePath) {
        File imageFile = new File(filePath);
        ITesseract instance = new Tesseract();
        try {
            String result = instance.doOCR(imageFile);
            return result;
        } catch (TesseractException e) {
            System.err.println(e.getMessage());
            return "Error while reading image";
        }
    }
}


Driver Class

package com.cooltrickshome;
public class Driver {
    /**  
     * @param args  
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub  
        System.out.println(ImageCracker.crackImage("testImage.PNG"));
    }
}


Hope it helps!

Java (programming language)

Published at DZone with permission of Anurag Jain. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Alternative Structured Concurrency
  • Jakarta EE 12: Entering the Data Age of Enterprise Java
  • Zero-Downtime Deployments for Java Apps on Kubernetes
  • Rethinking Java CRUDs With Event Sourcing and CQRS Patterns

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook