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

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

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

第二步:把opencv-4.5.2-android-sdk OpenCV-android-sdk\sdk\native 中的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"
        }
    }

/*
   packagingOptions {
        pickFirst 'lib/armeabi-v7a/libopencv_java4.so'
        pickFirst 'lib/arm64-v8a/libopencv_java4.so'
        pickFirst 'lib/x86_64/libopencv_java4.so'
        pickFirst 'lib/x86/libopencv_java4.so'
    }
*/
    sourceSets{
        main{
            jniLibs.srcDirs=["/src/main/jniLibs"]
        }
    }


/*
    splits {
        abi {
            enable true
            reset()
            include 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a' //select ABIs to build APKs for
            universalApk true //generate an additional APK that contains all the ABIs
        }
    }


   // project.ext.versionCodes = ['armeabi': 1, 'armeabi-v7a': 2, 'arm64-v8a': 3, 'mips': 5, 'mips64': 6, 'x86': 8, 'x86_64': 9]
    project.ext.versionCodes = ['armeabi': 1, 'armeabi-v7a': 2, 'arm64-v8a': 3, 'mips': 5, 'mips64': 6, 'x86': 8, 'x86_64': 9]
    android.applicationVariants.all { variant ->
        variant.outputs.each { output ->
            output.versionCodeOverride =
                    project.ext.versionCodes.get(output.getFilter(com.android.build.OutputFile.ABI), 0) * 1000000 + android.defaultConfig.versionCode
        }
    }

*/




}


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'

   /* // CameraX core library
    def camerax_version = '1.0.0-beta04'
    implementation "androidx.camera:camera-core:$camerax_version"
    // CameraX Camera2 extensions
    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-alpha11'
    */
    //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


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值