java.lang.UnsatisfiedLinkError: 'long org.opencv.core.Mat.n_Mat()'

1.3k views Asked by At

I am trying to build my Java project with maven and opencv on Centos 8. With try some way to fix, i can build it as a .jar file to run API. But when my service call lib from opencv, it throws the following error: java.lang.UnsatisfiedLinkError: 'long org.opencv.core.Mat.n_Mat()' .

My reference to install opencv in centos 8: https://my.oschina.net/u/3568600/blog/4553084
This is my step:

  1. Install cmake and ant.
  2. Download: wget https://github.com/opencv/opencv/archive/4.4.0.zip and unzip it
  3. Run command: cd opencv
    mkdir build
    cd build
  4. Run cmake: cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -DBUILD_TESTS=OFF ..
  5. make
    sudo make install
    Outout after run this step: enter image description here
    Move lib: cp -r /usr/local/share/java/opencv4/libopencv_java440.so /usr/lib
  6. Check version: ldconfig -v | grep opencv Here my ouput: enter image description here

Here my java code to load core native lib:

  public static void main(String[] args) {
        System.load("/user/lib/libopencv_java440.so");
        SpringApplication.run(MyApp.class, args);
    }

My process throw error:

    public static Mat BufferedImage2Mat(BufferedImage image) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ImageIO.write(image, "png", byteArrayOutputStream);
        byteArrayOutputStream.flush();
        return Imgcodecs.imdecode(new MatOfByte(byteArrayOutputStream.toByteArray()), Imgcodecs.IMREAD_UNCHANGED); // Throw in this line
    }

What do you recommend I do next?
Note: my code running normal in local (Windows) with change libopencv_java440.so to opencv_java440.dll and add opencv-440.jar to class path in eclipse.

1

There are 1 answers

0
Duy Huỳnh On

I found simple way to resolve it.
In pom.xml add dependency:

<dependency>
    <groupId>org.opencv</groupId>
    <artifactId>opencv-440</artifactId>
    <version>4.4.0</version>            
</dependency>

Register opencv-440.jar to maven by command:
mvn install:install-file -Dfile=/path/opencv-440.jar -DgroupId=org.opencv -DartifactId=opencv-440 -Dversion=4.4.0 -Dpackaging=jar