Android中NDK开发-OpenCV集成_android集成opencv(ndk)-程序员宅基地

技术标签: ndk  android  opencv  

关于Android studio ndk的配置大家可以在网上找教程进行,这里不介绍了。

1.OpenCV下载及准备

下载地址:https://opencv.org/releases/

找到最新版本(当前最新4.5.3)的下载地址,点击Android进入下载

解压出如下文件夹

 2.下面开始建Android项目配置

 新建的项目自己带一个native-lib实例,可以测试跑一下

 修改要我们要支持的CPU架构下,后面会附完整build.gradle文件代码

 题外话:调用OpenCV4Android中的so动态库用于打包进APK,下图中红框内的路径就是我们上面下载的OpenCV4.1.0中的动态库路径(注意:现在已经不需要配置这个啦,会报错 2 files found with path 'lib/armeabi-v7a/libopencv_java4.so' from inputs:

具体解释在https://developer.android.com/studio/releases/gradle-plugin#cmake-imported-targets

 3.完整build.gradle文件

plugins {
    id 'com.android.application'
}

android {
    compileSdk 30

    defaultConfig {
        applicationId "com.example.opencvdemo"
        minSdk 21
        targetSdk 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags ''
                abiFilters 'x86','armeabi-v7a'
                arguments "-DANDROID_ARM_NEON=TRUE",'-DANDROID_STL=c++_shared'
            }
        }
    }
//
//    sourceSets{
//        main{
//            //当前这个目录下的库文件会被调用并且被打包进apk中
//            jniLibs.srcDirs = ['D:/download/OpenCV-android-sdk/sdk/native/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 file('src/main/cpp/CMakeLists.txt')
            version '3.10.2'
        }
    }
    buildFeatures {
        viewBinding true
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.13.2'

}

4.修改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.10.2)
#project("OpenCVDemo")
# Declares and names the project.

#该变量为真时会创建完整版本的Makefile
set(CMAKE_VERBOSE_MAKEFILE on)
#定义变量ocvlibs使后面的命令可以使用定位具体的库文件
set(opencvlibs "D:/download/OpenCV-android-sdk/sdk/native/libs")
#调用头文件的具体路径
include_directories(D:/download/OpenCV-android-sdk/sdk/native/jni/include)

#增加我们的动态库
add_library(libopencv_java4 SHARED IMPORTED)
#建立链接
set_target_properties(libopencv_java4 PROPERTIES IMPORTED_LOCATION
        "${opencvlibs}/${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.
        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.

target_link_libraries( # Specifies the target library.
        native-lib libopencv_java4 jnigraphics

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

 修改完后最好执行一下如下操作:

 执行完后切换到Android视图下会在cpp--includes下看到opencv2已导入

编译可能会报如下错误提示:

java.lang.UnsatisfiedLinkError: dlopen failed: library "libc++_shared.so" not found

这个解决办法已在上面提供的build.gradle文件中给出

 重新编译,成功!

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_21434809/article/details/120223545

智能推荐

冯诺依曼、哈佛、RISC、CISC_冯诺依曼曼指令和数据区分-程序员宅基地

文章浏览阅读1.3w次,点赞5次,收藏20次。几个基础材料(源:http://jwc.seu.edu.cn/zq/signal/new/importent/zhang5_6/feng.htmhttp://jwc.seu.edu.cn/zq/signal/new/importent/zhang5_6/harvard.htmhttp://dingjun.net/dingjun/html/14/84FB43402106C575.html_冯诺依曼曼指令和数据区分

Netty——深入解析心跳检测机制_netty 服务端心跳检测-程序员宅基地

文章浏览阅读428次。客户端定时每X秒(推荐小于60秒)向服务端发送特定数据(任意数据都可),服务端设定为X秒没有收到客户端心跳则认为客户端掉线,并关闭连接触发onClose回调。当需要服务端定时给客户端发送心跳数据时, $gateway->pingData设置为服务端要发送的心跳请求数据,心跳数据是任意的,只要客户端能识别即可。当设置为服务端主动发送心跳时,如果客户端最近有发来数据,那么证明客户端存活,服务端会省略一个心跳,下个心跳大约1.5*$gateway->pingInterval秒后发送。心跳检测时间间隔 单位:秒。_netty 服务端心跳检测

linux内核SPI总线驱动分析(一)_linux spi驱动总线分析-程序员宅基地

文章浏览阅读535次。下面有两个大的模块:一个是SPI总线驱动的分析 (研究了具体实现的过程)另一个是SPI总线驱动的编写(不用研究具体的实现过程)SPI总线驱动分析 1 SPI概述 SPI是英语Serial Peripheral interface的缩写,顾名思义就是串行外围设备接口,是Motorola首先在其MC68HCXX系列处理器上定义的。SPI接口主要应_linux spi驱动总线分析

Nginx,nginx-rtmp-module-master搭建直播平台-程序员宅基地

文章浏览阅读511次。Nginx,nginx-rtmp-module-master搭建直播平台_nginx-rtmp-module-master

**The sip module implements API v11.0 to v11.2 but the PyQt5.QtCore module requires API v11.3**_the pyqt5.qtcore module failed to register with th-程序员宅基地

文章浏览阅读3.6k次。*The sip module implements API v11.0 to v11.2 but the PyQt5.QtCore module requires API v11.3*情况一 SIP版本不匹配我在使用PyQt5时遇到了这个问题:“sip模块实现了API v11.0到v11.2,但PyQt5.QtWidgets模块需要API v11.3”pip列表sip 4.18,但是使..._the pyqt5.qtcore module failed to register with the sip module

Lesson1强化学习(RL)初印象 学习笔记_rl编程-程序员宅基地

文章浏览阅读467次。Lesson1强化学习(RL)初印象_rl编程

随便推点

QEMU 运行 RISC-V 64 位 Linux_qemu启动riscv环境-程序员宅基地

文章浏览阅读420次。QEMU 运行 RISC-V 64 位 Linux_qemu启动riscv环境

一年对于程序员来说有多长?_程序员 一年多少秒-程序员宅基地

文章浏览阅读4.3k次。聊这个话题,我表示又蛋疼了!你想看么?还没写过这么有争议的话题,有点小小的不安!一年有多长?让我来掐指算一算:1年=365天(今年是366天)=多少小时呢?前方高能,等我去写个程序算一下!还用写程序吗?早有人写好了!有多少分有多少秒,又有多少毫秒呢?我们只需要选择一下单位就好了,结果是:1年(yr)=31536000000毫秒(ms)为什么聊这个话题?蛋当然不可能说疼就疼了!水是有源的,树是有根的_程序员 一年多少秒

idea使用码云管理项目教程_idea 使用码云维护项目-程序员宅基地

文章浏览阅读745次。第一步 : 下载git 进入git官网 https://git-scm.com/download/win 下载git软件. 我这里演示安装Git-2.16.1.4-64-bit.exe 1 . 运行 Git-2.16.1.4-64-bit.exe 点击Next 2. 选择安装目录 点击Next 3. 第二步 : 安装码云插件第三步 : 配..._idea 使用码云维护项目

js调用百度地图API创建地图,搜索位置-程序员宅基地

文章浏览阅读186次。实现代码:<!DOCTYPE html><html><head> <meta charset="UTF-8"> <meta http-equiv="Cache-Control" content="no-store" /> <meta http-equi_百度地图js api地址搜索

Excel VSTO开发1-VSTO简介_office开发版本号与vsto-程序员宅基地

文章浏览阅读986次。VSTO(Visual Studio Tools for Office)是微软开发的一种用于创建Microsoft Office应用程序的工具集,它可以让开发者在Microsoft Office应用程序中集成自己的.NET应用程序。VSTO还提供了一些特殊的工具和库,使得开发人员可以更方便地管理Microsoft Office应用程序的生命周期、访问Microsoft Office应用程序的API和对象模型、处理Microsoft Office应用程序的事件和异常等。Office版本:2016 32位。_office开发版本号与vsto

3 cmake-生成dll和lib_cmake生成dll和lib文件-程序员宅基地

文章浏览阅读8.9k次。1 工程目录最顶层的CMakeList.txt添加add_subdirectory (CMakeLibDemo)add_subdirectory (CMakeLibDemoUse)2 文件ALU.h#pragma once#define DllExport __declspec( dllexport )//宏定义#ifndef ALU_H #define ALU_H #include <iostream> using namespace std;class Dll_cmake生成dll和lib文件