Tess4J Documentation
repository·master·Indexed 23 days ago
https://github.com/nguyenq/tess4jA 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.
What's inside Tess4J
- Tess4J is a Java JNA (Java Native Access) wrapper for the Tesseract OCR API. It allows Java applications to perform optical character recognition (OCR) on various image and document formats.
Supported image and document formats in Tess4J
masterTess4J provides OCR support for the following formats:
- TIFF, JPEG, GIF, PNG, and BMP image formats
- Multi-page TIFF images
- PDF document format
Configure Windows dependencies for Tess4J
masterIf 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.Overview of Tess4J
masterTess4J is a JNA (Java Native Access) wrapper for the Tesseract OCR API. It provides character recognition support for common image formats, multi-page images, and PDF documents. The library is designed to work on both Windows and Linux environments.Software Requirements for Tess4J
masterTo 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.Configure Tesseract Language Data (tessdata)
masterTesseract requires language data files to perform OCR.
- Download language data packs from the Tesseract tessdata repository.
- Decompress the files.
- Place them into a folder named
tessdata. - Ensure this folder is accessible to your application. You can point the
ITesseractinstance to this folder usinginstance.setDatapath(path).
Perform OCR on an image with Tesseract
masterTo perform OCR, instantiate an
ITesseractobject (typically using theTesseractclass) and call thedoOCR(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()); } } }Use ITesseract interface
masterThe primary entry point for the library is the
ITesseractinterface.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 aString. ThrowsTesseractExceptionif an error occurs.instance.setDatapath(String path): Sets the path to thetessdatafolder containing language files.