OpenCV Study Notes (iv)--OPENCV for Android porting to Android platform

Source: Internet
Author: User

Today, most of the AR is based on mobile devices, so here we try to invoke the OpenCV function in Android to do some related testing. Since OpenCV is based on C and some C + + languages, and Android Development uses Java, how can I invoke classes or code in the OpenCV library in Java? This requires the use of jni , but before using JNI, we first need to configure the development environment.


First, tools:

1.Eclipse: Includes JDK, SDK, IDE,CDT,ndk , and ADT plug-ins, that is, after the Android software development environment configuration is complete, plus the NDK and CDT plugins, Here I'll just briefly describe the NDK configuration process:

Download NDK installer, I downloaded it in cloud disk, because the official link is very unstable, I am using the NDK-R9 version :

unzip the package to the specified directory, then open the Eclipse Editor, window->preferences in the toolbar:

Note The NDK path cannot contain spaces!!! If you encounter more problems after configuring NDK, refer to: http://www.bkjia.com/Androidjc/934212.html

2.OpenCV for Android Plug-in package: Download the OpenCV Resource pack for Android dedicated version to OpenCV's official website (http://opencv.org/)

after download get compressed package:, compress the package and get the SDK file:

The files that these directories hold separately:

apk: using the OpenCV Library's Android Program Management Pack , this needs to follow your phone's actual chip situation to choose the appropriate version of the manual installation, unless you can connect to Google Play automatic installation;

Samples: Some simple case APK and its engineering source

SDK: the most critical directory that contains OpenCV API and library files for Android development .


Second, installation configuration:

1. Import the OpenCV library into eclipse: in Eclipse, in the Select toolbar: file->import ... , and then select general->existing Projects into Workspace :

Select the root directory of the OpenCV SDK that you just unzipped, and then import one of the projects:

During the import process, eclipse will compile the imported project, and after the successful import, we will begin to resolve these error instructions to compile successfully.


2. OpenCV Library-3.1.0 is a dependent library file for all other demos, so we first solve its problem and then import the demo to test it. From the error list we can see that most of the errors are "XXX cannot is revoled to a type", this error actually refers to some undefined classes, considering that we have not yet introduced other third-party class libraries, So the cause of this problem is only the reference to the Android internal class library, so the obvious can be targeted to the Android API version mismatch problem.

Select the project, right-click Properties, and open the Properties page:


Choose android tab, here to switch engineering dependent Android API version, the query can locate the missing API is new in Android 5.0, so we just choose 5.0 or more than 5.0 API version, can solve the problem:


3. After solving the above problems, we can use the same method to import a demo project, here we can also import all the demo project into eclipse:

It's red again, isn't it? Don't panic, look at the error list the errors are caused by referencing an undefined class, because each demo will reference the OpenCV Library-3.1.0 library file, but the path to the library file changes. That's why it's not possible to introduce a library file due to an error, the workaround is simple, just to relocate the library file path to the current library file path for each project:

To remove the invalid reference, click Add to re-add the Library project reference:


4. After all the demo projects are set up, there are two errors found in the error bar:

The reason for this error is that the NDK tool cannot be found at compile time, and we need to modify some of the configuration information of C + + Build in the Properties panel to cancel the use of the default command:

Expand Directory Select the Environment option, where add has one more path parameter ndkroot, and the parameter value is the root path of the NDK:

5. After doing this, select project right-click "Clean Project" to recompile the project and find that the error has become:

This is because the tool opencv.mk in the OpenCV SDK was not found at compile time, that is, the matching path of the OpenCV SDK is incorrect or there is no match. Open the Android.mk file under the Jni folder for the current project, which is the configuration file that is used by C/s + + compile, and we can see that the path to opencv.mk must be under the OPENCV_ANDROID_SDK directory, or in the same directory as the current project file:

so the solution to this problem is to add a opencv_android_sdk path configuration constant on the environment page of the C + + build:


Third, the case operation:

after all of the above configuration, the engineering errors are all resolved, then we need to see if the demo will work properly, the device connected to the PC, and then select the Project Right button "Run as" and then select "Android Application":

The reason for this prompt is that after OpenCV 2.4.3 , the APK must be dependent on OpenCV manager.apk to find the APK directory under the OpenCV Android SDK directory:

This applies to a lot of Android hardware devices, here I choose to install armeabi-v7a This version, after the smooth installation of OpenCV Manager, again restart just the demo, you can run and see the results are as follows:

OpenCV Study Notes (iv)--OPENCV for Android porting to Android platform

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.