B4J Question Tesseract OCR Error

Philip Chatzigeorgiadis

Active Member
Licensed User
Longtime User
I am following this tutorial.


I have this code

B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
   
   #AdditionalJar:lept4j-1.19.0.jar
    #AdditionalJar:slf4j-api-1.7.32.jar
    #AdditionalJar:slf4j-simple-1.7.32.jar
    #AdditionalJar:jai-imageio-core-1.4.0.jar
    #AdditionalJar:jna-5.14.0.jar
    #AdditionalJar:tess4j-5.10.0.jar
   
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private Button1 As B4XView
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show   
End Sub

Sub Button1_Click
    Log(File.Exists(File.DirApp,"card1.png"))
    (Me).As(JavaObject).RunMethod("OCRImage",Array(File.Combine(File.DirApp,"card1.png")))   
End Sub

#If JAVA
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import java.io.IOException;
import net.sourceforge.tess4j.*;
import net.sourceforge.tess4j.ITessAPI;
import net.sourceforge.tess4j.Word;
import java.util.List;

public static void OCRImage(String imgPath) {
        ITesseract tesseract = new Tesseract();
        tesseract.setDatapath("objects\tessdata"); // Set the path to tessdata folder Objects\tessdata)
        try {
            // Load an image file
            BufferedImage image = ImageIO.read(new File(imgPath));
         
            // Perform OCR on the image
            String text = tesseract.doOCR(image);
         
            // Print the extracted text
            System.out.println("Extracted Text: " + text);

        } catch (TesseractException | IOException e) {
            e.printStackTrace();
        }
    }

#End If

The card1.png file is in the objects folder.
All additional jar files are in the Additional Libraries folder (I had to add #AdditionalJar:slf4j-simple-1.7.32.jar, to get rid of an initial error).

I am not sure which folder from tesseract-main download I should put into the Objects folder or how to reference it. The instructions in the tutorial link above mention "Download Tesseract-OCR and place the folder in Objects" but the inline java code mentions the tessdata folder.
For now I have the tessdata folder (a subfolder in the tesseract-main download) copied in the Objects folder and referencing it per above (in the inline java code). But I have made various combinations. I always get the same error (I have no idea what that garbled string in the error might be):

B4X:
true
main._button1_click (java line: 73)
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:119)
    at java.base/java.lang.reflect.Method.invoke(Method.java:578)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
    at b4j.example.main._button1_click(main.java:73)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
    at java.base/java.lang.reflect.Method.invoke(Method.java:578)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
    at anywheresoftware.b4a.BA$1.run(BA.java:236)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
    at java.base/java.lang.Thread.run(Thread.java:1589)
Caused by: java.lang.UnsatisfiedLinkError: Δεν ήταν Ξ΄Ο…Ξ½Ξ±Ο„Ο Ξ½Ξ± εντοπιστεί Ξ· ΞΊΞ±ΞΈΞΏΟισμΞΞ½Ξ· λειτ. μονάδα.
    at com.sun.jna.Native.open(Native Method)
    at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:300)
    at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:481)
    at com.sun.jna.Library$Handler.<init>(Library.java:197)
    at com.sun.jna.Native.load(Native.java:618)
    at com.sun.jna.Native.load(Native.java:592)
    at net.sourceforge.tess4j.util.LoadLibs.getTessAPIInstance(LoadLibs.java:83)
    at net.sourceforge.tess4j.TessAPI.<clinit>(TessAPI.java:42)
    at net.sourceforge.tess4j.Tesseract.init(Tesseract.java:518)
    at net.sourceforge.tess4j.Tesseract.doOCR(Tesseract.java:362)
    at net.sourceforge.tess4j.Tesseract.doOCR(Tesseract.java:311)
    at net.sourceforge.tess4j.Tesseract.doOCR(Tesseract.java:280)
    at b4j.example.main.OCRImage(main.java:112)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
    ... 14 more

What am I missing?
 
Top