Write a java program to extract text from image

Raikwarbeni
4 min readSep 5, 2021

--

Today in this post I am going to explain how to extract text from an image by a Java Program. This post contains the complete procedure from the beginning to the end. Also I am going to write a clear Java program so that you can just copy it in your IDE software and enjoy the extraction of text from image.

आज इस पोस्ट में मैं समझाने जा रहा हूं कि जावा प्रोग्राम द्वारा किसी इमेज से टेक्स्ट कैसे निकाला जाता है। इस पोस्ट में शुरू से अंत तक की पूरी प्रक्रिया है। इसके अलावा, मैं एक स्पष्ट जावा प्रोग्राम लिखने जा रहा हूं ताकि आप इसे अपने आईडीई सॉफ्टवेयर में कॉपी कर सकें और छवि से टेक्स्ट निकालने का आनंद ले सकें।

To extract text, we need a optical character recognition (OCR) engine. There are many OCR engines on internet like vision API, Tesseract etc. Vision API is a Google product but it is not free and also it is difficult in use. So I recommend you to use Tesseract OCR because it is free and easy for beginners. I also a use Tesseract to explain the extraction of text from image in this article.

टेक्स्ट निकालने के लिए, हमें एक ऑप्टिकल कैरेक्टर रिकग्निशन (OCR) इंजन की आवश्यकता होती है। इंटरनेट पर कई ओसीआर इंजन हैं जैसे विज़न एपीआई, टेसेरैक्ट आदि। विज़न एपीआई एक Google उत्पाद है लेकिन यह मुफ़्त नहीं है और साथ ही इसका उपयोग करना मुश्किल है। इसलिए मैं आपको Tesseract OCR का उपयोग करने की सलाह देता हूं क्योंकि यह शुरुआती लोगों के लिए मुफ़्त और आसान है। मैं इस लेख में छवि से पाठ के निष्कर्षण की व्याख्या करने के लिए टेसेरैक्ट का भी उपयोग करता हूं।

Download Tessj4 and add JAR file.

The first step of this project is to download Tessj4 by clicking on this link.

Now and unzip the downloaded the Tessj 4.
Now open your IDE software create a new project and add all the jar files available in the Tessj4 folder, in the library.

Add Dependency

<! — https://mvnrepository.com/artifact/net.sourceforge.tess4j/tess4j
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>4.0.0</version>
</dependency>

Write the below given Java program.

import net.sourceforge.tess4j.*;
import java.io.*;
public class App {
public String getImgText(String imageLocation) {
ITesseract iT = new Tesseract();
try
{
String imgText = iT.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(“D:/Download 2/Artificial Intelligence.jpg”));
}
}

Explanation

1. Firstly we import net.sourceforge.tess4j package. This package contains class ITesseract and this class have a method that perform OCR of image.

2. Secondly we have to import, java.io package, this class contain File class. By this class we can import any image file by its address.

3. Now create a class App.

4. In class App create method getImageText and this method receive the location of image in your system.

5. Now then I create object of ITesseract class, iT.

6. After it I write a try block to find out the error of File not found. In this part we perform the OCR of image by doOCR method and take the extract text into String imgText variable.

7. Catch block show if any error found.

8. Now I created a main method. In which we create the object of App class, app and called getImgText method.

9. After it I pass the image location and print the extract text of image into console.

Explanation in hindi

  1. सबसे पहले हम net.sourceforge.tess4j पैकेज आयात करते हैं। इस पैकेज में वर्ग ITesseract है और इस वर्ग में एक विधि है जो छवि का OCR करती है।
    2. दूसरी बात, हमें java.io पैकेज को इम्पोर्ट करना है, इस क्लास में फाइल क्लास है। इस क्लास के द्वारा हम किसी भी इमेज फाइल को उसके एड्रेस से इम्पोर्ट कर सकते हैं।
    3. अब एक क्लास ऐप बनाएं।
    4. क्लास ऐप क्रिएट मेथड में getImageText और यह मेथड आपके सिस्टम में इमेज की लोकेशन प्राप्त करता है।
    5. अब मैं ITesseract वर्ग, iT का ऑब्जेक्ट बनाता हूं।
    6. इसके बाद मैं फ़ाइल नहीं मिली की त्रुटि का पता लगाने के लिए एक कोशिश ब्लॉक लिखता हूं। इस भाग में हम doOCR विधि द्वारा छवि का OCR करते हैं और एक्सट्रेक्ट टेक्स्ट को String imgText वेरिएबल में लेते हैं।
    7. यदि कोई त्रुटि पाई जाती है तो कैच ब्लॉक शो।
    8. अब मैंने एक मुख्य विधि बनाई। जिसमें हम ऐप क्लास, ऐप का ऑब्जेक्ट बनाते हैं और getImgText मेथड कहते हैं।
    9. इसके बाद मैं इमेज लोकेशन पास करता हूं और इमेज के एक्सट्रैक्ट टेक्स्ट को कंसोल में प्रिंट करता हूं।
  2. for MORE interesting articles please go to this link https:/wonderdeals4u.blogspot.com

--

--

Raikwarbeni
Raikwarbeni

No responses yet