Opencv4android Explanation: Dialysis Android with JNI OpenCV three ways (let Opencvmanager never bothered)

Source: Internet
Author: User

The previous article has specifically discussed the use of OPENCV, originally thought the world has been peaceful. But the constant response is still not good opencv4android, can not be handy on Android use OpenCV. A lot of energy is wasted on the groping configuration. Especially after the birth of Opencvmanager. More to the people. Everyone's first reaction is how to not install Opencvmanager. It's too bad for the customer to install this stuff more.

Last night we studied until two and got up seven o'clock this morning. Finally, the clue is clear.

The following is a OpenCV2.3.1 based on the previous one. Android uses the JNI call OPENCV to implement a human face detection instance to answer one after the other, how to use the Java interface on Android without installing Opencvmanager, and three ways to use OPENCV in JNI mode.

First of all, we look at the three ways of Jni tune OpenCV. A lot of people would be surprised by the swelling. JNI tune OpenCV There are 3 more ways. A lot of online tutorials have long said that there are only Java and JNI two ways to use OpenCV on Android, and there are 3 ways to use JNI.

After I study. There are 3 different ways to tune JNI, even if the official website guidance documents are ambiguous, so that very many people are overwhelmed.

Each of these three ways is:

1, the use of static OpenCV library way.

2, the use of dynamic OpenCV library way;

3, the same time using the Java API and the way to use the JNI interface, at this time, the compiler is generally used in the dynamic Link OpenCV library way.

To illustrate, there is no need to install Opencvmanagerin these three ways, the difference is that the Mk file is different. One of the most admired is the first way, and the first is a way to match OpenCV2.3.1 in JNI OpenCV use. This article is based on the latest Windows platform OPENCV-2.4.9-ANDROID-SDK, using 2.4.9 opencv4android need to use the NDK version number is R9, I use the android-ndk-r9d version number. The reason churn to 2 last night was due to the version number of the NDK R7 that was used earlier. How to make up all just, because few things reported thousands of mistakes. ANDROID-NDK-R9D installation is very simple, only need to decompress the configuration of an environment variable can be.

first, Android with JNI OpenCV configuration method:

The contents of the Application.mk file are as follows:

App_stl:=gnustl_static
App_cppflags:=-frtti-fexceptions
app_abi:= armeabi-v7a

These three ways of Application.mk are the same, so I will not say. In the APPLICATION.MK can also be configured app_platform=17 similar, of course, not configured fully capable.

ANDROID.MK content such as the following:

local_path: = $ (Call My-dir )
include $ (clear_vars)


Opencv_install_modules:=on
Opencv_camera_modules:=off


Opencv_lib_type:=static


ifeq ("$ (wildcard $ (Opencv_mk_path))", "")
include D:\ProgramFile\ Opencv-2.4.9-android-sdk\sdk\native\jni\opencv.mk
Else  
include $ (opencv_mk_path)  
EndIf &NBSP


local_module    := processimg 
local_src_files: = detectface_jni.cpp \
src/copytoassets.cpp \
src/ DetectFace.cpp
Local_ldlibs    +=-lm-llog 

Include $ (build_shared_library)  

One by one, opencv_install_modules:=on means to copy the dependent OpenCV so library to the Libs directory, but unfortunately, this command is only valid for Opencv_camera_modules.

Only when there is opencv_camera_modules:=on. Can see that he will be on his own initiative to copy the inside of the camera so to project under the Libs directory. Include D:\ProgramFile\OpenCV-2.4.9-android-sdk\sdk\native\jni\ Opencv.mk This sentence is more critical, this is where I install OPENCV-2.4.9-ANDROID-SDK. I installed it on the D drive. And my working space is OK on the E-drive.

Instead of restricting the location of this decompression packet, as OpenCV2.3.1.

Local_module is the name of the library to be generated, and Local_src_files is the CPP file under the JNI directory. SRC indicates that my jni under the subdirectory name is "src", this block to replace their own source code file is OK.

For the rigor of the test. In project will be libs directory of things. and the contents of the obj directory are all deleted. Go to project with Cygwin, enter Ndk-build, and see for example the following information:

[email protected]/cygdrive/e/workspaces/opencv4android/facedetectliu2$ ndk-buildandroid NDK:WARNING:APP_ PLATFORM android-19 is larger than android:minsdkversion [/androidmanifest.xml[armeabi-v7a] Cygwin:generat ing dependency file converter script[armeabi-v7a] compile++ thumb:processimg <= detectface_jni.cppjni/detectface_ JNI.cpp:In function ' _jstring* java_org_yan_processlib_libprocessimg_processiplimg (jnienv*, Jobject, Jintarray, int, int) ': Jni/detectface_jni.cpp:99:44:warning:converting ' false ' to pointer type for argument 2 of ' jint* _jnienv::getintar Rayelements (Jintarray, jboolean*) ' [-wconversion-null]jni/detectface_jni.cpp:in function ' _jstring* Java_org_yan_ Processlib_libprocessimg_processstaticimg (jnienv*, Jobject, jintarray, int, int) ': jni/detectface_jni.cpp:133:44: Warning:converting ' false ' to pointer type for argument 2 of ' jint* _jnienv::getintarrayelements (Jintarray, jboolean*) ' [ -WCONVERSION-NULL][ARMEABI-V7A] compile++ thumb:processimg<= copytoassets.cpp[armeabi-v7a] compile++ thumb:processimg <= detectface.cpp[armeabi-v7a] SharedLibrary: LIBPROCESSIMG.SO[ARMEABI-V7A] Install:libProcessImg.so = libs/armeabi-v7a/libprocessimg.so
the above two warning is related, compile successfully. The size of the generated libprocessimg.so is 4M, and the entire apk size is 1.99M.

Note that it is assumed that the local_ldlibs + =-lm-llog in MK is incorrectly written as: local_ldlibs: =-lm-llog. The "+ =" error is written as ": =" will see a number of errors such as the following:

$ ndk-buildandroid NDK:WARNING:APP_PLATFORM android-19 is larger than android:minsdkversion./androidmanifest.xml[ ARMEABI-V7A] compile++ thumb:processimg <= detectface_jni.cppjni/detectface_jni.cpp:in function ' _jstring* Java_ Org_yan_processlib_libprocessimg_processiplimg (jnienv*, Jobject, jintarray, int, int) ': jni/detectface_jni.cpp : 99:44:warning:converting ' false ' to pointer type for argument 2 of ' jint* _jnienv::getintarrayelements (Jintarray, Jbool ean*) ' [-wconversion-null]jni/detectface_jni.cpp:in function ' _jstring* java_org_yan_processlib_libprocessimg_ Processstaticimg (jnienv*, Jobject, jintarray, int, int) ': Jni/detectface_jni.cpp:133:44:warning:converting ' false ' to Pointer type for argument 2 of ' jint* _jnienv::getintarrayelements (Jintarray, jboolean*) ' [-wconversion-null][ ARMEABI-V7A] compile++ thumb:processimg <= copytoassets.cpp[armeabi-v7a] compile++ thumb:processimg <= DETECTFACE.CPP[ARMEABI-V7A] Sharedlibrary:libprocessimg.sod:/programfile/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/. /lib/gcc/arm-linux-androideabi/4.6/. /.. /.. /.. /arm-linux-androideabi/bin/ld.exe:d:/programfile\opencv-2.4.9-android-sdk\sdk\native\jni\/. /libs/armeabi-v7a/libopencv_core.a (PERSISTENCE.CPP.O): in function Icvgets (cvfilestorage*, char*, int): Persistence.cpp (. text._zl7icvgetsp13cvfilestoragepci+0x7e): error:undefined reference to ' Gzgets ' d:/programfile/ android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/. /lib/gcc/arm-linux-androideabi/4.6/. /.. /.. /.. /arm-linux-androideabi/bin/ld.exe:d:/programfile\opencv-2.4.9-android-sdk\sdk\native\jni\/. /libs/armeabi-v7a/libopencv_core.a (PERSISTENCE.CPP.O): in function icvxmlskipspaces (cvfilestorage*, char*, int): Persistence.cpp (. text._zl16icvxmlskipspacesp13cvfilestoragepci+0x1c4): error:undefined reference to ' gzeof ' d:/ programfile/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/. /lib/gcc/arm-linux-androideabi/4.6/. /.. /.. /../arm-linux-androideabi/bin/ld.exe:d:/programfile\opencv-2.4.9-android-sdk\sdk\native\jni\/. /libs/armeabi-v7a/libopencv_core.a (PERSISTENCE.CPP.O): in function icvymlskipspaces (cvfilestorage*, char*, int, int) [Clone. constprop.65]:p ersistence.cpp (. text._zl16icvymlskipspacesp13cvfilestoragepcii.constprop.65+0x122): Error : Undefined reference to ' gzeof ' d:/programfile/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/ windows/bin/. /lib/gcc/arm-linux-androideabi/4.6/. /.. /.. /.. /arm-linux-androideabi/bin/ld.exe:d:/programfile\opencv-2.4.9-android-sdk\sdk\native\jni\/. /libs/armeabi-v7a/libopencv_core.a (PERSISTENCE.CPP.O): in function icvputs (cvfilestorage*, char const*): Persistence.cpp (. text._zl7icvputsp13cvfilestoragepkc+0x32): error:undefined reference to ' gzputs ' d:/programfile/ android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/. /lib/gcc/arm-linux-androideabi/4.6/. /.. /.. /.. /arm-linux-androideabi/bin/ld.exe:d:/programfile\opencv-2.4.9-android-sdk\sdk\native\jni\/. /libs/armeabi-v7a/libopencv_core.a (PERSISTENCE.CPP.O): in function Icvclose (cvfilestorage*, std::string*): Persistence.cpp (. text._zl8icvclosep13cvfilestoragepss+0x132): error:undefined reference to ' Gzclose ' d:/programfile /android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/. /lib/gcc/arm-linux-androideabi/4.6/. /.. /.. /.. /arm-linux-androideabi/bin/ld.exe:d:/programfile\opencv-2.4.9-android-sdk\sdk\native\jni\/. /libs/armeabi-v7a/libopencv_core.a (PERSISTENCE.CPP.O): in function CvOpenFileStorage:persistence.cpp (. TEXT.CVOPENFILESTORAGE+0X1AC): error:undefined reference to ' Gzrewind ' d:/programfile/android-ndk-r9d/toolchains/ arm-linux-androideabi-4.6/prebuilt/windows/bin/. /lib/gcc/arm-linux-androideabi/4.6/. /.. /.. /.. /arm-linux-androideabi/bin/ld.exe:d:/programfile\opencv-2.4.9-android-sdk\sdk\native\jni\/. /libs/armeabi-v7a/libopencv_core.a (PERSISTENCE.CPP.O): in function CvOpenFileStorage:persistence.cpp (. TEXT.CVOPENFILESTORAGE+0X6D4): Error:undeFined reference to ' Gzclose ' d:/programfile/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/ bin/. /lib/gcc/arm-linux-androideabi/4.6/. /.. /.. /.. /arm-linux-androideabi/bin/ld.exe:d:/programfile\opencv-2.4.9-android-sdk\sdk\native\jni\/. /libs/armeabi-v7a/libopencv_core.a (PERSISTENCE.CPP.O): in function CvOpenFileStorage:persistence.cpp (. text.cvopenfilestorage+0x75a): error:undefined reference to ' Gzopen ' d:/programfile/android-ndk-r9d/toolchains/ arm-linux-androideabi-4.6/prebuilt/windows/bin/. /lib/gcc/arm-linux-androideabi/4.6/. /.. /.. /.. /arm-linux-androideabi/bin/ld.exe:d:/programfile\opencv-2.4.9-android-sdk\sdk\native\jni\/. /libs/armeabi-v7a/libopencv_core.a (PERSISTENCE.CPP.O): in function CvOpenFileStorage:persistence.cpp (. TEXT.CVOPENFILESTORAGE+0XD80): error:undefined reference to ' Gzclose ' d:/programfile/android-ndk-r9d/toolchains/ arm-linux-androideabi-4.6/prebuilt/windows/bin/. /lib/gcc/arm-linux-androideabi/4.6/. /.. /.. /.. /arm-linux-androideabi/bin/ld.exe:d:/programfile\opencv-2.4.9-android-sdk\sdk\native\jni\/. /3RDPARTY/LIBS/ARMEABI-V7A/LIBLIBPNG.A (PNGREAD.C.O): in function png_create_read_struct_2:pngread.c (. text.png_ create_read_struct_2+0x112): error:undefined reference to ' Inflateinit_ ' d:/programfile/android-ndk-r9d/toolchains/ arm-linux-androideabi-4.6/prebuilt/windows/bin/. /lib/gcc/arm-linux-androideabi/4.6/. /.. /.. /.. /arm-linux-androideabi/bin/ld.exe:d:/programfile\opencv-2.4.9-android-sdk\sdk\native\jni\/. /3RDPARTY/LIBS/ARMEABI-V7A/LIBLIBPNG.A (PNGREAD.C.O): in function png_read_row:pngread.c (. text.png_read_row+0x218) : error:undefined reference to ' inflate ' d:/programfile/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/ prebuilt/windows/bin/. /lib/gcc/arm-linux-androideabi/4.6/. /.. /.. /.. /arm-linux-androideabi/bin/ld.exe:d:/programfile\opencv-2.4.9-android-sdk\sdk\native\jni\/. /3RDPARTY/LIBS/ARMEABI-V7A/LIBLIBPNG.A (PNGREAD.C.O): in function png_read_destroy:pngread.c (. Text.png_read_ destroy+0x96): error:undefined refErence to ' Inflateend ' d:/programfile/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/. /lib/gcc/arm-linux-androideabi/4.6/. /.. /.. /.. /arm-linux-androideabi/bin/ld.exe:d:/programfile\opencv-2.4.9-android-sdk\sdk\native\jni\/. /3RDPARTY/LIBS/ARMEABI-V7A/LIBLIBPNG.A (PNGWRITE.C.O): in function png_write_flush:pngwrite.c (. Text.png_write_ FLUSH+0X1C): error:undefined reference to ' deflate '
on two, each is the preview interface to detect faces and take photos after the test:




second, Android with JNI tune OpenCV Another method of configuration

Application.mk file Ibid. ANDROID.MK files such as the following:

Local_path: = $ (call My-dir)
Include $ (clear_vars)

Opencv_install_modules:=on
Opencv_camera_modules:=off
Opencv_lib_type:=share
Ifeq ("$ (wildcard $ (Opencv_mk_path))", "" ")
Include D:\ProgramFile\OpenCV-2.4.9-android-sdk\sdk\native\jni\OpenCV.mk
Else
Include $ (opencv_mk_path)
endif


Local_module: = processimg
Local_src_files: = detectface_jni.cpp \
Src/copytoassets.cpp \
Src/detectface.cpp
Local_ldlibs: =-lm-llog

Opencv4android Explanation: Dialysis Android with JNI OpenCV three ways (let Opencvmanager never bothered)

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.