DZone
Java Zone
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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > 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.

Anurag Jain user avatar by
Anurag Jain
·
Mar. 10, 17 · Java Zone · Tutorial
Like (28)
Save
Tweet
99.65K 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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The Impacts of Blockchain on the Software Development Industry
  • How API Management Can Ease Your Enterprise Cloud Migration
  • You Are Blind to the Risks in Your Cloud — Why Companies Need Cloud Security Monitoring
  • Rails Asynchronous Processing

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo