I've studied OpenCV before, but all of them run programs on vs. Now go to the Android development camp and try to get the old bank A few days ago tried the next opencv2.4.x configuration, need to install manager.apk, found that the different platform is the same CPU architecture needs to install the APK is different, try to install the corresponding apk in the document, a variety of error, should be in recent years mobile phone development too fast, opencv2.x.x to the APK has Can't catch up with the progress of hardware. Now development on the android7.1.1 platform, want to find a 5.0 version is not. So the idea of using OPENCV to develop on Android, can not always be an extra adb apk. So just think, could you please not manager. APK. Looked up some information, after Opencv3 can, today got down, indeed can. The following describes the construction and use of the environment.
I use AndroidStudio2.2.3, of course, your computer itself has been set up androidstudio and can normally create projects, here just tell you how to join OpenCV. Search the Internet to opencv-3.1.0-android-sdk, download down (after this version should also be, interested can try). Extract it. Here's a step-by-step description:
1) in Androidstudio file->new->importmodule, import the project is actually import OpenCV library.
2) The imported items are extracted from the Opencv-3.1.0-android-sdk folder,. /sdk/java (Java folder), click OK to import.
3) because the profile of the imported project does not match, the Androidstudio is tuned to Project view, Enter the new openCVLibrary310 in the Build.gradle, change the red circle to your own development environment configuration, you can refer to the other build.gradle of your project.
4) Next, configure the project you want to add the OpenCV library to (my app project), and go to project struture in file, as shown in the steps.
5) Add openCVLibrary310.
6) Copy Unzip Opencv-3.1.0-android-sdk folder, ... /sdk/native/libs the Libs folder.
7) Paste into the project you need to opencv the library. /app/src/main, rename the Libs to Jnilibs.
8) through the above steps, it is done.
Now we are testing the code in the app project. (added in mainactivity:static{system. LoadLibrary ("opencv_java3");} )
Public classMainactivityextendsActivity {
Static{system.loadlibrary ("OPENCV_JAVA3"); } PrivateButton btn; PrivateImageView img; PrivateBitmap Srcbitmap; PrivateBitmap Graybitmap; Private Static BooleanFlag =true; Private Static BooleanIsFirst =true; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); IMG=(ImageView) Findviewbyid (r.id.img); BTN=(Button) Findviewbyid (R.ID.BTN); Btn.setonclicklistener (NewProcessclicklistener ()); } Public voidProcsrc2gray () {Mat Rgbmat=NewMat (); Mat Graymat=NewMat (); Srcbitmap=Bitmapfactory.decoderesource (Getresources (), R.drawable.genie); Graybitmap=Bitmap.createbitmap (Srcbitmap.getwidth (), Srcbitmap.getheight (), Bitmap.Config.RGB_565); Utils.bitmaptomat (Srcbitmap, Rgbmat);//Convert original bitmap to Mat, R G B.Imgproc.cvtcolor (Rgbmat, Graymat, Imgproc.color_rgb2gray);//Rgbmat to Gray GraymatUtils.mattobitmap (Graymat, Graybitmap);//convert mat to bitmap } Public classProcessclicklistenerImplementsview.onclicklistener{@Override Public voidOnClick (View v) {//TODO auto-generated Method Stub if(isFirst) {Runnable Runnable=NewRunnable () {@Override Public voidrun () {Procsrc2gray (); } }; NewThread (runnable). Start (); IsFirst=false; } if(flag) {Img.setimagebitmap (GRAYBITMAP); Btn.settext ("View Original"); Flag=false; }Else{img.setimagebitmap (SRCBITMAP); Btn.settext ("Grayscale"); Flag=true; } } }}
<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"> <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparenttop= "true"Android:text= "OpenCV"/> <ImageViewAndroid:id= "@+id/img"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_centerinparent= "true"Android:background= "@drawable/genie"/> <ButtonAndroid:id= "@+id/btn"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_below= "@id/img"Android:layout_centerhorizontal= "true"Android:text= "Grayscale"/>"</Relativelayout>
Test results:
Android Studio2.2.3 configuration OpenCV3.1.0 and does not install OpenCV Manager run