nguyenq / tess4j

Java JNA wrapper for Tesseract OCR API
Apache License 2.0
1.58k stars 372 forks source link

LoadLibs copies files into wrong directory when a custom value for jna.library.path is set #251

Closed moritzfl closed 6 months ago

moritzfl commented 1 year ago

In the following code, the resources are extracted to the temp folder. This works if jna.library.path is not set to a custom value because in that case, Tess4j actually sets jna.library.path to the temp directory.

However if another value for jna.library.path is defined, the resources are still copied to the temp folder but the jna.library.path is then composed of userCustomizedPath + File.pathSeparator + targetTempFolder.getPath() which does not seem to make sense to me.

The same behavior applies to Lept4j, but as of now, I have not opened an issue there ...

    static {
        System.setProperty("jna.encoding", "UTF8");
        String model = System.getProperty("sun.arch.data.model",
                                          System.getProperty("com.ibm.vm.bitmode"));
        String resourcePrefix = "32".equals(model) ? "win32-x86" : "win32-x86-64";
        File targetTempFolder = extractTessResources(resourcePrefix);
        if (targetTempFolder != null && targetTempFolder.exists()) {
            String userCustomizedPath = System.getProperty(JNA_LIBRARY_PATH);
            if (null == userCustomizedPath || userCustomizedPath.isEmpty()) {
                System.setProperty(JNA_LIBRARY_PATH, targetTempFolder.getPath());
            } else {
                System.setProperty(JNA_LIBRARY_PATH, userCustomizedPath + File.pathSeparator + targetTempFolder.getPath());
            }
        }
    }
nguyenq commented 12 months ago

jna.library.path can contain multiple paths, each is separated by File.pathSeparator. JNA will check all those locations to find and load the native resources.

So if you have your own custom Tesseract native library in some directory, setting the variable will tell JNA to look there.

zymgg commented 7 months ago

jna.library.path can contain multiple paths, each is separated by File.pathSeparator. JNA will check all those locations to find and load the native resources.

So if you have your own custom Tesseract native library in some directory, setting the variable will tell JNA to look there.

how set custom Tesseract. tess4j5.9/5.10 and Tesseract-OCR5.3.3.20231005 reslut are different, i need set custom Tesseract function please is windows system i set System.setProperty("jna.library.path", "D:\Tesseract-OCR"); or System.setProperty("jna.library.path", "D:\Tesseract-OCR\libtesseract-5.dll"); but result always different

nguyenq commented 7 months ago

@zymgg Make sure your custom DLL has the appropriate filename, which could vary based on the library version. Check LoadLibs class for more details.

zymgg commented 7 months ago

@zymgg Make sure your custom DLL has the appropriate filename, which could vary based on the library version. Check LoadLibs class for more details.

i see code, but i cant find my need, i create issue, help me please

nguyenq commented 6 months ago

The library is looking for a libtesseract533.dll to load. If your custom native library is libtesseract-5.dll, it won't be able to find it; you will have to change the referenced name in tess4j code. And it will require you to include all the dependencies (DLLs) that libtesseract-5.dll requires, such as Leptonica, etc.