gpt4 book ai didi

android - 如何将 OpenCV 集成到 Qt Creator Android 项目中

转载 作者:可可西里 更新时间:2023-11-01 09:38:04 25 4
gpt4 key购买 nike

我使用 Qt Creator 编译一个 Android 应用程序。我需要将 OpenCV 集成到其中,我花了半天时间才正确配置它,所以我想在这里记录下我采取的步骤,以防其他人必须这样做。

最佳答案

编辑:对于 OpenCV 4.x,请参阅下面的答案。我的答案仅在 OpenCV 2.4 上进行了测试。

原答案:


首先,我下载了OpenCV-2.4.10-android-sdk ,并放入我的项目目录。它包含静态库,and link order matters for static libraries for GCC .所以你需要这样订购它们。这就是我的 .pro 文件最后的样子($$_PRO_FILE_PWD_ 指的是项目目录):

INCLUDEPATH += "$$_PRO_FILE_PWD_/OpenCV-2.4.10-android-sdk/sdk/native/jni/include"
android {
LIBS += \
-L"$$_PRO_FILE_PWD_/OpenCV-2.4.10-android-sdk/sdk/native/3rdparty/libs/armeabi-v7a"\
-L"$$_PRO_FILE_PWD_/OpenCV-2.4.10-android-sdk/sdk/native/libs/armeabi-v7a"\
-llibtiff\
-llibjpeg\
-llibjasper\
-llibpng\
-lIlmImf\
-ltbb\
-lopencv_core\
-lopencv_androidcamera\
-lopencv_flann\
-lopencv_imgproc\
-lopencv_highgui\
-lopencv_features2d\
-lopencv_calib3d\
-lopencv_ml\
-lopencv_objdetect\
-lopencv_video\
-lopencv_contrib\
-lopencv_photo\
-lopencv_java\
-lopencv_legacy\
-lopencv_ocl\
-lopencv_stitching\
-lopencv_superres\
-lopencv_ts\
-lopencv_videostab

ANDROID_PACKAGE_SOURCE_DIR=$$_PRO_FILE_PWD_/android
}

之后该项目将编译但将无法运行并出现错误

E/AndroidRuntime(11873): java.lang.UnsatisfiedLinkError: Cannot load library: link_image[1891]:   176 could not load needed library 'libopencv_java.so' for 'libMyProject.so' (load_library[1093]: Library 'libopencv_java.so' not found)

要克服这个问题,您需要 add libopencv_java.so to your APK ,然后从 QtActivity.java 手动加载它。这就是末尾的 ANDROID_PACKAGE_SOURCE_DIR=$$_PRO_FILE_PWD_/android 行的用途。现在您需要将 libopencv_java.so 放在这里:

project_root/android/libs/armeabi-v7a/libopencv_java.so
project_root/android/src/org/qtproject/qt5/android/bindings/QtActivity.java

您可以从 Android 目标构建目录获取 QtActivity.java,在我的例子中完整路径是 c:\Workspace\build-MyProject-Android_for_armeabi_v7a_GCC_4_9_Qt_5_4_0-Debug\android-build\src\org\qtproject\qt5\android\bindings\QtActivity.java,复制即可。

然后你在里面找到那些行:

        // now load the application library so it's accessible from this class loader
if (libName != null)
System.loadLibrary(libName);

然后在它们之前加载libopencv_java.so,这样它们就变成了:

        // This is needed for OpenCV!!!
System.loadLibrary("opencv_java");

// now load the application library so it's accessible from this class loader
if (libName != null)
System.loadLibrary(libName);

请注意,您将 opencv_java 传递给 System.loadLibrary(),即使文件是 libopencv_java.so


编辑: 我忘了说了,但是当我尝试运行 OpenCV-2.4.10-android-sdk 附带的示例之一时,我已经在我的手机上安装了 OpenCV 管理器,所以我不知道有没有必要。无论如何,请记住,如果在我的步骤之后它仍然失败,您可能需要下载 OpenCV 管理器(它可以在 Google 商店中获得)。

编辑 2:我正在使用 adt-bundle-windows-x86-20140702、android-ndk-r10d、OpenCV-2.4.10-android-sdk、Qt Creator 3.3.0 和我的构建目标是“Android for armeabi-v7a (GCC 4.9, Qt 5.4.0)”。

编辑 3: 来自 Daniel Saner's comment :

In OpenCV 3.x, opencv_java has been renamed to opencv_java3. Also, while I didn't look into the specific changes that might have effected this, the workaround regarding that library in the final step seems to no longer be necessary. The app compiles and runs without the ANDROID_PACKAGE_SOURCE_DIR line

编辑4:@myk的评论:

Worked for me with OpenCV 3.2. To workaround the build issues with carotene finish the LIBS+ section with: -lopencv_videostab\ -ltegra_hal\ – myk 2 hours ago

关于android - 如何将 OpenCV 集成到 Qt Creator Android 项目中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18951005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com