美文网首页
在Android上通过JNI的形式,导入OpenCV

在Android上通过JNI的形式,导入OpenCV

作者: andforce | 来源:发表于2019-05-28 15:16 被阅读0次

    下载OpnenCV For Android

    OpenCV官网--Release--Android,即可下载到最新的OpenCV的Android版本

    image.png

    认识一下OpenCV For Android的目录结构

    .
    ├── LICENSE
    ├── README.android
    ├── samples
    │   ├── 15-puzzle
    │   │   ├── AndroidManifest.xml
    │   │   ├── build.gradle
    │   │   ├── res
    │   │   └── src
    │   ├── build.gradle
    │   ├── camera-calibration
    │   │   ├── AndroidManifest.xml
    │   │   ├── build.gradle
    │   │   ├── res
    │   │   └── src
    │   ├── color-blob-detection
    │   │   ├── AndroidManifest.xml
    │   │   ├── build.gradle
    │   │   ├── res
    │   │   └── src
    │   ├── face-detection
    │   │   ├── AndroidManifest.xml
    │   │   ├── build.gradle
    │   │   ├── jni
    │   │   ├── res
    │   │   └── src
    │   ├── gradle
    │   │   └── wrapper
    │   ├── gradle.properties
    │   ├── gradlew
    │   ├── gradlew.bat
    │   ├── image-manipulations
    │   │   ├── AndroidManifest.xml
    │   │   ├── build.gradle
    │   │   ├── res
    │   │   └── src
    │   ├── settings.gradle
    │   ├── tutorial-1-camerapreview
    │   │   ├── AndroidManifest.xml
    │   │   ├── build.gradle
    │   │   ├── res
    │   │   └── src
    │   ├── tutorial-2-mixedprocessing
    │   │   ├── AndroidManifest.xml
    │   │   ├── build.gradle
    │   │   ├── jni
    │   │   ├── res
    │   │   └── src
    │   └── tutorial-3-cameracontrol
    │       ├── AndroidManifest.xml
    │       ├── build.gradle
    │       ├── res
    │       └── src
    └── sdk
        ├── build.gradle
        ├── etc
        │   ├── haarcascades
        │   ├── lbpcascades
        │   ├── licenses
        │   ├── valgrind_3rdparty.supp
        │   └── valgrind.supp
        ├── java
        │   ├── AndroidManifest.xml
        │   ├── javadoc
        │   ├── res
        │   └── src
        └── native
            ├── 3rdparty
            ├── jni
            ├── libs
            └── staticlibs
    

    下载并配置好NDK

    image.png

    在SDKTools中,要下载好LLDB,CMake,NDK

    image.png

    用Studio创建 Native C++工程

    Toolchain选C++11


    image.png

    拷贝OpenCV中的so库到工程中

    sdk/
    ├── build.gradle
    ├── etc
    │   ├── haarcascades
    │   ├── lbpcascades
    │   ├── licenses
    │   ├── valgrind_3rdparty.supp
    │   └── valgrind.supp
    ├── java
    │   ├── AndroidManifest.xml
    │   ├── javadoc
    │   ├── res
    │   └── src
    └── native
        ├── 3rdparty
        ├── jni
        ├── libs
        └── staticlibs
    

    把sdk/native/libs下的so拷贝到工程的app/src/main/jniLibs中

    sdk/native/libs/
    ├── arm64-v8a
    │   └── libopencv_java4.so
    ├── armeabi-v7a
    │   └── libopencv_java4.so
    ├── x86
    │   └── libopencv_java4.so
    └── x86_64
        └── libopencv_java4.so
    

    如下图位置:

    image.png
    ps:如果不打算支持x86架构,可以不需要拷贝x86开头的那几个

    修改CMakeLists.txt

    1.添加头文件

    # OpenCV 头文件路径
    include_directories(/home/andforce/Desktop/opencv-4.1.0-android-sdk/OpenCV-android-sdk/sdk/native/jni/include)
    

    2.引入so

    #引入libopencv_java4.so文件
    add_library(opencv_java4 SHARED IMPORTED)
    
    set_target_properties(opencv_java4
            PROPERTIES
            # ${PROJECT_SOURCE_DIR}
            # ${CMAKE_CURRENT_SOURCE_DIR}
            # ${CMAKE_SOURCE_DIR}
            IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libopencv_java4.so
            )
    

    3.添加自己的so

    # 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.
            flood-fill
    
            # Sets the library as a shared library.
            SHARED
    
            # Provides a relative path to your source file(s).
            cpp_java_utils.cpp
            find_objects.cpp
            mat_map_cvt.cpp
            flood_fill.cpp)
    

    4.添加bitmap支持,一般OpenCV都需要跟Bitmap交互,如果不处理Bitmap,可以不添加

    # 支持操作Bitmap的库
    find_library( # Sets the name of the path variable.
            jnigraphics-lib
            # Specifies the name of the NDK library that
            # you want CMake to locate.
            jnigraphics )
    

    5.最后把自己的库和opencv库添加上

    # 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.
            flood-fill
            opencv_java4
            # Links the target library to the log library
            # included in the NDK.
            ${jnigraphics-lib}
            ${log-lib})
    

    完整的CMakeLists.txt

    # 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)
    
    # OpenCV 头文件路径
    include_directories(/home/andforce/Desktop/opencv-4.1.0-android-sdk/OpenCV-android-sdk/sdk/native/jni/include)
    
    #引入libopencv_java4.so文件
    add_library(opencv_java4 SHARED IMPORTED)
    
    set_target_properties(opencv_java4
            PROPERTIES
            # ${PROJECT_SOURCE_DIR}
            # ${CMAKE_CURRENT_SOURCE_DIR}
            # ${CMAKE_SOURCE_DIR}
            IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libopencv_java4.so
            )
    
    # 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.
            flood-fill
    
            # Sets the library as a shared library.
            SHARED
    
            # Provides a relative path to your source file(s).
            cpp_java_utils.cpp
            find_objects.cpp
            mat_map_cvt.cpp
            flood_fill.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.
    
    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)
    
    # 支持操作Bitmap的库
    find_library( # Sets the name of the path variable.
            jnigraphics-lib
            # Specifies the name of the NDK library that
            # you want CMake to locate.
            jnigraphics )
    
    # 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.
            flood-fill
            opencv_java4
            # Links the target library to the log library
            # included in the NDK.
            ${jnigraphics-lib}
            ${log-lib})
    

    github demo: https://github.com/andforce/OpenCV4ImportDemo

    相关文章

      网友评论

          本文标题:在Android上通过JNI的形式,导入OpenCV

          本文链接:https://www.haomeiwen.com/subject/uanrtctx.html