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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How to Convert XLS to XLSX in Java
  • Recurrent Workflows With Cloud Native Dapr Jobs
  • Java Virtual Threads and Scaling
  • Java’s Next Act: Native Speed for a Cloud-Native World

Trending

  • Scaling in Practice: Caching and Rate-Limiting With Redis and Next.js
  • Start Coding With Google Cloud Workstations
  • Chaos Engineering for Microservices
  • Scaling InfluxDB for High-Volume Reporting With Continuous Queries (CQs)
  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.5K 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.

Related

  • How to Convert XLS to XLSX in Java
  • Recurrent Workflows With Cloud Native Dapr Jobs
  • Java Virtual Threads and Scaling
  • Java’s Next Act: Native Speed for a Cloud-Native World

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!