Android Studio-use the OpenCV configuration method and demo, and solve problems encountered during development.
Prerequisites:
1. Install Android Studio (Procedure omitted)
2. Download OpenCV for Android on the official website: http: opencv.org/downloads.html the version I downloaded
3. decompress the downloaded OpenCV for Android file to a fixed folder.
4. Create an android Project (just create one and use it later)
The above steps are basically no problem (except when the web page is opened, the network speed is very slow ...)
Android Studio imports OpenCV:
1. Click File-new-import Module:
2. content in Source directory: Find the location where OpenCV for Android is extracted in step 1. OpenCV-android-sdk-> java to directly copy the file, after the copy is completed, the Module name will appear (because I have already imported the file, so there will be an exclamation point. Normally, click Next to the next step)
Errors that may be encountered in the above steps: After the import, various bugs are output, and my example (only part of the screenshot ):
Solution: After importing OpenCV for Android, change the build. gradle file under openCVLibrary310 to the same as the build. gradle file under the android project created in step 1. Then clean and OK.
Apply openCVLibrary310 to your project:
1. Click File-Project Structure. The following figure will appear: Find your new android Project under Modules and click Dependencies.
2. Click the plus sign on the right to select the third Module dependency, select openCVLibrary310, and click Finish.
After completing the preceding steps, your app can be used to develop openCV.
Android uses OpenCV to convert a color image to a gray image:
MainActivity. java code:
Package com. example. lenovo. myapplication; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. support. v7.app. appCompatActivity; import android. OS. bundle; import android. util. log; import android. view. view; import android. widget. button; import android. widget. imageView; import com. example. lenovo. r; import org. opencv. android. baseLoaderCallback; import org. opencv. android. loaderCal LbackInterface; import org. opencv. android. openCVLoader; import org. opencv. android. utils; import org. opencv. core. mat; import org. opencv. imgproc. imgproc; public class MainActivity extends AppCompatActivity {Button btnProcess; Bitmap srcBitmap; Bitmap grayBitmap; ImageView imgHuaishi; private static boolean flag = true; // private static boolean isFirst = true; private static final String TAG = "MainActiv Ity "; // callback function after the OpenCV library is loaded and initialized successfully: private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback (this) {@ Override public void onManagerConnected (int status) {// TODO Auto-generated method stub switch (status) {case BaseLoaderCallback. SUCCESS: Log. I (TAG, "loaded successfully"); break; default: super. onManagerConnected (status); Log. I (TAG, "loading failed"); break ;}};@ Override protected void onCreate (Bundle savedInst AnceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); initUI (); btnProcess. setOnClickListener (new ProcessClickListener ();} public void initUI () {btnProcess = (Button) findViewById (R. id. btn_gray_process); imgHuaishi = (ImageView) findViewById (R. id. img_huaishi); Log. I (TAG, "initUI sucess... ");} public void procSrc2Gray () {Mat rgbMat = new Mat (); Mat grayMat = new Mat (); srcBitmap = BitmapFactory. decodeResource (getResources (), R. drawable. text_img); 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 grayMat Utils. matToBitmap (grayMat, grayBitmap); // convert m At to bitmap Log. I (TAG, "procSrc2Gray sucess... ");} private class ProcessClickListener implements View. onClickListener {@ Override public void onClick (View v) {// TODO Auto-generated method stub // if (isFirst) // {procSrc2Gray (); // isFirst = false; //} if (flag) {imgHuaishi. setImageBitmap (grayBitmap); btnProcess. setText ("View Source image"); flag = false;} else {imgHuaishi. setImageBitmap (srcBitmap); btnProces S. setText ("grayscale"); flag = true ;}}@ Override public void onResume () {super. onResume (); if (! OpenCVLoader. initDebug () {Log. d (TAG, "Internal OpenCV library not found. using OpenCV Manager for initialization "); OpenCVLoader. initAsync (OpenCVLoader. OPENCV_VERSION_3_0_0, this, mLoaderCallback);} else {Log. d (TAG, "OpenCV library found inside package. using it! "); MLoaderCallback. onManagerConnected (LoaderCallbackInterface. SUCCESS );}}}
Corresponding xml file:
<? Xml version = "1.0" encoding = "UTF-8"?> <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 = "16dp" android: paddingRight = "16dp" android: paddingTop = "16dp" android: paddingBottom = "16dp" tools: context = "com. example. mytest. mainActivity "> <ImageView android: id =" @ + id/img_policishi "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: layout_centerInParent = "true"/> <Button android: id = "@ + id/btn_gray_process" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_below = "@ id/img_javasishi" android: layout_centerHorizontal = "true" android: text = "grayscale"/> "</RelativeLayout>
This is enough, and then click Run .....
Hehe, the running fails, right? Remind me to install the apk of opencv manager. I have installed all the apk in the official demo, and I am still prompted not to install it. The program cannot run ,,, later I thought about it. In actual development, we need a facial recognition function. Do we have to install the apk of opencv manager while allowing customers to use your software? Then there is no user experience ...... Then we found a way to use OpenCV normally without installing this apk.
Reference: http://jingyan.baidu.com/article/60ccbceb53533364cab197db.html
The method is as follows:
1. copy the libs folder under F: \ OpenCV-Android-sdk \ native in the OpenCV sdk for android file to your Android project, under your project \ src \ main and rename libs to jniLibs
2. Copy the xml file OpenCV-android-sdk \ samples \ image-manipulations \ res \ layout to your project \ src \ main \ res
3. copy the java files in OpenCV-android-sdk \ samples \ image-manipulations \ src \ org \ opencv \ samples \ imagemanipulations to your project \ src \ main \ java \ MainActivity package name, that is, the directory at the same level as MainActivity
4. Configure the imported java file in the project list file and add the corresponding permissions,
The next step is to run your project.
After I run:
Finally, let's talk about other amazing issues:
1. It is clear that everything is done properly. There is no problem with the resource file, that is, the R file clean cannot be found ,,,,
Unscrupulous solution: manually write import your registration. R, I tried, OK .......
2. An error occurred while running the app.
Enable ADB Integration check box
I hope to help you.