Tutorial on OpenCV for Android with Android Studio
Welcome guys! my goal is to make it easy to create an android CV application with OpenCV libraries.
before starting tutorial its better to know the version of tools that i used!
Tool | Version |
---|---|
OpenCV Android SDK | openCV-3.2.0 |
Android Studio | v2.2.2 |
Android SDK build-tools | v25 |
Android SDK platform-tools | v25.0.3 |
Android Official NDK | android-ndk-r12b |
First of all you need to download OpenCv Android SDK and Android Official NDK!
Right click on This PC and go to Properties find Advanced dystem Settings on right side and open it!
In new Window go to Advanced tab and click on Environment Variables.
Find PATH variable for current user and click on Edit
Click New button in opened window and paste the direction of your java bin folder! like : *C:\Program Files\Java\jdk1.8.0_40\bin*
Add the path of NDK like step 4!for NDK you need to add root direction! like : C:\work\android-ndk-r12b
Done!
if you got an exceptin with this message : Error:NDK not configured. Download it with SDK manager.)
you should follow this steps:
{YOUR_OPENCV_SDK_DIR}\sdk\
and select java folder!click on OK!compile project(':openCVLibrary320')
openCVLibrary320
is the name of added module from OpenCV and it may have different name for you!src/main/jni/
to src/main/jniLibs/
and click on Finish
You need to add native libraries for different type of proccessors!fortunetly OpenCV provides all of them for you and you can find them under OpenCV SDK directory \sdk\native\libs
!
In order to add a specific proccessor's library you just need to copy that folder and paste it under jniLibs folder!
Android.mk
file and configure it!Android.mk
!LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
OPENCV_LIB_TYPE:=SHARED
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
include {OpenCV.mk_DIR}
LOCAL_SRC_FILES := native-lib.cpp
LOCAL_C_INCLUDES += {INCLUDE_DIR}
LOCAL_LDLIBS += -llog -ldl
LOCAL_CFLAGS += -DOPENCV_OLDER_VISION
LOCAL_CPP_FEATURES += exceptions (Recommended)
LOCAL_CPPFLAGS += -fexceptions
LOCAL_MODULE := native-lib
include $(BUILD_SHARED_LIBRARY)
{OpenCV.mk_DIR}
: directory of OpenCV.mk
file under under OpenCV SDK directory \sdk\native\jni\OpenCV.mk
{INCLUDE_DIR}
: directory of OpenCV include folder under OpenCV SDK directory \sdk\native\jni\include
LOCAL_MODULE
: name of native cpp file that you need for writing native cpp code! skip for now.Optional config:
OPENCV_CAMERA_MODULES
and OPENCV_INSTALL_MODULES
: if you turn this option off your application need external OpenCV Package Manager installed on user device to work!