opencv 动态库
https://blog.csdn.net/wanggao_1990/article/details/105139586
若编译运行提示缺少 libc++_shared.so库,java.lang.UnsatisfiedLinkError: dlopen failed: library “libc++_shared.so” not found。添加其支持,位于 build.gradle的defaultConfig->externalNativeBuild->cmake:
复制Include
在src下创建一个opencv目录,将sdk的OpenCV-4.2.0-android-sdk\sdk\native\jni的include目录复制到src/main/cpp这里,用于引用c++头文件。
复制opencv.so 动态库
在main下创建jniLib目录,并复制sdk中的OpenCV-4.2.0-android-sdk\sdk\native\libs下所有文件到src/main/jniLabs这里,没有可以创建通过,点击src/main/ 右键,new/folder/jni folder ,新建一个文件,用于打包到app中。
原文链接:https://blog.csdn.net/wanggao_1990/article/details/105139586
cmake
cmake_minimum_required(VERSION 3.10.2)# Declares and names the project.project("myapplication")include_directories(${CMAKE_SOURCE_DIR}/cpp/include)# 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.#动态方式加载 SHARED# 静态加载STATICadd_library(lib_opencv SHARED IMPORTED )message("../../..")message("${ANDROID_ABI}")message("${CMAKE_SOURCE_DIR}")message("<><><>><><><><><><><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")#引入libopencv_java4.so文件 路径不要有问题set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION${CMAKE_SOURCE_DIR}/jniLibs/libs/${ANDROID_ABI}/libopencv_java4.so)add_library(libMNN STATIC IMPORTED)add_library(libMNN_CL STATIC IMPORTED)add_library(libMNN_Vulkan STATIC IMPORTED)add_library(libMNN_Express STATIC IMPORTED)set_target_properties(libMNN PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/jniLibs/libs/${ANDROID_ABI}/libMNN.so)set_target_properties(libMNN_CL PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/jniLibs/libs/${ANDROID_ABI}/libMNN_CL.so)set_target_properties(libMNN_Vulkan PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/jniLibs/libs/${ANDROID_ABI}/libMNN_Vulkan.so)set_target_properties(libMNN_Express PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/jniLibs/libs/${ANDROID_ABI}/libMNN_Express.so)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).cpp/native-lib.cpp)# 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.add_definitions(-DMNN_USE_LOGCAT)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.target_link_libraries( # Specifies the target library.native-libandroid #-ljnigraphiclib_opencvlibMNN libMNN_CL libMNN_Vulkan libMNN_Express# Links the target library to the log library# included in the NDK.${log-lib} )
可以查看message信息
app/.cxx/cmake/debug/arm64-v8a/build_output.txt
app/build.gradle
android {
    compileSdkVersion 28
    buildToolsVersion "28.0.1"
    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags "-std=c++11 -frtti -fexceptions" // 根据情况设置
                abiFilters  'armeabi-v7a', 'arm64-v8a' // 根据情况设置
                arguments "-DANDROID_ARM_NEON=TRUE", "-DANDROID_TOOLCHAIN=clang","-DANDROID_STL=c++_shared"
                //abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a','arm64-v8a', 'mips', 'mips64'
            }
        }
    }
    externalNativeBuild {
        cmake {
            path "src/main/CMakeLists.txt"
            version "3.10.2"
        }
    }
     sourceSets {
        main {
            jni {
                srcDirs 'src/main/jniLibs' //'src/main/jni',
            }
            jniLibs.srcDirs = ['src/main/jniLibs']
            assets {
                srcDirs 'src/main/assets'
            }
        }
    }
}
SDK版本号太高29,30 及以上版本读写Image图片会有问题。
cmake 参数
opencv 静态库
https://www.cnblogs.com/xiaoxiaoqingyi/p/6676096.html
静态库编译老是缺东西。
# 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.10.2)
# Declares and names the project.
project("myapplication")
include_directories(${CMAKE_SOURCE_DIR}/cpp/include)
# 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.
message("../../..")
message("${ANDROID_ABI}")
message("${CMAKE_SOURCE_DIR}")
message("<><><>><><><><><><><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
#动态方式加载 SHARED
# 静态加载   STATIC
##set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_java4.so )
#add_library(lib_opencv STATIC IMPORTED )
#set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION
#        ${CMAKE_SOURCE_DIR}/jni/${ANDROID_ABI}/libopencv_java4.so)
#find_package(OpenCV  REQUIRED) #寻找这个包 opencv 包名 4 最低版本号 REQUIRED 如果没找到就报错
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")
message(STATUS "   LD_LIBRARY_PATH  path: ${LD_LIBRARY_PATH}")
set(OpenCV_STATIC ON)
#second step to use opencv
set(OpenCV_DIR /media/wwf/data/ubuntuSoft/opencv-4.5.1-android-sdk/OpenCV-android-sdk/sdk/native/jni )
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")
message(STATUS "   LD_LIBRARY_PATH  path: ${LD_LIBRARY_PATH}")
find_package(OpenCV 4 REQUIRED) #寻找这个包 opencv 包名 4 最低版本号 REQUIRED 如果没找到就报错
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")
message(STATUS "   LD_LIBRARY_PATH  path: ${LD_LIBRARY_PATH}")
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).
        cpp/native-lib.cpp)
# 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.
add_definitions(-DMNN_USE_LOGCAT)
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.
# opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_gapi;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_stitching;opencv_video;opencv_videoio
target_link_libraries( # Specifies the target library.
                        native-lib
                        android #-ljnigraphic
                        ${OpenCV_LIBS}
                        # Links the target library to the log library
                        # included in the NDK.
                        ${log-lib} )
                    