praxmon
praxmon

Reputation: 5121

Display image on Android using OpenCV

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?

Upvotes: 0

Views: 10706

Answers (3)

timegalore
timegalore

Reputation: 731

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.

Upvotes: 4

berak
berak

Reputation: 39796

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.

Upvotes: 0

18446744073709551615
18446744073709551615

Reputation: 16832

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

Upvotes: 3

Related Questions