OpenCV for Android 通过 Visual Studio 到 Unity

OpenCV for Android via Visual Studio to Unity

我尝试使用 Visual Studio 2019 和 OpenCV Android 编译一个 .so 库,以便在 Unity 中使用这个库。 关于如何配置 Visual Studio 以使用 OpenCV Android ( or here) 有一些答案,但其中 none 对我有用。下面你可以看到我的配置。

Visual Studio 2019(运行 在 Windows 10)

android-ndk-r21e // 也尝试使用 android-ndk-r15c android-ndk-r16b 和 android-ndk-r17c

OpenCV Android 4.5.2 // 也尝试使用 OpenCV Android 4.0.0、4.0.1 和 4.1.0

我在 Visual Studio 2019 年的设置如下:

配置属性

- 一般

C/C++

- 一般

链接器

- 一般

- 输入

我的Source.cpp我尝试编译的只是一个用于测试目的的函数

#include <opencv2/core.hpp>
extern "C" float test(float a, float b)
{float c = a * b;   return c;}

这给了我以下错误:

E0035 #error directive: This constructor has not been ported to this platform
E0020 identifier "__fp16" is undefined
use of undeclared identifier 'ANDROID_LOG_INFO'

当我在抛出此错误的文件顶部添加 #include "android/log.h" 时,可以修复 ANDROID_LOG_INFO 错误。但是其他两个错误仍然存​​在。

我遇到了与您完全相同的问题(尽管我使用的是 C++ 11),但设置完全相同,并且苦苦挣扎了好几天。我相信您看到的错误(像我一样)来自 arm_neon.h。非常奇怪的是,我能够成功地构建(不是 运行).so,即使有这些错误(我说“错误”是因为如果你看 arm_neon.h,其他人会弹出),所以试试它。也许这是某种 IntelliJ/Intellisense 错误,它从其他工具链设置中捕获了假阴性。

同时,我不是 100% 确定我总是能够解决这个问题,所以如果不能,请尝试以下步骤:

  1. 将 OpenCV 4.0.1 与 Android NDK 16rb 一起使用。当涉及到 OpenCV 构建时,NDK 很重要,这是我所知道的唯一假设匹配。
  2. 从头开始学习本教程:https://amin-ahmadi.com/2019/06/03/how-to-use-opencv-in-unity-for-android/
  3. 如果下载的 OpenCV android SDK 仍然存在问题,请使用此处的其他教程从源代码构建 OpenCV:https://amin-ahmadi.com/2019/02/03/how-to-build-opencv-4-x-for-native-android-development/ 然后重复步骤 2。

主要编辑: OpenCV 4.5.2 需要区别对待,因为它不再使用带有 gnu c++ 的工具链。 - 当您从 CMake 构建 OpenCV 时,使用 Android NDK 21e 构建,并且不要使用 OpenCV 4.5.2 中的工具链。使用 Android NDK 构建文件夹中的那个 (android-ndk-r21e\build\cmake)。 -当你从 Visual Studio 2019 构建你的 .so 时,不要使用 GNU STL,使用 LLVM。 GNU c++ 不再是 Android NDK 的一部分,您需要将其完全从流程中删除。 - 在链接器输入中,将您的库文件(或文件,如果它只是世界文件)的名称放在库依赖项字段中,而不是附加依赖项字段中。 -其他都和那些普通教程一样。