Ask Your Question
1

Load and show image using Opencv on Android Studio

asked 2015-03-10 01:44:54 -0600

jossyy gravatar image

I try to create simple android app that shows an image. I can show the image without opencv.

  1. I copy the image under src/drawable folder.
  2. Content of activity_main.xml file
<RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"    tools:context=".MainActivity">
 <ImageView android:src="@drawable/logo"
 android:id="@+id/imageView1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" />
  </RelativeLayout>
  1. Content of onCreate method in MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   }

But I want to do this work using opencv functions. I use below code in onCreate function. I run the code on virtual device and get unfortunately, application has stopped. What is the wrong on this code?

Code :

// make a mat and draw something
  Mat m = Mat.zeros(100,400, CvType.CV_8UC3);
Core.putText(m, "hello world", new Point(30,80), Core.FONT_HERSHEY_SCRIPT_SIMPLEX, 2.2, new Scalar(200,200,0),2);
// convert to bitmap:
Bitmap bm = Bitmap.createBitmap(m.cols(), m.rows(),Bitmap.Config.ARGB_8888);
Utils.matToBitmap(m, bm);

// find the imageview and draw it!
ImageView iv = (ImageView) findViewById(R.id.imageView1);
iv.setImageBitmap(bm);

My logcat message is here;

 No implementation found for long org.opencv.core.Mat.n_zeros(int, int, int) (tried       Java_org_opencv_core_Mat_n_1zeros and Java_org_opencv_core_Mat_n_1zeros__III)
    at org.opencv.core.Mat.n_zeros(Native Method)
    at org.opencv.core.Mat.zeros(Mat.java:2431)
edit retag flag offensive close merge delete

Comments

you can't use any opencv code in onCreate(), because the native c++ libs are not loaded yet.

berak gravatar imageberak ( 2015-03-10 02:57:33 -0600 )edit

I download opencv-android-2.4.9-sdk. Should I use native c++ libs? To use opencv on android, what is the best way?

jossyy gravatar imagejossyy ( 2015-03-10 03:06:51 -0600 )edit

onCreate() is just the wrong place. see answer below.

berak gravatar imageberak ( 2015-03-10 03:13:30 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-03-10 03:11:50 -0600

berak gravatar image

updated 2015-03-10 03:12:44 -0600

again, just move your code out of onCreate(), you can only call opencv code after BaseLoaderCallback finished loading.:

public class PicActivity extends Activity  {

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

    private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            if (status == LoaderCallbackInterface.SUCCESS ) {
                // now we can call opencv code !
                helloworld();
            } else {
                super.onManagerConnected(status);
            }
        }
    };

    @Override
    public void onResume() {;
        super.onResume();
        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_5,this, mLoaderCallback);
        // you may be tempted, to do something here, but it's *async*, and may take some time,
        // so any opencv call here will lead to unresolved native errors.
    }

    public void helloworld() {
        // make a mat and draw something
        Mat m = Mat.zeros(100,400, CvType.CV_8UC3);
        Core.putText(m, "hi there ;)", new Point(30,80), Core.FONT_HERSHEY_SCRIPT_SIMPLEX, 2.2, new Scalar(200,200,0),2);

        // convert to bitmap:
        Bitmap bm = Bitmap.createBitmap(m.cols(), m.rows(),Bitmap.Config.ARGB_8888);
        Utils.matToBitmap(m, bm);
        // find the imageview and draw it!
        ImageView iv = (ImageView) findViewById(R.id.imageView1);
        iv.setImageBitmap(bm);
    }
}
edit flag offensive delete link more

Comments

I try this code on emulator. It says same message. "unfortunately, application has stopped"

jossyy gravatar imagejossyy ( 2015-03-10 03:20:55 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-03-10 01:44:54 -0600

Seen: 10,974 times

Last updated: Mar 10 '15