Unsatisfied Link Error - OpenCV for Android Non-native

≯℡__Kan透↙ 提交于 2019-11-27 14:40:29

After a bunch of searching, I found this:

"3. If your application project doesn’t have a JNI part, just copy the corresponding OpenCV native libs from /sdk/native/libs/ to your project directory to folder libs/."

So that means copy the \armeabi, \armeabi-v7a, and \x86 folders.

"4. The last step of enabling OpenCV in your application is Java initialization code before call to OpenCV API. It can be done, for example, in the static section of the Activity class, which gets executed only once, before any instance of the class is created:

static {
    if (!OpenCVLoader.initDebug()) {
        // Handle initialization error
    }
}

Alternatively, you can put it inside the onCreate method:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_load_image);
    if (!OpenCVLoader.initDebug()) {
        // Handle initialization error
    }
    [...]
}

Now it works!

you should use

if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack))
{
    Log.e("TEST", "Cannot connect to OpenCV Manager");
}

in OnCreate() And use

private BaseLoaderCallback  mOpenCVCallBack = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        switch (status) {
                case LoaderCallbackInterface.SUCCESS:
                {
                               Mat Image = Highgui.imread("/image.jpg");
                               if (Image == null) {
                                   AlertDialog ad = new AlertDialog.Builder(this).create(); 
                                   ad.setMessage("Fatal error: can't open /image.jpg!");  
                                }
                } break;
                default:
                {
                    super.onManagerConnected(status);
                } break;
            }
    }
    };

In most situations, a line like this before calling openCV is enough: "System.loadLibrary(Core.NATIVE_LIBRARY_NAME);"

The problem is that you are using Highgui.imread method before the OpenCV4Android library even finishes loading. Android calls the "onCreate" method before loading the OpenCV4Android library. So, create a separate method for your OpenCV code like this :-

public class Start extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_start);
   }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_start, menu);
    return true;
   }

public void readImage {
    Mat Image = Highgui.imread("/image.jpg");
    if(Image=null) {
    Log.i("Start", "--------Image Cannot be Loaded--------");
    else if(!Image=null) {
    Log.i("Start", "--------Image Loaded Successfully--------");
   }

}

I was adding opencv in my project in Android Studio. This error occurs when native files are not available at run time. So you have to copy native files at correct location.

First create the jniLibs at this location /app/src/main/ location and copy the all the folder with *.so files (armeabi, armeabi-v7a, mips, x86) in the jniLibs from the OpenCV SDK and make your gradle plugin above 0.7.2+

The link in the answer was not working and i had to dig around for sometime for the solution that worked for me.

I first had a BaseLoaderCallback defined in the class

private BaseLoaderCallback  mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS:
            {
                Log.i(TAG, "OpenCV loaded successfully");
               // any immediate code for using OpenCV
            } break;
            default:
            {
                super.onManagerConnected(status);
            } break;
        }
    }
};

Then in onResume function i had:

@Override
public void onResume()
{
    super.onResume();
    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);

    }
}

Make sure of the following

1.You are changing the OPENCV_VERSION_3_0_0 as per your version

  1. Not to run any opencv library before loading. Even in onCreate(), opencv has not yet loaded. Better to put it in the onManagerConnected() function in the switch case where OpenCV has loaded successfully.

In my case I paste the opencv classes in wrong package name

Wrong one

I paste the opencv classes into com.opencv -> all classes

Correct one

org.opencv -> all classes

After change this right package name it will works.

Reason - they may refer "org.opencv.."

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!