Development with Tess4J

Tesseract, Ghostscript, and Leptonica Windows 32- and 64-bit DLLs are embedded in tess4j.jar and lept4j.jar, respectively. They will automatically be extracted and loaded at run-time. For other platforms, be sure to install or build Tesseract first.

NetBeans

Create a new Java Application project. In Projects view, click on Libraries node, and select Add JAR/Folder.... Find and add all the required JAR files, including jai_imageio.jar, jna.jar, commons-io-2.4.jar, lept4j.jar, and tess4j.jar. Add a new Java Class file named TesseractExample with appropriate package name

NetBeans IDE

Place tessdata and sample images at the project's root directory. If you select Files view, it should display as follows:

NetBeans Files view

You can configure NetBeans to launch with a JDK 64-bit to run the example; this can be done by adjusting netbeans_jdkhome value in NetBeans\etc\netbeans.conf file.

Eclipse

Create a new project. In project's Properties window, select Java Build Path > Add External JARs... and add the required JARs: jai_imageio.jar, jna.jar, commons-io-2.4.jar, lept4j.jar, and tess4j.jar. Now add a new Class named TesseractExample as shown. Place tessdata, and sample images in the project's root directory.

Eclipse IDE

To run with a JVM 64-bit, configure Eclipse to launch with an Alternate JRE 64-bit to run the example.

Eclipse JRE 64-bit Configuration

Command-line on Windows 7 64-bit

Create a working directory with content and structure as below. Place all the dependent JAR files in lib subdirectory.

Command-line example

Create a Java source file TesseractExample.java in the appropriate directory.

Command-line example

Type %windir%\SysWoW64\cmd in Start Search box to open a 32-bit command prompt. Navigate to the project directory and execute the following commands:

C:\Projects\TessExample>javac -cp lib\* tess4j\example\TesseractExample.java

C:\Projects\TessExample>java -cp lib\*;. tess4j.example.TesseractExample
The (quick) [brown] {fox} jumps!
Over the $43,456.78 #90 dog
& duck/goose, as 12.5% of E-mail
from aspammer@website.com is spam.
...

If you want to use your customized version of the DLLs, you will need to make your target library available to your Java program by setting the jna.library.path system property or changing the appropriate library access environment variable before launching the VM (see Getting Started with JNA).

package tess4j.example;

import java.io.File;
import net.sourceforge.tess4j.*;

public class TesseractExample {
    public static void main(String[] args) {
        // System.setProperty("jna.library.path", "32".equals(System.getProperty("sun.arch.data.model")) ? "lib/win32-x86" : "lib/win32-x86-64");

        File imageFile = new File("eurotext.tif");
        ITesseract instance = new Tesseract();  // JNA Interface Mapping
        // ITesseract instance = new Tesseract1(); // JNA Direct Mapping
        // File tessDataFolder = LoadLibs.extractTessResources("tessdata"); // Maven build bundles English data
        // instance.setDatapath(tessDataFolder.getParent());

        try {
            String result = instance.doOCR(imageFile);
            System.out.println(result);
        } catch (TesseractException e) {
            System.err.println(e.getMessage());
        }
    }
}