Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Reading Text from Images Using Java

DZone's Guide to

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.

· Java Zone
Free Resource

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

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

Language Used

  • Java

Git Location

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!

Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.

Topics:
java ,tesseract ,image-to-text-conversion ,tutorial

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

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}