Load and show image using Opencv on Android Studio
I try to create simple android app that shows an image. I can show the image without opencv.
- I copy the image under src/drawable folder.
- 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>
- 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)
you can't use any opencv code in onCreate(), because the native c++ libs are not loaded yet.
I download opencv-android-2.4.9-sdk. Should I use native c++ libs? To use opencv on android, what is the best way?
onCreate() is just the wrong place. see answer below.