0

I am learning to develop android apps and I am trying to incorporate OpenCV in the apps. I decided to make a simple app that displays an image. The code is given below:

package com.example.first;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Canvas;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceView;
import android.view.WindowManager;
import android.webkit.WebView.FindListener;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity{

    Mat m=Highgui.imread("C:/Users/Administrator/Desktop/circle1.png");

}

On checking the logcat the following errors were being displayed:

E/AndroidRuntime(1310): FATAL EXCEPTION: main

E/AndroidRuntime(1310): java.lang.UnsatisfiedLinkError: Native method not found: org.opencv.highgui.Highgui.imread_1:(Ljava/lang/String;)

E/AndroidRuntime(1310): at org.opencv.highgui.Highgui.imread_1(Native Method)

E/AndroidRuntime(1310): at org.opencv.highgui.Highgui.imread(Highgui.java:359)

E/AndroidRuntime(1310): at com.example.run.MainActivity.(MainActivity.java:26)

E/AndroidRuntime(1310): at java.lang.Class.newInstanceImpl(Native Method)

E/AndroidRuntime(1310): at java.lang.Class.newInstance(Class.java:1130)

E/AndroidRuntime(1310): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)

E/AndroidRuntime(1310): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)

E/AndroidRuntime(1310): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)

E/AndroidRuntime(1310): at android.app.ActivityThread.access$600(ActivityThread.java:141)

E/AndroidRuntime(1310): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)

E/AndroidRuntime(1310): at android.os.Handler.dispatchMessage(Handler.java:99)

E/AndroidRuntime(1310): at android.os.Looper.loop(Looper.java:137)

E/AndroidRuntime(1310): at android.app.ActivityThread.main(ActivityThread.java:5103)

E/AndroidRuntime(1310): at java.lang.reflect.Method.invokeNative(Native Method)

E/AndroidRuntime(1310): at java.lang.reflect.Method.invoke(Method.java:525)

E/AndroidRuntime(1310): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)

E/AndroidRuntime(1310): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)

E/AndroidRuntime(1310): at dalvik.system.NativeStart.main(Native Method)

What is wrong with this code?

4
  • Are you able to make a hello-world app display an image? (Say, the stackoverflow logo?) Commented Jan 16, 2014 at 10:11
  • @18446744073709551615 In the activity_main.xml file, when I view it in the graphical layout, I can see the image but when I run it on the emulator it does not display the image. Commented Jan 16, 2014 at 10:14
  • If you are not able to write an app displaying an image, this is a different question, unrelated to opencv. (You are welcome to search Google for examples or ask here if you bump into some problem that nobody else has encountered.) Once you manage to display an image, you will have to convert Mat to something that Android understands by itself (e.g. make a bitmap drawable, or save the Mat as a png) and display it. Commented Jan 16, 2014 at 10:20
  • @18446744073709551615 Checked the error in the logcat and it says that the function imread() cannot be found. So it seems it is related to OpenCV. Commented Jan 16, 2014 at 10:43

3 Answers 3

4

If you are relatively new to opencv on Android I strongly suggest you start with the sample that comes with the opencv android SDK called image-manipulations. There is only one java file ImageManipulationsActivity.java but it has samples of all the typical opencv operations such as Canny etc. It also shows you how to do the async loading of opencv. If you can get that to work on your device then you are in a good starting position.

Note that opencv on Android needs the opencv manager and uses native libraries and so trying to run on an emulator may not be that successful; I would recommend testing on your device when developing.

As others have said, you need to copy the image file over to your device to read it. I use Eclipse DDMS File Explorer and put the files in mnt/sdcard. I then use the following function to load (it could do with more error checking but this should illustrate the idea):

public Mat loadImageFromFile(String fileName) {

    Mat rgbLoadedImage = null;

    File root = Environment.getExternalStorageDirectory();
    File file = new File(root, fileName);

    // this should be in BGR format according to the
    // documentation.
    Mat image = Highgui.imread(file.getAbsolutePath());

    if (image.width() > 0) {

        rgbLoadedImage = new Mat(image.size(), image.type());

        Imgproc.cvtColor(image, rgbLoadedImage, Imgproc.COLOR_BGR2RGB);

        if (DEBUG)
            Log.d(TAG, "loadedImage: " + "chans: " + image.channels()
                    + ", (" + image.width() + ", " + image.height() + ")");

        image.release();
        image = null;
    }

    return rgbLoadedImage;

}

you'll see it comes in as BGR format so I convert to RGB for my purposes.

Getting your Mat to then display on Android I think has been covered by previous questions.

Sign up to request clarification or add additional context in comments.

1 Comment

Good answer, but just for the record, android doesn't need the OpenCV manager. You can include all the opencv native libs for your device in your apk, and then tell your program to look there with initDebug(). But I've read that using the OpenCV manager with initAsync() is the preferred way.
3

"C:/Users/Administrator/Desktop/circle1.png" is a Windows path, not an Android path

2 Comments

So what is the edit that I am supposed to make? Copy the image in the res folder in drawable and then try to give its path? Tried that and it is not working.
OMG! You are expected to use adb push and deliver the file to the (virtual or real) device in the first place. And when you manage to do that, you will know the path.
0

please, before typing code blindly into your ide, and then nagging SO with the outcome,

have a look the opencv android docs , demos / tutorials

you can't use any opencv functionality, before OpenCVLoader.initAsync() finished ( loading the opecv so's)

you're not even calling it, so go back at reading the docs.

2 Comments

That is the thing. I did and didn't find anything that might help. Calm down and help mate, if you cannot then do not comment/answer and waste time. Thanks for nothing.
Like I said, I CHECKED. Did not find the solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.