java.lang.UnsatisfiedLinkError: org.opencv.core.Mat.n_Mat(IIILjava/nio/ByteBuffer;)J

huangapple go评论78阅读模式
英文:

java.lang.UnsatisfiedLinkError: org.opencv.core.Mat.n_Mat(IIILjava/nio/ByteBuffer;)J

问题

我正在使用JavaFX和JavaCV开发项目。我的应用程序中的主要功能基于网络摄像头捕获。我正在寻找一种将OpenCVFrameGrabber的帧显示到JavaFX组件中的方法。

我找到了一种方法,但是我遇到了以下错误:

Exception in thread "main" java.lang.UnsatisfiedLinkError: org.opencv.core.Mat.n_Mat(IIILjava/nio/ByteBuffer;)J
	at org.opencv.core.Mat.n_Mat(Native Method)
	at org.opencv.core.Mat.<init>(Mat.java:52)
	at org.bytedeco.javacv.OpenCVFrameConverter.convertToOrgOpenCvCoreMat(OpenCVFrameConverter.java:187)
	at org.bytedeco.javacv.OpenCVFrameConverter$ToOrgOpenCvCoreMat.convert(OpenCVFrameConverter.java:61)
	at TestJavaCV$Companion.main(TestJavaCV.kt:20)
	at TestJavaCV.main(TestJavaCV.kt)

我怀疑应用程序不起作用是由于依赖项失败造成的。可能我应该为org.opencv.core提供附加的库,但是我不知道如何在Gradle中实现这一点。

最小可复现示例:

build.gradle

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile group: 'org.bytedeco', name: 'javacv-platform', version: '1.5.3'
}

TestJavaCV.kt

import javafx.scene.image.Image
import org.bytedeco.javacv.Frame
import org.bytedeco.javacv.OpenCVFrameConverter
import org.bytedeco.javacv.OpenCVFrameGrabber
import org.opencv.core.Mat
import org.opencv.core.MatOfByte
import org.opencv.imgcodecs.Imgcodecs
import java.io.ByteArrayInputStream

class TestJavaCV {
    companion object {
        private val toOrgOpenCvCoreMat = OpenCVFrameConverter.ToOrgOpenCvCoreMat()

        @JvmStatic
        fun main(args: Array<String>) {
            val grabber: OpenCVFrameGrabber = OpenCVFrameGrabber(0);
            grabber.start()

            val frame: Frame = grabber.grabFrame()
            val mat: Mat = toOrgOpenCvCoreMat.convert(frame)
            val matOfByte: MatOfByte = MatOfByte()

            Imgcodecs.imencode(".png", mat, matOfByte)
            val image = Image(ByteArrayInputStream(matOfByte.toArray()))
        }
    }
}
英文:

I'm work on project using JavaFX and JavaCV. My main feature in app is based on webcam capture. I'm looking for a way to display frame from OpenCVFrameGrabber into JavaFX component.

I found a way to do it but I get the following error:

Exception in thread &quot;main&quot; java.lang.UnsatisfiedLinkError: org.opencv.core.Mat.n_Mat(IIILjava/nio/ByteBuffer;)J
	at org.opencv.core.Mat.n_Mat(Native Method)
	at org.opencv.core.Mat.&lt;init&gt;(Mat.java:52)
	at org.bytedeco.javacv.OpenCVFrameConverter.convertToOrgOpenCvCoreMat(OpenCVFrameConverter.java:187)
	at org.bytedeco.javacv.OpenCVFrameConverter$ToOrgOpenCvCoreMat.convert(OpenCVFrameConverter.java:61)
	at TestJavaCV$Companion.main(TestJavaCV.kt:20)
	at TestJavaCV.main(TestJavaCV.kt)

I suspect that the application is not working due to not dependency failure. Probably I should provide additional libs for org.opencv.core but I don't know how to achieve this in gradle.

Minimal, Reproducible Example:

build.gradle

dependencies {
    implementation &quot;org.jetbrains.kotlin:kotlin-stdlib-jdk8&quot;
    testCompile group: &#39;junit&#39;, name: &#39;junit&#39;, version: &#39;4.12&#39;
    compile group: &#39;org.bytedeco&#39;, name: &#39;javacv-platform&#39;, version: &#39;1.5.3&#39;
}

TestJavaCV.kt

import javafx.scene.image.Image
import org.bytedeco.javacv.Frame
import org.bytedeco.javacv.OpenCVFrameConverter
import org.bytedeco.javacv.OpenCVFrameGrabber
import org.opencv.core.Mat
import org.opencv.core.MatOfByte
import org.opencv.imgcodecs.Imgcodecs
import java.io.ByteArrayInputStream

class TestJavaCV {
    companion object {
        private val toOrgOpenCvCoreMat = OpenCVFrameConverter.ToOrgOpenCvCoreMat()

        @JvmStatic
        fun main(args: Array&lt;String&gt;) {
            val grabber: OpenCVFrameGrabber = OpenCVFrameGrabber(0);
            grabber.start()

            val frame: Frame = grabber.grabFrame()
            val mat: Mat = toOrgOpenCvCoreMat.convert(frame)
            val matOfByte: MatOfByte = MatOfByte()

            Imgcodecs.imencode(&quot;.png&quot;, mat, matOfByte)
            val image = Image(ByteArrayInputStream(matOfByte.toArray()))
        }
    }
}

答案1

得分: 1

这个问题(链接:https://stackoverflow.com/questions/42569126/java-lang-unsatisfiedlinkerror-org-opencv-core-mat-n-matiiij)回答了为什么会出现“UnsatisfiedLinkError”错误;OpenCV库没有被您的Java虚拟机加载,因此无法进行相关的本地调用。

这个问题(链接:https://stackoverflow.com/questions/17767557/how-to-use-opencv-in-using-gradle)应该可以帮助您配置使用Gradle的OpenCV。

根据您的评论问题:“我需要让应用程序在Windows 10、Linux和MacOS上运行。” 我不知道如何使用Gradle解决这个问题,但是在Java中,我会根据不同的操作系统插入不同的系统属性,并在您的类中的静态块中使用它来加载库:

static {

    String libraryPath = System.getProperty("opencv.path");

    System.load(libraryPath);

}
英文:

This question https://stackoverflow.com/questions/42569126/java-lang-unsatisfiedlinkerror-org-opencv-core-mat-n-matiiij answers why you are getting that UnsatisfiedLinkError; the library for OpenCV is not loaded by your Java VM and therefore the relevant native calls cannot be made.

This question https://stackoverflow.com/questions/17767557/how-to-use-opencv-in-using-gradle should help you with configuring OpenCV using Gradle.

Per your comment question "I have a requirement for the application to work on Windows 10, Linux and MacOS." I don't know how to solve that using gradle, however in Java I would inject a different system property per o/s and use that in a static block in your class to load your library:

static {

    String libraryPath = System.getProperty(&quot;opencv.path&quot;);

    System.load(libraryPath);

}

huangapple
  • 本文由 发表于 2020年7月23日 22:04:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63056169.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定