9951 explained code solutions for 126 technologies


tesseract-ocrHow can I integrate Tesseract OCR with Java using Maven?


Tesseract OCR can be integrated with Java using Maven by following these steps:

  1. Add the Tesseract OCR dependency to the project's pom.xml file:
<dependency>
    <groupId>net.sourceforge.tess4j</groupId>
    <artifactId>tess4j</artifactId>
    <version>3.4.8</version>
</dependency>
  1. Download the Tesseract OCR language data files and add to the project's resources folder.
  2. Create a Java class for the OCR implementation.
import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;

public class TesseractExample {
    public static void main(String[] args) {
        ITesseract instance = new Tesseract();
        instance.setDatapath("<path_to_data_files>");
        try {
            String result = instance.doOCR(new File("<image_file>"));
            System.out.println(result);
        } catch (TesseractException e) {
            System.err.println(e.getMessage());
        }
    }
}
  1. Output of the above code will be the extracted text from the image.
  2. Build the project using mvn clean install.
  3. Execute the project using mvn exec:java.

Helpful links

Edit this code on GitHub