栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

android studio 3.5.2 配置 opencv-4.5.2-android-sdk

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

android studio 3.5.2 配置 opencv-4.5.2-android-sdk

测试C++使用 opencv处理CameraX 拍摄的照片,整了好几天,一步一个坑,记录一下最后成功的配置

第一步:把opencv-4.5.2-android-sdk OpenCV-android-sdksdknativejni 中的include文件夹放到 与CMakelists.txt 同级目录

第二步:把opencv-4.5.2-android-sdk OpenCV-android-sdksdknative 中的libs 文件夹拷贝到项目的main目录下 并改名为jniLibs

第三步:修改 CMakeLists.txt

其实修改部分为:

target_link_libraries( # Specifies the target library.
                       native-lib
                       libopencv_java4 #这是新增的
                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib}
                       )
# 设置include文件夹的地址
include_directories(${CMAKE_SOURCE_DIR}/include)


# 设置opencv的动态库,注意这里需要把项目路径填写进去,尽量注意不要填写错误,
# 笔者在这里也遇到过路径错误导致找不到.so文件
add_library(libopencv_java4 SHARED IMPORTED)
set_target_properties(libopencv_java4 PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libopencv_java4.so)
#link_directories(${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI})

完整文件:
# 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)

# 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 )

# 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.


#include_directories(${CMAKE_SOURCE_DIR}/../../opencv/include)
#link_directories(${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI})


target_link_libraries( # Specifies the target library.
                       native-lib
                       libopencv_java4
                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib}
                       )
################################################################Native


# 设置include文件夹的地址
include_directories(${CMAKE_SOURCE_DIR}/include)


# 设置opencv的动态库,注意这里需要把项目路径填写进去,尽量注意不要填写错误,
# 笔者在这里也遇到过路径错误导致找不到.so文件
add_library(libopencv_java4 SHARED IMPORTED)
set_target_properties(libopencv_java4 PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libopencv_java4.so)
#link_directories(${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI})



第四步:修改build.gradle(Module:app) 

            我在网上找了很多配置,最后又一点儿点儿的测试 注释掉,最后只剩下了两处改动就可以正常运行:

arguments "-DANDROID_STL=c++_shared"
sourceSets{
    main{
        jniLibs.srcDirs=["/src/main/jniLibs"]
    }
}
apply plugin: 'com.android.application'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"
    defaultConfig {
        applicationId "com.njm.myobj05"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags "-std=c++11"
                arguments "-DANDROID_STL=c++_shared"
            }
        }
      //  ndk {
        //    abiFilters  'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
      //  }

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
            version "3.10.2"
            //arguments "-DANDROID_STL=c++_shared"
        }
    }


    sourceSets{
        main{
            jniLibs.srcDirs=["/src/main/jniLibs"]
        }
    }







}


dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'

   
    //def camerax_version = "1.1.0-alpha05"
    def camerax_version = "1.1.0-alpha08"
// CameraX core library using camera2 implementation
    implementation "androidx.camera:camera-camera2:$camerax_version"
// CameraX Lifecycle Library
    implementation "androidx.camera:camera-lifecycle:$camerax_version"
// CameraX View class
    implementation "androidx.camera:camera-view:1.0.0-alpha27"
}
// native opencv


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/883443.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号