Tess4J Documentation

repository·master·Indexed 23 days ago

https://github.com/nguyenq/tess4j

A Java JNA wrapper for the Tesseract OCR API that enables optical character recognition on PDF documents and image formats including TIFF, JPEG, GIF, PNG, and BMP. It provides the ITesseract interface for performing OCR via the doOCR method and supports both Windows and Linux environments.

Tokens
1.1K
Snippets
1
Records
8
Agent score
33%

What's inside Tess4J

  1. Configure Windows dependencies for Tess4J

    master
    If you are running Tess4J on Windows, you must ensure that the Microsoft Visual C++ v14 Redistributable is installed. This is required because the Tesseract and Leptonica Windows binaries were built using the Visual Studio 2026 (v145) Platform Toolset.
  2. Software Requirements for Tess4J

    master

    To use Tess4J, ensure the following requirements are met:

    • Java Runtime Environment (JRE)
    • JNA (Java Native Access)
    • JAI-ImageIO
    • Visual C++ 2022 Redistributable Packages (Required on Windows because Tesseract DLLs were built with VS2022 v143)
    • PDFBox (Required if you need support for PDF documents)

    For Linux users, you may need to provide the Tesseract shared object library (libtesseract.so) if not using the bundled versions.

  3. Perform OCR on an image with Tesseract

    master

    To perform OCR, instantiate an ITesseract object (typically using the Tesseract class) and call the doOCR(File) method.

    Best Practices for Input Images:

    • Resolution: Scan images at 200 DPI to 400 DPI. 300 DPI is a typical recommended setting.
    • Format: Use monochrome (black & white) or grayscale.
    • File Types: Uncompressed TIFF or PNG are recommended. PNG is lossless and efficient, while TIFF supports multiple pages in a single file.
    package net.sourceforge.tess4j.example;
    
    import java.io.File;
    import net.sourceforge.tess4j.*;
    
    public class TesseractExample {
        public static void main(String[] args) {
            // ImageIO.scanForPlugins(); // for server environment
            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 only; only English data bundled
            // instance.setDatapath(tessDataFolder.getPath());
    
    try {
                String result = instance.doOCR(imageFile);
                System.out.println(result);
            } catch (TesseractException e) {
                System.err.println(e.getMessage());
            }
        }
    }
  4. Use ITesseract interface

    master

    The primary entry point for the library is the ITesseract interface.

    • ITesseract instance = new Tesseract();: Uses JNA Interface Mapping.
    • ITesseract instance = new Tesseract1();: Uses JNA Direct Mapping.
    • instance.doOCR(File imageFile): Performs optical character recognition on the provided file and returns the text as a String. Throws TesseractException if an error occurs.
    • instance.setDatapath(String path): Sets the path to the tessdata folder containing language files.