How does Java load the OpenCV so library on LINUX?

PHPz
Release: 2023-05-26 17:18:46
forward
1847 people have browsed it

This example may not work. The reason is the problem of dependent library loading. If libopencv_java.so:

  • contains all other so functions, the above blog post is correct.

  • If it is not included, certain loading skills are required.

The code example is as follows:

package taishan;
 
import java.io.File;
import java.util.LinkedList;
import java.util.List;

import org.opencv.core.CvType;
import org.opencv.core.Mat;
 
@SuppressWarnings("serial")
public class OpenCVTest
{
    public final static String LIB_PATH = "/home/wuxi/eclipse-workspace/OpenCVTest/libs/bin";
    
    private static List<File> getOpenCVFiles(final String dirName)
    {
        if (dirName == null)
        {
            return null;
        }
        File dir = new File(dirName);
        if (!dir.exists() || !dir.isDirectory())
        {
            return null;
        }
        
        File[] files = dir.listFiles();
        List<File> fileList = new LinkedList<File>();
        for (File file : files)
        {
            String name = file.getName();
            if (   name.startsWith("lib") && name.endsWith(".so"))
            {
                fileList.add(file);
            }
        }
        return fileList;
    }

    private static void loadNativeOpenCV(final String dirName)
    {
        List<File> fileList = getOpenCVFiles(dirName);
        if (fileList == null || fileList.size() == 0)
        {
            return;
        }
        
        while (fileList.size() > 0)
        {
            for (int i=0; i<fileList.size(); i++)
            {
                File file = fileList.get(i);
                try
                {
                    System.load(file.getAbsolutePath());
                }
                catch (java.lang.Throwable e)
                {
                    continue;
                }
                
                fileList.remove(i);
                i--;
            }
        }
        
        //如果libopencv_java.so在另外目录,需要单独加载
        //System.load(Dir+"/"+"lib"+Core.Core.NATIVE_LIBRARY_NAME+"."+LIB_SUFFIX_LINUX);
    }
    
    public static void main(String[] args)
    {
        loadNativeOpenCV(LIB_PATH);
        
        Mat m  = Mat.eye(3, 3, CvType.CV_8UC1);  
        System.out.println("m = " + m.dump());  
    }
}
Copy after login

The key sentence is to find the so package correctly.

The above is the detailed content of How does Java load the OpenCV so library on LINUX?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template