Reading Text from Image using Java – Tess4J

In this post, We will show you How to read text from image using Java programming language. The example program has been tested and shared in the same post.

This example application (ie: Reading Text from Image) using open source API called Tess4J, The API can be downloaed from this link.

Project Structure

How to Read Text from Image using Java

Implementation Steps

Step 1: Once the distribution is downloaded, unzip it, Navigate to lib folder and copy all the JAR files add to your project.

Step 2: Refer the JAR to your project.

Step 3: Copy the testdata folder to your project.

Step 4: Add the Image file that you want to extract to the project.

(Note: You can refer the above steps in the project structure image that is attached above.)

Example Program (TextExtractor.java)

package com.dineshkrish;
import java.io.File;
import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
/**
* 
* @author Dinesh Krishnan
*
*/
public class TextExtractor {
private static ITesseract instance;
private TextExtractor() {
}
public static ITesseract getInstance() {
if (instance == null) {
instance = new Tesseract();
}
return instance;
}
public static String getText(final File imageFile) {
String result = null;
try {
result = TextExtractor.getInstance().doOCR(imageFile);
} catch (TesseractException e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
String fileName = "input.png";
File imageFile = new File(fileName);
System.out.println("The Text is : " + TextExtractor.getText(imageFile));
}
}

Download Source Code

You can download the source code from here

Input Image

How to Read Text from Image using Java

Output

The Text is : DINESH KRISHNAN

References

1. Tess4J Official Website
2. Tess4J Java API Documentation

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *