如何将OpenCV库添加到AS3.1.2和NDK 17.0 [英] How to add OpenCV lib to AS3.1.2 and NDK 17.0

查看:145
本文介绍了如何将OpenCV库添加到AS3.1.2和NDK 17.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Android Studio 3.1.2中使用OpenCV.我在2018年5月20日拥有此IDE和NDK的最新版本(17.0.4754217).

I need to use OpenCV in Android Studio 3.1.2. I have the last version at 05/20/2018 of this IDE and the NDK (17.0.4754217).

我阅读了这篇文章谈论它,并说出有关NDK版本16的问题,但事实并非如此.

I read this post talking about it, and says something about a problem with the NDK version 16, but is not the case.

大多数指南都是针对较旧版本的Android的(我看到了有关Eclipse IDE进行Android开发的指南),但没有一个是最新版本的,也没有一个是最接近的,这是一个问题,因为最新版本的AS会导致很多旧项目或其制作方式存在错误,因此这些指南已过时.

Most of the guides are for older versions of Android (I saw guides talking about Eclipse IDE for Android development) but no one with the last version or one closest, and it is a problem, since the latest version of AS causes many errors with older projects or the way in which they are made, so they make these guides obsolete.

也许是由于我不知道如何使用NDK和CMake造成的问题,所以如果someoanewho知道如何或曾经尝试这样做,我将不胜感激,告诉我如何将OpenCV添加到Android 3.1.2,也许这篇文章可能会对其他用户使用NDK和AS的将来版本有所帮助.

Maybe is a problem caused by the fact that i don't kown how to work with the NDK and CMake, so i would appreciate if someoanewho knows how or had tried to do this, would tell me how to add OpenCV to Android 3.1.2, and maybe this post can ben helpful to another users for future versions of NDK and AS.

TY那么多.

推荐答案

1..请确保您具有最新的Android SDK,并已安装NDK

1. Make sure you have Android SDK up to date, with NDK installed

2..从OpenCV.org下载最新的Android版OpenCV SDK并解压缩zip文件.

2. Download latest OpenCV SDK for Android from OpenCV.org and decompress the zip file.

3..创建一个新的Android Studio项目

3. Create a new Android Studio project

  • 选中包括C ++支持"
  • 选择空活动
  • 在C ++支持中,您可以检查-fexceptions和-frtti
  • Check Include C++ Support
  • Choose empty Activity
  • In C++ Support, you can check -fexceptions and -frtti

4..导入OpenCV库模块

4. Import OpenCV library module

  • 新建->导入模块
  • 选择YOUR_OPENCV_SDK/sdk/java文件夹
  • 解开替换jar,解开替换lib,解开创建gradle样式
  • New -> Import Module
  • Choose the YOUR_OPENCV_SDK/sdk/java folder
  • Unckeck replace jar, unckeck replace lib, unckeck create gradle-style

5..设置OpenCV库模块以适合您的SDK

5. Set the OpenCV library module up to fit your SDK

app/build.gradle

opencv/build.gradle

编辑openCVLibrary/build.gradle以匹配您的app/build.gradle,例如:

Edit openCVLibrary/build.gradle to match your app/build.gradle e.g:

compileSdkVersion 27
defaultConfig {
minSdkVersion 19
targetSdkVersion 27
}

6..在您的应用模块中添加OpenCV模块依赖项

6. Add OpenCV module dependency in your app module

文件->项目结构->模块应用->依赖项选项卡->新模块依赖项->选择OpenCV库模块

7..通过右键单击 app/src/main 并单击更改文件夹位置,创建 jni 文件夹>在该文件夹后将文件夹从 jni 重命名为 jniLibs

7. Make a jni folder by right clicking on app/src/main and click Change Folder Location box after that rename folder fron jni to jniLibs

  • 第一步

  • 1st step

第二步

第三步

8.复制您已下载的opencv目录中的所有文件 YOUR_OPENCV_SDK/sdk/native/libs ,并将其粘贴到 jniLibs 文件夹

8. Copy all files from your opencv directory YOUR_OPENCV_SDK/sdk/native/libs that you have downloaded and paste them in jniLibs folder

  • 第一步

  • 1st step

第二步

9..设置应用程序build.gradle

9. Set the app build.gradle

  • 添加abiFilters

  • Add abiFilters

externalNativeBuild {
cmake {
    cppFlags "-frtti -fexceptions"
    abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
   }
}

10..配置CMakeLists.txt文件

10. Configure the CMakeLists.txt file

  • 复制这三行并粘贴到 cmake_minimum_required

include_directories(YOUR_OPENCV_SDK/sdk/native/jni/include)
add_library( lib_opencv SHARED IMPORTED )
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so)

转到 CMakeLists.txt 的末尾,并将 lib_opencv 写入 target_link_libraries 列表

Go to the end of CMakeLists.txt and write lib_opencv to the target_link_libraries list

所有操作都已完成,现在可以使用opencv ...

All is done now enjoy coding with opencv...

这篇关于如何将OpenCV库添加到AS3.1.2和NDK 17.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆