Android NDK OpenCV - 未找到原生的实现

Android NDK OpenCV - No implementation found for native

你能帮我处理我的错误吗?我已经阅读了很多如何修复它们的提示,但没有任何帮助。我也知道已经问过类似的问题,但他们没有帮助我。所以我给你一个机会问你:

我正在尝试制作 Android 应用程序,该应用程序从图库中获取图像并调用用 C++ 编写的本机函数使其变灰。

我的主要activity是:

static {
        if(!OpenCVLoader.initDebug())
        {
            Log.e("tom", "Tomasku opencv problem static");
        }
        else
        {
            Log.e("tom", "OPENCV NACITANE");
            System.loadLibrary("halgorithm");
        }
    }

***在同一个Activity中我调用另一个activity(EditorActivity):

Intent i = new Intent(this, EditorActivity.class);
                ...
                i.putExtra("path", selectedImagePath); //path to image from gallery
                startActivity(i);

***在我的编辑器中Activity:

...
public native int fce(long matAddrRgba, long matAddrGray);
...

fce(image.getNativeObjAddr(), image_gray.getNativeObjAddr());
// and here it crashes

**在我的halgorithm.cpp

#include <jni.h>
#include <string.h>
#include <stdio.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc_c.h"

//using namespace std;
using namespace cv;

extern "C"
{

JNIEXPORT jint JNICALL Java_com_example_hematoma_MainActivity_fce(JNIEnv*, jobject, jlong addrRgba, jlong addrGray) {

    Mat& mRgb = *(Mat*)addrRgba;
    Mat& mGray = *(Mat*)addrGray;

    int conv;
    jint retVal;

    conv = toGray(mRgb, mGray);
    retVal = (jint)conv;

    return retVal;

}

}

int toGray(Mat img, Mat& gray)
{
    cvtColor(img, gray, CV_RGBA2GRAY); // Assuming RGBA input

    if (gray.rows == img.rows && gray.cols == img.cols)
    {
        return 1;
    }
    return 0;
}

**在我的Android.mk

include $(CLEAR_VARS)

OPENCV_INSTALL_MODULES:=on
OPENCV_CAMERA_MODULES:=on
include /home/nemesis/adt-bundle-linux-x86_64-20140702/OpenCV-2.4.10-android-sdk/sdk/native/jni/OpenCV.mk

LOCAL_MODULE := halgorithm
LOCAL_SRC_FILE := halgorithm.cpp

LOCAL_C_INCLUDES := /home/nemesis/adt-bundle-linux-x86_64-20140702/OpenCV-2.4.10-android-sdk/sdk/native/jni/include

include $(BUILD_SHARED_LIBRARY)

***LogCat

04-30 13:16:28.320: W/dalvikvm(3863): No implementation found for native Lcom/example/hematoma/EditorActivity;.fce (JJ)I
04-30 13:16:28.320: D/AndroidRuntime(3863): Shutting down VM
04-30 13:16:28.320: W/dalvikvm(3863): threadid=1: thread exiting with uncaught exception (group=0x40185760)
04-30 13:16:28.320: E/AndroidRuntime(3863): FATAL EXCEPTION: main
04-30 13:16:28.320: E/AndroidRuntime(3863): java.lang.UnsatisfiedLinkError: fce
04-30 13:16:28.320: E/AndroidRuntime(3863):     at com.example.hematoma.EditorActivity.fce(Native Method)
04-30 13:16:28.320: E/AndroidRuntime(3863):     at com.example.hematoma.EditorActivity.onClick(EditorActivity.java:166)
04-30 13:16:28.320: E/AndroidRuntime(3863):     at android.view.View.performClick(View.java:3110)
04-30 13:16:28.320: E/AndroidRuntime(3863):     at android.view.View$PerformClick.run(View.java:11934)
04-30 13:16:28.320: E/AndroidRuntime(3863):     at android.os.Handler.handleCallback(Handler.java:587)
04-30 13:16:28.320: E/AndroidRuntime(3863):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-30 13:16:28.320: E/AndroidRuntime(3863):     at android.os.Looper.loop(Looper.java:132)
04-30 13:16:28.320: E/AndroidRuntime(3863):     at android.app.ActivityThread.main(ActivityThread.java:4123)
04-30 13:16:28.320: E/AndroidRuntime(3863):     at java.lang.reflect.Method.invokeNative(Native Method)
04-30 13:16:28.320: E/AndroidRuntime(3863):     at java.lang.reflect.Method.invoke(Method.java:491)
04-30 13:16:28.320: E/AndroidRuntime(3863):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
04-30 13:16:28.320: E/AndroidRuntime(3863):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
04-30 13:16:28.320: E/AndroidRuntime(3863):     at dalvik.system.NativeStart.main(Native Method)

提前致谢!

******************************已编辑****************** *******

根据你的建议我修改了:

***在Android.mk中:

JNIEXPORT jint JNICALL 
Java_com_example_hematoma_**EditorActivity**_fce(JNIEnv*, jobject, jlong addrRgba, jlong addrGray) {...}

***在编辑器中Activity: 我在本机方法之前调用 System.loadLibrary("halgorithm")。

static {
        if(!OpenCVLoader.initDebug())
        {
            Log.e("tom", "Tomasku opencv problem static");
        }
        else
        {
            Log.e("tom", "OPENCV NACITANE");
            System.loadLibrary("halgorithm");
        }
    }

但这并没有解决我的错误。有同样的错误:/

***LogCat

04-30 13:36:21.760: W/dalvikvm(4255): No implementation found for native Lcom/example/hematoma/EditorActivity;.fce (JJ)I
04-30 13:36:21.760: D/AndroidRuntime(4255): Shutting down VM
04-30 13:36:21.760: W/dalvikvm(4255): threadid=1: thread exiting with uncaught exception (group=0x40185760)
04-30 13:36:21.760: E/AndroidRuntime(4255): FATAL EXCEPTION: main
04-30 13:36:21.760: E/AndroidRuntime(4255): java.lang.UnsatisfiedLinkError: fce
04-30 13:36:21.760: E/AndroidRuntime(4255):     at com.example.hematoma.EditorActivity.fce(Native Method)
04-30 13:36:21.760: E/AndroidRuntime(4255):     at com.example.hematoma.EditorActivity.onClick(EditorActivity.java:166)
04-30 13:36:21.760: E/AndroidRuntime(4255):     at android.view.View.performClick(View.java:3110)
04-30 13:36:21.760: E/AndroidRuntime(4255):     at android.view.View$PerformClick.run(View.java:11934)
04-30 13:36:21.760: E/AndroidRuntime(4255):     at android.os.Handler.handleCallback(Handler.java:587)
04-30 13:36:21.760: E/AndroidRuntime(4255):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-30 13:36:21.760: E/AndroidRuntime(4255):     at android.os.Looper.loop(Looper.java:132)
04-30 13:36:21.760: E/AndroidRuntime(4255):     at android.app.ActivityThread.main(ActivityThread.java:4123)
04-30 13:36:21.760: E/AndroidRuntime(4255):     at java.lang.reflect.Method.invokeNative(Native Method)
04-30 13:36:21.760: E/AndroidRuntime(4255):     at java.lang.reflect.Method.invoke(Method.java:491)
04-30 13:36:21.760: E/AndroidRuntime(4255):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
04-30 13:36:21.760: E/AndroidRuntime(4255):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
04-30 13:36:21.760: E/AndroidRuntime(4255):     at dalvik.system.NativeStart.main(Native Method)

public native int fce(...)EditorActivity 的成员,但在 C++ 代码中您调用函数 Java_com_example_hematoma_MainActivity_fce.

将其重命名为 Java_com_example_hematoma_EditorActivity_fce

不要忘记在调用本机函数之前调用 System.loadLibrary(),例如添加 activity:

static {
    System.loadLibrary("halgorithm");
}

经过几天的绝望:D 我解决了我的问题。问题是 $(OPENCV_INSTALL_MODULES) 应该相等。在我的 Android.mk 文件中的 OPENCV_INSTALL_MODULES:=on 行。

我覆盖了OPENCV_INSTALL_MODULES:=on

OPENCV_INSTALL_MODULES=开

现在可以用了....也许对其他人也有帮助 :)

如果您还有

,请关注 this 文章
unsatisfied link

Android studioopencv4

中出错

重命名几次即可解决问题。