2016-12-15 74 views
0

我正在使用本機代碼並通過使用ndk-build在android studio項目中打開cv,但我想使用cmake。請告訴我如何在cmakelists.txt文件中設置這些東西。 這是我的Android.mk文件在cmakelists.txt中添加opencv android

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:/OpenCV-3.1.0-android-sdk/sdk/native/jni/OpenCV.mk 
else 
include $(OPENCV_MK_PATH) 
endif 

LOCAL_MODULE := module_name 

LOCAL_CFLAGS := -DANDROID_NDK \ 
       -DDISABLE_IMPORTGL 

LOCAL_SRC_FILES := src/main/cpp/test.cpp 

LOCAL_LDLIBS += -lm -llog -ljnigraphics 
include $(BUILD_SHARED_LIBRARY) 

如何設置的cmake

回答

2

這些東西它是爲時已晚,但有人可能會發現它很有用。 如果您還將linonfree.so添加到jniLibs文件夾,請首先將libopencv_java.so用於opencv 2或libopencv_java3.so用於oepncv3,並放在不同架構下的app/src/main/jniLibs文件夾中。將cmake鏈接到gradle。
這裏是CmakeLists。

cmake_minimum_required(VERSION 3.4.1) 
     set(EXECUTABLE_OUTPUT_PATH  "${CMAKE_CURRENT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}") 
    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) 
#the nonfree optional if you have build the nonfree.so of the xfeature2d module  
add_library(# Sets the name of the library 
      nonfree 
      # Sets the library as shared library. 
      SHARED 
      # indicate the library is prebuilt. 
      IMPORTED) 
set_target_properties(# Specifies the targeted library. 
         nonfree 
         # Specifies the parameter you want to define. 
          PROPERTIES IMPORTED_LOCATION 
         # Specifies the location of the library. 
         ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libnonfree.so) 
    include_directories(C:/Users/what/Documents/OpenCV-android-sdk/sdk/native/jni/include) 
    add_library(lib-opencv SHARED IMPORTED) 
    set_target_properties(lib-opencv PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so) 
    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 
          # prebuilt library nonfree. 
          nonfree 
          # prebuilt library opencv java. 
          lib-opencv 
          # Links the target library to the log library 
          # included in the NDK. 
          ${log-lib}) 

在build.gradle(app)中設置jniLis.srcDirs文件夾路徑。

sourceSets { 
    main { 
     // let gradle pack the shared library into apk 
     jniLibs.srcDirs = ['src/main/jniLibs'] 
    } 
    }