Android Studio opencv开发配置

时间:2021-01-04 18:00:06

1 下载Android Studio

https://developer.android.com/studio/archive?hl=zh-cn

2 安装Android Studio

安装完成,下载SDK NDK LLIB  Cmake

FIle-Setting 

Android Studio   opencv开发配置

 

Android Studio   opencv开发配置

 

3  开始建立工程使用

4 配置

Android Studio   opencv开发配置

Android Studio   opencv开发配置

Android Studio   opencv开发配置

 

CMakeLists.txt 

1 包括三个三方库test1.so test.so gnustl_shared.so

2 加入opencv的支持

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)




#step1 ##################### OpenCV 环境 ############################
#参考https://www.jianshu.com/p/6e16c0429044
#设置OpenCV-android-sdk路径
set(OpenCV_STATIC ON)
set(OpenCV_DIR C:/software/opencv-4.0.1-android-sdk/OpenCV-android-sdk/sdk/native/jni )

find_package(OpenCV REQUIRED )
if(OpenCV_FOUND)
    include_directories(${OpenCV_INCLUDE_DIRS})
    message(STATUS "OpenCV library status:")
    message(STATUS "    version: ${OpenCV_VERSION}")
    message(STATUS "    libraries: ${OpenCV_LIBS}")
    message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")
else(OpenCV_FOUND)
    message(FATAL_ERROR "OpenCV library not found")
endif(OpenCV_FOUND)

#set(OpenCV_STATIC ON)
#set( OpenCV_DIR C:/software/opencv-3.4.5-android-sdk/OpenCV-android-sdk/sdk/native/jni )
#find_package (OpenCV REQUIRED)
#
#target_link_libraries(native-lib ${OpenCV_LIBS})


#step2 设置so库路径
#参考 https://blog.csdn.net/carryWorld/article/details/75026171
include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp/include)
set(jnilibs "${CMAKE_SOURCE_DIR}/src/main/jniLibs")
#set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${jnilibs}/${ANDROID_ABI})



# ###################### 项目原生模块 ###########################
# 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 them for you.
# Gradle automatically packages shared libraries with your APK.

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).
        native-lib.cpp)

#step3 加入共享库
add_library(test1 SHARED IMPORTED)
add_library(test2 SHARED IMPORTED)
add_library(gnustl_shared SHARED IMPORTED)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

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)

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

message($CMAKE_SOURCE_DIR)
set_target_properties(test1
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libtest1.so)

set_target_properties(test2
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libtest2.so)

set_target_properties(gnustl_shared
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libgnustl_shared.so)

target_link_libraries( # Specifies the target library.
        native-lib
        ${OpenCV_LIBS}
        jnigraphics
        log
        test1
        test2
        gnustl_shared
        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

 

5 开始编译