Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Tesseract from java

I'm trying to build a sample application in java that will read an image file and just output the text extracted from the image. I found the Tesseract project which seems promising, however, its in c++. In order to use it, should I simply run it as a command line from my java app Runtime.exec(...) ? Or is there a better solution, maybe a JAR? Additionally, this is just a sample app, would running it as a command line app be a concern from scalability perspective?

like image 390
Omnipresent Avatar asked Dec 20 '12 14:12

Omnipresent


People also ask

Does Tesseract use Java?

Tess4J is a Java wrapper for the Tesseract APIs that provides OCR support for various image formats like JPEG, GIF, PNG, and BMP. Then, we can use the Tesseract class provided by tess4j to process the image: File image = new File("src/main/resources/images/multiLanguageText.

What is Tesseract OCR Java?

Tesseract OCR is an optical character reading engine developed by HP laboratories in 1985 and open sourced in 2005. Since 2006 it is developed by Google.

What is OCR in Java?

OCR for Java allows you to extract text from images, screenshots, specific areas of an image, and create searchable PDFs from scanned files on any platform that supports Java. With its powerful yet easy-to-use API, even the complex OCR tasks take less than 10 lines of code.

Can I use Tesseract?

Tesseract is an open source text recognition (OCR) Engine, available under the Apache 2.0 license. It can be used directly, or (for programmers) using an API to extract printed text from images. It supports a wide variety of languages.


2 Answers

Now tesseract is provided by the javacv project, this is a far better option than using Tess4J since all that is required is adding a single dependency to your pom file, the native libs for your platform will then be downloaded and linked automatically for you by the javacv tesseract version.

I've created an example maven project here - https://github.com/piersy/BasicTesseractExample

and also an example gradle project here - https://github.com/piersy/BasicTesseractExampleGradle

For this to work on my ubuntu machine I needed to update my install of libstdc++6

I achieved this by running the following although just installing libstdc++6 may work for you.

sudo add-apt-repository ppa:ubuntu-toolchain-r/test  sudo apt-get update sudo apt-get install libstdc++6 

Note the gradle project does not perform the automatic install but is is still a hell of a lot simpler than using Tess4J

The javacv project is here - https://github.com/bytedeco/javacpp-presets/tree/master/tesseract

Big props to the javacv guys, only wish I'd found this earlier as it would have saved me a week of getting tess4j to work on multiple platforms!

like image 129
PiersyP Avatar answered Oct 10 '22 19:10

PiersyP


I have used the tesseract project in my java code. All you need to do is

  1. Get the tess4j jni wrapper for tesseract.
  2. Open the tess4j proj in your ide and add the source packages and libs into your own
    project.
  3. Write the code creating an instance for the tesseract class and then use it for
    performing the OCR.

Please have a look into this http://tphangout.com/?p=18

It gives instructions on how to build a java project to read an image and convert it into text using the tesseract OCR API.

like image 41
Raja Yogan Avatar answered Oct 10 '22 18:10

Raja Yogan