I mentioned in the http://www.cnblogs.com/fx-blog/p/8206737.html article How to import OpenCV (including the Opencv_contrib part) in the Java layer in Android studio, But this is only the Java layer of import, with the deepening of learning, we can gradually find that OPENCV library support for Java is not very strong, such as when I use the SIFT algorithm, generally extracted features more than 10,000 points, which contains a large number of invalid feature points, if I want to specify the number of feature points, For example, 500 (after testing, Java uses OPENCV to extract feature points by default to 500), but OpenCV Java library does not provide such a method (or I did not find that if the great God knows, also hope to inform). However, in C + +, it is possible to assign a feature point to sift, where it is necessary to have a function called C + + in Java, we know in Java we can use JNI or JNA solution, then in Androd we can use the NDK to do Java to C + + calls.
Prior to Android Studio 2.2, we usually set up local development through the ANDROID.MK and application.mk two files, but in the version after Android Studio 2.2, added the method of compiling the NDK project with the CMake configuration , which is certainly a good news, we can finally abandon the previous tedious method, this article just talk about using CMake in Android studio for OpenCV's NDK development.
Preparatory work:
First, we need to configure CMake, NDK tools in Android Studio, open Android Studio 2.2, click the button to open SDK Manager, select the Android version you need in the SDK platforms, Here I am using Android 7.0.
Select the part of the red box that is marked in SDK tools (recommended in the SDK manager Ndk,ndk installed after the path is <android SDK path>\ndk-bundle):
Official start:
Create a new project, in the process of creation, we need to tick the include C + + support, the following steps by default, can be corresponding to the http://www.cnblogs.com/fx-blog/p/8206737.html, writing is not very good, We forgive you.
After the project is successfully created, a folder called CPP is automatically set up under App\src\main, which contains a native-lib.cpp file. At the same time, there is a CMakeLists.txt file in the app directory, and Android Studio calls CMake to use the file to coordinate the compilation of C + + code (by default, using Clang compilation) and to provide the resulting. So file to the APK file's packaging process.
Then you need to import the OPENCV,OPENCV 3.2 Android SDK in the Java layer to use the previously compiled library: Https://pan.baidu.com/s/1kVOejLt; in Android Studio, click File- New ... Import module, then select the <OPENCV 3.2 Android Sdk>\sdk\java directory in source directory, and the Module name will automatically become " OpenCVLibrary320 ", followed by the default settings.
After the OPENCV package is imported, Android Studio will attempt to compile automatically because its default Build.gradle file setting is not suitable for the latest version, so it will get an error. Modifying the Opencvlibrary320\build.gradle as follows will correct these errors (the red-framed parts need to be consistent with app\build.gradle).
Click File--Project Structure, click "App" in the modules on the left, then click the plus sign on the right, select Module Dependency, and then select: openCVLibrary320 in the pop-up box. This adds OPENCV support on the Java layer for our Project app (where the library is another repository I use, regardless).
The next step is to configure the OPENCV application with CMake configuration:
First, you need to copy all of the header and library files required to apply OpenCV C + + into your project and copy the <OPENCV 3.2 Android Sdk>\sdk\native\jni\include folder to app\src\main\ CPP, copy the <OPENCV 3.2 Android Sdk>\sdk\native\libs folder into the App\src\main and rename the folder to Jnilibs.
Then change the App\build.gradle to (you crossing can compare and modify):
Apply plugin:'com.android.application'Android {compilesdkversion -buildtoolsversion"27.0.1"Defaultconfig {ApplicationID"com.example.demo02"minsdkversion thetargetsdkversion -Versioncode1Versionname"1.0"Testinstrumentationrunner"Android.support.test.runner.AndroidJUnitRunner"Externalnativebuild {cmake {cppflags"-std=c++11","-frtti","-fexceptions"abifilters'x86','x86_64','Armeabi','armeabi-v7a','arm64-v8a','MIPS','Mips64'}}} sourcesets {main {jnilibs.srcdirs= ['Src/main/jnilibs']}} buildtypes {release {minifyenabledfalseproguardfiles Getdefaultproguardfile ('Proguard-android.txt'),'Proguard-rules.pro'}} externalnativebuild {cmake {path"CMakeLists.txt"}}}dependencies {compile'com.android.support:design:25.3.1'compile Filetree (include: ['*.jar'], dir:'Libs') Androidtestcompile ('com.android.support.test.espresso:espresso-core:2.2.2', {Exclude group:'Com.android.support', module:'support-annotations'}) Compile'com.android.support:appcompat-v7:25.3.1'Testcompile'junit:junit:4.12'Compile project (': openCVLibrary320')}
App\cmakelists.txt modified to:
Cmake_minimum_required (VERSION 3.4.1) Set (Cmake_verbose_makefile on) set (Ocvlibs"${cmake_source_dir}/src/main/jnilibs") include_directories (${cmake_source_dir}/src/main/cpp/include) add_library (LIBOPENCV_JAVA3 SHARED imported) set_target_properties (LIBOPENCV_JAVA3 properties Imported_location"${ocvlibs}/${android_abi}/libopencv_java3.so") Add_library (#sets the name of the library.native-Lib#sets the library as a shared library.SHARED#provides a relative path to your source file (s).src/main/cpp/native-lib.cpp) find_library (#sets the name of the path variable.log-Lib#Specifies the name of the NDK library that #You want CMake to locate.log) target_link_libraries (#Specifies the target library.native-lib Android log libopencv_java3#Links the target library to the log library #included in the NDK.${log-lib})
The configuration section is done and the code for the key section is pasted, and the Ndk helper class Opencvndkhelper:
1 Packagecom.example.ndk;2 3 Public classOpencvndkhelper {4 Static {5System.loadlibrary ("Native-lib");6 }7 Public native Static voidDetectfeatures (LongSRCMATADDR,Longdstmataddr);8}
C + + file Native-lib.cpp:
1#include <jni.h>2#include <string>3#include <opencv2/core/core.hpp>4#include <opencv2/features2d/features2d.hpp>5#include <opencv2/xfeatures2d/nonfree.hpp>6 7 using namespace std;8 using namespace CV;9 using namespace xfeatures2d;Ten Oneextern "C" A { -JniexportvoidJnicall java_com_example_ndk_opencvndkhelper_detectfeatures -(JNIENV *, Jclass, Jlong srcmataddr, Jlong dstmataddr) { themat* Srcmat = (mat*) srcmataddr; -mat* descriptors = (mat*) dstmataddr; -Vector<keypoint>keypoints; -ptr<sift> detector = sift::create (1000); +Detector->detect (*Srcmat, keypoints); -Detector->compute (*srcmat, Keypoints, *descriptors); + } A}
The main function of this code is to extract the SIFT features (here I do not describe the process of generating. h files, as long as you master the method of naming the functions in the. cpp file, this process can be omitted).
To add: This code actually explains how to pass the mat to a method in C + + in Java, which is called in Java (SRC and Srcmat are mat objects):
Opencvndkhelper.detectfeatures (Src.getnativeobjaddr (), srcmat.getnativeobjaddr ());
PS: The article used in the jnilibs is directly copied to the directory, the storage space for this project is relatively large, about 1G, each new project needs to be re-copied, we can also use soft links or absolute path instead, here is not more introduction. But the resulting apk file size is almost the same.
over~~~
Run the following question:
The process of blogging, Dad suddenly find me video chat, we chatted a lot, mainly is to urge me to find a girlfriend, is it 95 after the parents urge to find a girlfriend, feel from my June graduation (perhaps earlier), this topic has not stopped Ah, the brain to fill the scene of the New Year home ...
NDK Development using Camke for OPENCV in Android Studio 2.2