Search code examples
androidopencvunsatisfiedlinkerror

Android for OpenCV - error opening trace file, UnsatisfiedLinkError


I'm new to both Android development and OpenCV. I downloaded the OpenCV library for Android from http://sourceforge.net/projects/opencvlibrary/files/opencv-android/ and set up a phone using the Virtual Device Manager. When I try to run "OpenCV Tutorial 0 - Android Camera" in Eclipse, the phone's screen says "Fatal error: can't open camera!" and I get these errors in the log:

08-07 15:02:57.322: E/Trace(708): error opening trace file: No such file or directory (2)
08-07 15:02:57.772: E/Sample::SurfaceView(708): Can't open camera!

I also tried running a simple project:

package com.example;

import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;

    public class Test{
        public static void main(String[] args) {
            Mat example = Highgui.imread("/image.jpg");
        return;
    }
}

which gives me the error

Exception in thread "main" java.lang.UnsatisfiedLinkError: org.opencv.highgui.Highgui.imread_1(Ljava/lang/String;)J

How can I fix these errors? On a side note, /image.jpg looks in the parent directory of the project, not the SD card on the Android virtual device, right?

Thanks so much.


Solution

  • After a bunch of searching, I found this solution for the UnsatisfiedLinkError (which came up again after I thought it was fixed - see here):

    "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:

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

    This code snippet should go right after:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_load_image);
    

    Now it works!