IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> Android Studio使用opencv4.5.5(CmakeList<3.18.1版本> NDK配置) -> 正文阅读

[移动开发]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 写法
另外,网上有大量博客在采用 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

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2022-04-26 11:52:02  更:2022-04-26 11:52:28 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 23:27:03-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码