Configure and use OpenCV in Android Studio

Source: Internet
Author: User

Configure and use OpenCV in Android Studio

Configure Android Studio and use OpenCV

 

Recently, a project is being transplanted, and the project is large. The Jni and OpenCV environments configured in Eclipse have no problems, but there are a lot of problems when migrating to Studio, I have also found some materials for reference and Study on the Internet. I am very grateful to my predecessors for their summary and experience. We have also studied how to use jni in AS and how to configure OpenCV for a period of time. If you forget to use jni in the future, we will record it here. On the other hand, we can provide some new users with shortcuts to avoid detours.

 

1. Introduction to downloads and directories

Go to the official website (http://opencv.org/) to download OpenCV4Android and unzip (here is the OpenCV-2.4.9-android-sdk ). The structure of the directory is as follows:

The sdk directory is the class library we need to develop opencv;

The samples directory contains several opencv application examples (including Face Detection), which can be used as a reference for development of opencv in android;

The doc directory contains the usage instructions and api documentation of the opencv class library;

The apk directory stores the OpenCV application installation packages corresponding to each kernel version, such;

(Used to manage the opencv class library on mobile devices. Before running the opencv application, make sure that OpenCV_2.4.3.2_Manager_2.4 _ * has been installed on the mobile phone _*. apk. Otherwise, the opencv application cannot run because it cannot load the opencv class library)

 

 

2. Introduce OpenCV into Android Studio

Note that you can directly Select File> Import Module in Android Studio, find the path for extracting OpenCV, and select the sdk/java folder as the Module for importing. However, it is easy to maintain. Here I suggest you find the java directory under the sdk, copy it to your StudioProject project directory, and introduce it.

 

3. Update build. gradle Information

After the import, an error occurs due to configuration issues in Gradle. in the upper left corner of Studio, select the Project view. In the OpenCVLibrary folder introduced, open build. gradle (note that openCVLibrary249 is introduced). Modify the following information in the file:

1) compileSdkVersion

2) buildToolsVersion

3) minSdkVersion

4) targetSdkVersion (the content is consistent with the information in build. gradle in the app folder)

As shown in:

 

Then click Gradle to synchronize (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 to add openCVLibrary and click OK.

 

5. Copy the libs folder to the project.

In the extract package of OpenCV, copy the sdk --> native --> libs folder, paste it in the app --> src --> main directory in the Project view, and rename it jniLibs.

At this point, the OpenCV environment has been configured. The following example is used for testing. Here I refer to a blog post, here also thanks to the blogger: http://blog.csdn.net/yanzi1225627/article/details/16917961

 

6. Demo

OK! First lookOpenCVActivityHow to write:

 

Public class OpenCVActivity extends Activity {private Button btn; private ImageView img; private Bitmap srcBitmap; private Bitmap grayBitmap; private static boolean flag = true; private static boolean isFirst = true; private static final String TAG = gao_chun; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_opencv); img = (ImageView) findViewById (R. id. img); btn = (Button) findViewById (R. id. btn); btn. setOnClickListener (new ProcessClickListener ();} @ Override protected void onResume () {super. onResume (); // load OpenCV engine and init OpenCV library OpenCVLoader. initAsync (OpenCVLoader. OPENCV_VERSION_2_4_9, getApplicationContext (), mLoaderCallback); Log. I (TAG, onResume sucess load OpenCV ...);} // callback function private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback (this) {@ Override public void onManagerConnected (int status) after the OpenCV library is loaded and initialized) {// 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 ;}}; public void procSrc2Gray () {Mat rgbMat = new Mat (); Mat grayMat = new Mat (); 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 grayMat Utils. matToBitmap (grayMat, grayBitmap); // convert mat to bitmap Log. I (TAG, procSrc2Gray sucess ...);} public class ProcessClickListener implements View. onClickListener {@ Override public void onClick (View v) {// TODO Auto-generated method stub if (isFirst) {procSrc2Gray (); isFirst = false;} if (flag) {img. setImageBitmap (grayBitmap); btn. setText (View Source image); flag = false;} else {img. setImageBitmap (srcBitmap); btn. setText (grayscale); flag = true ;}}}}

Let's take a look at the simple layout interface. Activity_opencv.xml:

 

 

     
      
   
    
   
  
 

 

 

After running:

 

According to the steps, there are basically not many problems. If you think about the Demo, please upload it for your reference. However, after copying it, we found that more than 220 MB of the entire package was too scary, simply delete some items, delete the content under the build directory under the app directory, and delete the content under src ---> main ---> jniLibs. so library deleted,Note:: If you want to download the demo and run it, you need to downloadSdk --> native --> libsCopy the four directories in the folder to the jniLibs directory of the project.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.