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

  1. cmake_minimum_required(VERSION 3.10.2)
  2. # Declares and names the project.
  3. project("myapplication")
  4. include_directories(${CMAKE_SOURCE_DIR}/cpp/include)
  5. # Creates and names a library, sets it as either STATIC
  6. # or SHARED, and provides the relative paths to its source code.
  7. # You can define multiple libraries, and CMake builds them for you.
  8. # Gradle automatically packages shared libraries with your APK.
  9. #动态方式加载 SHARED
  10. # 静态加载STATIC
  11. add_library(lib_opencv SHARED IMPORTED )
  12. message("../../..")
  13. message("${ANDROID_ABI}")
  14. message("${CMAKE_SOURCE_DIR}")
  15. message("<><><>><><><><><><><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
  16. #引入libopencv_java4.so文件 路径不要有问题
  17. set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION
  18. ${CMAKE_SOURCE_DIR}/jniLibs/libs/${ANDROID_ABI}/libopencv_java4.so)
  19. add_library(libMNN STATIC IMPORTED)
  20. add_library(libMNN_CL STATIC IMPORTED)
  21. add_library(libMNN_Vulkan STATIC IMPORTED)
  22. add_library(libMNN_Express STATIC IMPORTED)
  23. set_target_properties(libMNN PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/jniLibs/libs/${ANDROID_ABI}/libMNN.so)
  24. set_target_properties(libMNN_CL PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/jniLibs/libs/${ANDROID_ABI}/libMNN_CL.so)
  25. set_target_properties(libMNN_Vulkan PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/jniLibs/libs/${ANDROID_ABI}/libMNN_Vulkan.so)
  26. set_target_properties(libMNN_Express PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/jniLibs/libs/${ANDROID_ABI}/libMNN_Express.so)
  27. add_library( # Sets the name of the library.
  28. native-lib
  29. # Sets the library as a shared library.
  30. SHARED
  31. # Provides a relative path to your source file(s).
  32. cpp/native-lib.cpp)
  33. # Searches for a specified prebuilt library and stores the path as a
  34. # variable. Because CMake includes system libraries in the search path by
  35. # default, you only need to specify the name of the public NDK library
  36. # you want to add. CMake verifies that the library exists before
  37. # completing its build.
  38. add_definitions(-DMNN_USE_LOGCAT)
  39. find_library( # Sets the name of the path variable.
  40. log-lib
  41. # Specifies the name of the NDK library that
  42. # you want CMake to locate.
  43. log )
  44. # Specifies libraries CMake should link to your target library. You
  45. # can link multiple libraries, such as libraries you define in this
  46. # build script, prebuilt third-party libraries, or system libraries.
  47. target_link_libraries( # Specifies the target library.
  48. native-lib
  49. android #-ljnigraphic
  50. lib_opencv
  51. libMNN libMNN_CL libMNN_Vulkan libMNN_Express
  52. # Links the target library to the log library
  53. # included in the NDK.
  54. ${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} )