Example of configuring and using OpenCV in Android Studio

Source: Internet
Author: User

Android Studio Configuration and use OPENCV foreword: Recently in the project porting, the project is large, the JNI and OPENCV environment configured in Eclipse no problem, but migrated to studio in the problem of a lot, the internet also found some information reference and learning, Thank you for the summary and experience left by our predecessors. Regarding the use of JNI and configuration in as OpenCV also studied a

Android Studio Configuration and use OpenCV

Introduction: Recently in the project transplant, the project is large, the JNI and OPENCV environment configured in Eclipse no problem, but migrated to studio in a lot of problems, the Internet also found some information reference and learning, thank the predecessors left the summary and experience. about using JNI and configuration in as OpenCV also studied for a period of time, afraid to forget in the future in this record, on the other hand can also give some beginners a shortcut, less go some detours.

1. Download and catalogue Introduction

Go to the official website () download opencv4android and unzip (here is OPENCV-2.4.9-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_2.4.3.2_manager_2.4_*.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 case, the OPENCV environment is configured, which is tested with an example below. Here is a blog post, thanks to the blogger:

6. Sample Demo

Ok! First look at how the opencvactivity is written:

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.initas Ync (Opencvloader.opencv_version_2_4_9, Getapplicationcontext (), mloadercallback); LOG.I (TAG, onresume sucess load OpenCV ...); The//OPENCV library loads and initializes the successful callback function private Baseloadercallback mloadercallback = new Baseloadercallback (this) {@Override public vo ID onmanagerconnected (int status) {//TODO auto-generated Method stub switch (status) {case BaseLOADERCALLBACK.SUCCESS:LOG.I (TAG, successfully loaded); Break default:super.onManagerConnected (status); LOG.I (TAG, failed to load); 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.C Olor_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.sette XT (view original); Flag = false; }else{Img.setimagebitmap (Srcbitmap); Btn.settext (grayscale); flag = True;}} }} 
Take a look at the simple layout interface Activity_opencv.xml:

Example of configuring and using OpenCV in Android Studio

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.