android 使用opencv

1、将已有的项目名称改名字,但一直报错

Error:A problem occurred configuring project ':app'.
> executing external native build for cmake /home/xia/ncnn_demo/ssd_mobilenet_opencv/app/CMakeLists.txt

解决办法:直接删掉app/.externalNativeBuild即可

2、在main下新建文件夹jniLibs,将OpenCV SDK 目录 sdk\native\libs 下的 armeabiarmeabi-v7a 复制到  jniLibs 目录下;

3、修改CMakeLists.txt

# Sets the minimum version of CMake required to build the native
# library. You should either keep the default value or only pass a
# value of 3.4.0 or lower.


#include头文件目录
include_directories(src/main/cpp/include
                    src/main/cpp/)

#source directory源文件目录
file(GLOB SSD_SRC src/main/cpp/*.h
                    src/main/cpp/*.cpp)
set(SSD_COMPILE_CODE ${SSD_SRC})


cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds it for you.
# Gradle automatically packages shared libraries with your APK.

add_library(libncnn STATIC IMPORTED )
set_target_properties(libncnn
  PROPERTIES IMPORTED_LOCATION
  ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libncnn.a)

# opencv import libs

set(pathToOpenCV /home/xia/OpenCV-android-sdk)

include_directories( ${pathToOpenCV}/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)




#编译为动态库
add_library(ssd SHARED ${SSD_COMPILE_CODE})

#添加工程所依赖的库
find_library(  log-lib log )
target_link_libraries( ssd
                       libncnn
                       lib_opencv
                       jnigraphics
                       z
                       ${log-lib}
                        )

 

4、在cpp文件中调用即可

参考:https://blog.csdn.net/youngpan1101/article/details/53614588

 

posted @ 2018-05-22 16:42  牧马人夏峥  阅读(1066)  评论(0编辑  收藏  举报