Android Studio使用opencv4.5.5(CmakeList<3.18.1版本> NDK配置)

博客背景:博主想用android显示c++处理后的相机数据,把画面实时展示出来,并研究如何通过CMakeLists.txt配置,采用自己写的c++文件,与android交互。转载请注明出处:Android Studio使用opencv4.5.5 CMakeLists.txt NDK配置

网上找到一个例子,存在问题

1:例子过于老旧(5年前的),其中的CMakeLists.txt文件可能由于CMake版本及opencv sdk版本不同,导致小编自己的环境无法运行(小编的CMake,opencv sdk都是当前最新的版本),且项目中关于opencv的部分代码不全
2:网上搜索大量关于CMakeLists.txt NDK开发使用opencv sdk时候的配置,大都是将opencv sdk中include文件夹还有so库文件copy到项目的cpp文件夹中,千篇一律,也不符合软件开发的思想

解决办法:

1、CMakeLists.txt采用当前最新版本3.18.1(当前Android Studio中最新的CMake版本是3.18.1)
2、opencv sdk采用CMakeLists.txt中配置引用的方式,不再copy到项目
3、修改例子中的部分jni代码,使其符合当前jni代码规范要求,或者可以理解为是最新的的jni 写法
另外,在使用opencv里面的java方法时需要将opencv按module引入,网上有大量博客在引用的时候也是过于麻烦(不知道是当时opencv版本低的问题,还是出于其他考虑<怕包太大,不想把so库引用过来,还是没有思考>),引用opencv 作为module的时候,直接选sdk文件夹即可按module引用,可参考小编另一篇博客Android Studio配置OpenCV 4.4.0(进行边缘检测、霍夫直线识别)

为了避免重复造轮子,找了两个例子,和二为一后运行,
实时显示 Opencv处理后的Camera图像 AndroidStudio NDK方法
https://github.com/sd707589/Camera1GLTest1
Android Studio 集成OpenCV
能运行的open cv demo例子 https://gitee.com/hugang2021/open-cv-demo-for-android

一、Android Studio新建C++工程

二、app build.gradle 配置

plugins {
    id 'com.android.application'
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "opencv4unity.camera1gltest1"
        minSdk 27
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags ''
                abiFilters 'armeabi-v7a'
                //远程下载 libc++_shared.so
                arguments '-DANDROID_STL=c++_shared'
            }
        }

        ndk {
            abiFilters 'armeabi-v7a'
        }
    }
    sourceSets {
        main {
            jniLibs.srcDirs = ['src/main/cpp/libs']
        }
    }
    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 'jni/CMakeLists.txt'
            path file('src/main/cpp/CMakeLists.txt')
            version '3.18.1'
        }
    }
    buildFeatures {
        viewBinding true
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

}

三、修改CMakeLists.txt文件,直接上代码

第一种:采用引用本地下载的opencv sdk的方法(建议采用这种)

说明:最好是在新建C++工程后,系统生成的CMakeLists.txt基础上去修改CMakeLists.txt文件,而不是复制黏贴别人工程里的CMakeLists.txt文件。CMake版本不一样,直接整个CMakeLists.txt文件copy过来不一定能用,最好copy某一部分过来,而不是整个文件copy

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

# Declares and names the project.

project("camera1gltest1")

set(pathToOpenCv D:/opencv-4.5.5-android-sdk/OpenCV-android-sdk)#设置OpenCv的路径变量
#配置加载native依赖
include_directories(${pathToOpenCv}/sdk/native/jni/include)
#动态方式加载
add_library(lib_opencv STATIC IMPORTED) #表示创建一个导入库,静态方式
#引入libopencv_java4.so文件
set_target_properties(lib_opencv
        PROPERTIES
        IMPORTED_LOCATION ${pathToOpenCv}/sdk/native/libs/armeabi-v7a/libopencv_java4.so
        #IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${}/libopencv_java4.so
        )

#设置系统变量,目的是为了让CMake知道在哪个目录下找到我们的libopencv_java4.so这个库
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L${CMAKE_SOURCE_DIR}/libs/${CMAKE_ANDROID_ARCH_ABI}")

message("========= ${ANDROID_ABI} =========")
message("========= ${CMAKE_ANDROID_ARCH_ABI} =========")



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

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        native-lib.cpp
        imgProcess.cpp
        ExtractCase.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.

target_link_libraries( # Specifies the target library.
        camera1gltest1
        lib_opencv
        #opencv_java4
        # Links the target library to the log library
        # included in the NDK.
        ${log-lib}
        android
        jnigraphics
        )

目录结构
在这里插入图片描述

第二种:采用复制opencv sdk中include文件夹即so库到项目的方法

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

# Declares and names the project.

project("camera1gltest1")

#目的是为了让CMake找到我们的头文件在哪里,这里是在 cpp/include 这个文件夹下
include_directories(include)

#设置系统变量,目的是为了让CMake知道在哪个目录下找到我们的libopencv_java4.so这个库
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L${CMAKE_SOURCE_DIR}/libs/${CMAKE_ANDROID_ARCH_ABI}")

message("========= ${ANDROID_ABI} =========")
message("========= ${CMAKE_ANDROID_ARCH_ABI} =========")



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

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        native-lib.cpp
        imgProcess.cpp
        ExtractCase.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.

target_link_libraries( # Specifies the target library.
        camera1gltest1

        opencv_java4
        # Links the target library to the log library
        # included in the NDK.
        ${log-lib}
        android
        jnigraphics
        )

目录结构
在这里插入图片描述

参考
Android 接入 OpenCV库的三种方式
实时显示 Opencv处理后的Camera图像 AndroidStudio NDK方法
https://github.com/sd707589/Camera1GLTest1
Android Studio 集成OpenCV
能运行的open cv demo例子 https://gitee.com/hugang2021/open-cv-demo-for-android
Android基于CMake进行OpenCV开发配置
解决Android运行出现NDK at /Library/Android/sdk/ndk-bundle did not have a source.properties file
Android开发中的NDK到底是什么?(详细解析+案例)

【Android NDK】cannot initialize a parameter of type ‘jboolean *’ (aka ‘unsigned char *’) with an rva
编译问题追踪: Expected elements are <{}codename>,<{}layoutlib>,<{}api-level>
Warning: 意外的元素 (uri:““, local:“base-extension“)。所需元素为<{}codename>,<{}layoutlib>,<{}api-level>
NDK does not contain any platforms问题解决
Android Studio gardle 配置 ndk 指定 ABI: abiFilters 详解
AS上利用NDK——CMake方法移植ORB SLAM算法到Android

Android Studio 配置 OpenCV4+
opencv配置 https://github.com/ITQmz/opencv
Android Studio 中集成 OpenCV (Java 和 NDK 均可,不使用Manager)
Android studio上在Java层和Native层配置OpenCV4.1.0
Android studio+opencv-4.1.0 开发环境搭建(一)
2021年7月1日:AndroidStudio集成opencv指南。
安卓数字图像处理实战(1)——集成并使用OpenCV4.4
Android Studio 配置OpenCV4.4.0 不用安装OpenCV Manager (泪崩居然用了礼拜天2天的时间居然还没配置成功,今天又看了下配置成功了)
Android Studio之配置OpenCV
AndroidStudio配置opencv
Android Studio OpenCV 4.5.2环境搭建
Android Studio4.0导入OpenCv4.3.0的方法步骤
Android Studio Cmake配置opencv
【Bug】did not have a source.properties file
Android:NDK at D:\develop\androidAs\android-sdk\ndk-bundle did not have a source.properties file
2017年Android Studio做NDK情况调查

2022.04.25 0:18 扛不住了,休息 sh ylxy3

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值