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

Extracting Text From an Image

DZone's Guide to

Extracting Text From an Image

Advances in Big Data have made extracting text from images a much easier task than it used to be years ago.

· Big Data Zone
Free Resource

Need to build an application around your data? Learn more about dataflow programming for rapid development and greater creativity. 

Years ago, extracting text from images seemed to be one of the greatest challenges to all developers. Now, with the arrival of great tools, reading and extracting text from images is easy.

Today's I'll be explaining how to extract text from images using the Java Tesseract API from net.sourceforge.tess4j.

Extracting text from an image means that you are considering the flowchart imagery that's processed to extract the text components and then extracting the geometrical shapes components. The text components are extracted with geometrical components, as well. The internal relationship between the components is set up by tracing the flow lines that connect different components. The extracted components are output to metadata (in XML format), which is machine-readable. This metadata can be archived, stored in a knowledge base, or shared with others.

Below is the code for extracting text from images using the Java Tesseract API from  net.sourceforge.tess4j

1. Adding the API

Add the net.sourceforge.tess4j.*; API to your pom.xml:

<dependency> 
 <groupId>net.sourceforge.tess4j</groupId> 
 <artifactId>tess4j</artifactId> 
 <version>3.2.1</version> 
</dependency>

This is the image that we're extracting the text from:
Image title

2. Download the CAPTCHA Language Extractor

Download the CAPTCHA language extractor and put it in the tessdata folder.

For example, if you download eng.trainedata from the above URL, put the file at the project root folder tessdata/eng-trainedata .

 3. Read the Code

Here's the Java code that will read the text from an image in any format:

package com.amudabadmus.awfa;
import net.sourceforge.tess4j.*;
import java.io.*;
public class App {
    public String getImgText(String imageLocation) {
      ITesseract instance = new Tesseract();
      try 
      {
         String imgText = instance.doOCR(new File(imageLocation));
         return imgText;
      } 
      catch (TesseractException e) 
      {
         e.getMessage();
         return "Error while reading image";
      }
   }
   public static void main ( String[] args)
   {
      App app = new App();
      System.out.println(app.getImgText("C:\\Users\\User\\Pictures\\img.png"));
   }
}

Image title

For more information, check out the source code and the demo

Check out the Exaptive data application Studio. Technology agnostic. No glue code. Use what you know and rely on the community for what you don't. Try the community version.

Topics:
big data ,tutorial ,image processing ,text analysis ,extracting

Published at DZone with permission of Amuda Adeolu. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}