1. Download and catalogue Introduction
Go to the official website (http://opencv.org/) to download the opencv4android and unzip it (this is OPENCV-3.2.0-ANDROID-SDK). Here is the chart of the directory:
The SDK directory is the class library we need to develop OPENCV;
Some examples of OPENCV applications (including face detection) are stored in the Samples directory, which provides a reference for us to develop OpenCV under Android.
Doc directory for the OpenCV class library usage instructions and API documentation, etc.;
The APK directory contains the OPENCV application installation package that corresponds to each kernel version, such as;
(To manage the OpenCV class library in your mobile device, you must make sure that the opencv_3.2.0_manager_3.2.0_*.apk is already installed in your phone before you run the OpenCV app. Otherwise the OPENCV application will not run because the OpenCV class library cannot be loaded)
2. Introducing OpenCV to Android Studio
One thing to note here is that you can select the File-->import Module directly in Android Studio, find the path to OpenCV decompression, and select the Sdk/java folder to import as a Module. But easy to maintain, here's my advice is to find the SDK under the Java directory, copy it to your Studioproject project directory, in the introduction.
3. Update build.gradle information
After importing, the Project view is selected in the upper-left corner of studio, under the introduced Opencvlibrary folder, due to errors in configuration issues in Gradle. Open Build.gradle (note is introduced under openCVLibrary249), modify the following information in the file:
1) compilesdkversion
2) buildtoolsversion
3) Minsdkversion
4) targetsdkversion (match its content to the information in the Build.gradle in the app folder)
As shown in the following:
Then click Gradle to Sync (sync Gradle).
4. Add Module Dependency
Select File--->project Structure, in the Dependencies column of the app module, click the Green plus sign in the upper-right corner, add opencvlibrary, and click OK.
5. Copy the Libs folder into the project
In OpenCV's unpacking package, copy the Sdk-->native-->libs folder, paste it under the App-->src-->main directory under Project view, and rename it to Jnilibs.
In this situation, the OPENCV environment is well-equipped.
6. Sample Demo
Ok! First look at how the mainactivity is written:
PackageCom.tmf.testopencv;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.ImageView;ImportAndroid.widget.TextView;ImportOrg.opencv.android.BaseLoaderCallback;ImportOrg.opencv.android.OpenCVLoader;Importorg.opencv.android.Utils;ImportOrg.opencv.core.Mat;ImportOrg.opencv.imgproc.Imgproc; Public classMainactivityextendsappcompatactivity{PrivateButton btn; PrivateImageView img; PrivateBitmap Srcbitmap; PrivateBitmap Graybitmap; Private Static BooleanFlag =true; Private Static BooleanIsFirst =true; Private Static FinalString TAG = "Gao_chun"; @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 ()); } @Overrideprotected voidOnresume () {Super. Onresume (); //load OpenCV engine and init OpenCV libraryOpencvloader.initasync (Opencvloader.opencv_version_3_2_0, Getapplicationcontext (), mLoaderCallback); LOG.I (TAG,"Onresume sucess load OpenCV ..."); } //OpenCV Library loading and initializing a successful callback function PrivateBaseloadercallback Mloadercallback =NewBaseloadercallback ( This) {@Override Public voidOnmanagerconnected (intstatus) { //TODO auto-generated Method Stub Switch(status) { Casebaseloadercallback.success:log.i (TAG,"Load Successfully"); Break; default: Super. onmanagerconnected (status); LOG.I (TAG,"Load Failed"); Break; } } }; Public voidProcsrc2gray () {Mat Rgbmat=NewMat (); Mat Graymat=NewMat (); Srcbitmap=Bitmapfactory.decoderesource (Getresources (), r.drawable.test); 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 bitmapLOG.I (TAG, "Procsrc2gray sucess ..."); } Public classProcessclicklistenerImplementsview.onclicklistener{@Override Public voidOnClick (View v) {//TODO auto-generated Method Stub if(IsFirst) {Procsrc2gray (); IsFirst=false; } if(flag) {Img.setimagebitmap (GRAYBITMAP); Btn.settext ("View Original"); Flag=false; }Else{img.setimagebitmap (SRCBITMAP); Btn.settext ("Grayscale"); Flag=true; } } }}
Take a look at the simple layout interface activity_main.xml:
<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"/> <ButtonAndroid:id= "@+id/btn"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_centerhorizontal= "true"Android:text= "Grayscale"/>" <ImageViewAndroid:id= "@+id/img"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_centerinparent= "true"/></Relativelayout>
After the run:
Follow the steps, basically not much problem, think of the demo or upload for everyone to refer to it, but because of the copy after the whole package has found that more than 220 m too scary, simply delete something, the app directory under the build directory to delete content, the src---> main---> Jnilibs. So library deleted, note : If you download the demo run, you need to OPENCV in the unpacking directory, will sdk-->native-->libs The four directories under the folder are copied to the project's Jnilibs directory.
Android Studio Configuration and use OpenCV