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.
Join the DZone community and get the full member experience.
Join For FreeThis 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
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.Create a folder named tessdata inside c:\myprogram\
Navigate to https://github.com/tesseract-ocr/tessdata
Download eng.traineddata for breaking Captchas with English (trained data is available for other languages as well).
Place the eng.traineddata inside the tessdata folder.
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.You can also skip Step 2 to Step 5 and simply download the tessdata-master folder from https://github.com/tesseract-ocr/tessdata
Unzip the content of tessdata-master.zip file in your main project folder (for example, here, it is c:\myprogram\)
Rename tessdata-master to tessdata
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
First, crackImage takes the image that needs to be read.
We point a file object to that image.
We make a Tesseract object named instance.
We call the predefined method doOCR of the Tesseract library, passing the file object from step 2.
The doOCR method returns the text read from the image and returns the same.
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
We call the crackImage method, passing the image to be read from.
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!
Published at DZone with permission of Anurag Jain, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments