在Android Java应用程序中使用OpenCV Mat. OpenCV Mat出现NoClassDefFound错误 [英] Use OpenCV Mat in Android Java Application. NoClassDefFound Error with OpenCV Mat

查看:334
本文介绍了在Android Java应用程序中使用OpenCV Mat. OpenCV Mat出现NoClassDefFound错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Java开发一个Android应用程序,该应用程序在本机c库中调用一个函数.本机函数需要一个指向Opencv Mat类型的指针,因此我需要能够在Java应用程序中创建一个Opencv Mat类型,以传递给本机库.

I'm developing an Android application in java, that calls a function in a native c library. The native function requires a pointer to an Opencv Mat type so I need to be able to create a Opencv Mat type in my Java application to pass to the native library.

我正在使用Android Studio,并且已将"opencv库" .jar文件添加到项目的libs文件夹中.因此,我可以导入所需的Opencv模块并创建Opencv Mat类型/访问其功能.问题是在运行时出现以下错误:

I'm using Android Studio and I've added the 'opencv library' .jar file to my project's libs folder. So I can import the required Opencv modules and create an Opencv Mat type/access its functions. The problem is at runtime I'm getting the following error:

java.lang.NoClassDefFoundError: org.opencv.core.Mat

我已经阅读到Opencv .jar文件只是本机功能的包装,并且该应用程序需要本机库".我如何找到/添加?我需要添加什么到项目中,以便应用程序可以在运行时找到Opencv Mat类定义?我只想使用OpenCV Mat类型,别无其他.

I've read that the Opencv .jar file is just a wrapper around native functionality and the application needs 'the native library'. How do I find/add this? What do I need to add to my project so that the Application can find the Opencv Mat class definition at runtime? I only want to use the OpenCV Mat type and nothing else.

我已经阅读了有关设置OpenCV Android的教程,但是这些教程需要在Android设备上安装OpenCV Manager应用程序,但这对我来说可能是个问题,因为我无法访问将要安装我的应用程序的设备并且OpenCV Manager应用程序不会安装在这些设备上.相反,只有一个".so"库可以添加到我的Android Studio项目中,该库将与我的项目捆绑在一起,并允许该应用在运行时查找所需的Mat类吗?

I've read tutorials on setting up OpenCV Android, but these require installing the OpenCV Manager App on the Android device but this could be a problem for me because I won't have access to the devices where my app will be installed and the OpenCV Manager App won't be installed on these devices. Instead, is there just an '.so' library that I can add to my Android Studio project which will get bundled with my project and allow the app to find the required Mat class at runtime, which is all I need?

先谢谢了.

推荐答案

有3种方法可以在您的应用程序中包含OpenCV

There are 3 ways to include OpenCV in your app

  1. 使用NDK编译opencv并编写自己的包装程序以包含您的opencv c ++代码.
  2. 导入Android Opencv SDK提供的OpenCV Java包装器模块,并将其连接到手机上安装的Opencv管理器.
  3. 导入Android Opencv SDK提供的OpenCV Java包装器模块,然后将本地库复制到您的项目中.

对于选项1,您必须设置ndk项目.对于2、3,您只需导入opencv android模块并通过调用-

For option 1, you'll have to setup the ndk project. For 2, 3 you can simply import the opencv android module and initialize it in your code by calling -

    if(!OpenCVLoader.initDebug()) {
        Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization.");
        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mLoaderCallback);
    } else {
        Log.d(TAG, "OpenCV library found inside package using it.");
        mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
    }

请参阅以下链接,以在您的android studio项目中设置opencv android sdk-

Refer to following links to setup opencv android sdk in your android studio project -

http: //blog.codeonion.com/2015/11/25/creating-a-new-opencv-project-in-android-studio/

Android Studio中的OpenCV

大多数opencv功能都可以使用java包装器直接访问.因此,您可能不需要将任何Mat传递给本机C函数.如果仍要调用本机函数,则可以参考Opencv Java包装程序以获取帮助,甚至可以参考SDK随附的示例以进一步了解.

Most of the opencv functionalities can be directly accessed using the java wrapper. So you might not need to pass any Mat to native C functions. If you still want to call native functions, you can refer to Opencv Java wrapper for help and even samples provided with SDK for further understanding.

这篇关于在Android Java应用程序中使用OpenCV Mat. OpenCV Mat出现NoClassDefFound错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆