如何将 OpenCV 集成到 Qt Creator Android 项目中 [英] How to integrate OpenCV into Qt Creator Android project

查看:34
本文介绍了如何将 OpenCV 集成到 Qt Creator Android 项目中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use Qt Creator to compile an Android application. I needed to integrate OpenCV into it, and it took me half a day to configure it properly, so I want to document the steps I took here, in case somebody else ever has to do it.

推荐答案

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

For OpenCV 4.x see the answers below. My answer was tested on OpenCV 2.4 only.

原答案:

首先,我下载了OpenCV-2.4.10-android-sdk,并放入我的项目目录.它包含静态库,以及 GCC 静态库的链接顺序问题.所以你需要这样订购它们.这就是我的 .pro 文件最后的样子($$_PRO_FILE_PWD_ 指的是项目目录):

First, I downloaded OpenCV-2.4.10-android-sdk, and put into my project directory. It contains static libraries, and link order matters for static libraries for GCC. So you need to order them just so. This is how my .pro file looked in the end ($$_PRO_FILE_PWD_ refers to the project directory):

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
}

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

After that the project will compile but it will fail to run with the error

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)

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

To overcome this, you need to add libopencv_java.so to your APK, and then manually load it from QtActivity.java. That's what the ANDROID_PACKAGE_SOURCE_DIR=$$_PRO_FILE_PWD_/android line at the end was for. Now you need to place libopencv_java.so here:

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

您可以从 Android 目标构建目录中获取 QtActivity.java,在我的情况下,完整路径是 c:Workspaceuild-MyProject-Android_for_armeabi_v7a_GCC_4_9_Qt_5_4_0-Debugandroid-buildsrcorgqtprojectqt5androidindingsQtActivity.java,然后复制它.

You can get QtActivity.java from the Android target build directory, in my case the full path was c:Workspaceuild-MyProject-Android_for_armeabi_v7a_GCC_4_9_Qt_5_4_0-Debugandroid-buildsrcorgqtprojectqt5androidindingsQtActivity.java, and just copy it.

然后您会在其中找到这些行:

Then you find those lines in it:

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

并在它们之前加载libopencv_java.so,使它们变成:

And load libopencv_java.so before them, so they become:

        // 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.

Note that you pass opencv_java to System.loadLibrary(), even though the file is libopencv_java.so.

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

I forgot to mention, but I already had installed OpenCV Manager on my phone when trying to run one of the samples that come with OpenCV-2.4.10-android-sdk, so I don't know if it's needed or not. In any event, keep it in mind, if it fail even after my steps, you might need to download OpenCV Manager (it's available on the Google Store).

编辑 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)".

Edit 2: I'm using adt-bundle-windows-x86-20140702, android-ndk-r10d, OpenCV-2.4.10-android-sdk, Qt Creator 3.3.0, and my build target is "Android for armeabi-v7a (GCC 4.9, Qt 5.4.0)".

编辑 3: 来自 丹尼尔·萨纳 (Daniel Saner) 的评论:

在 OpenCV 3.x 中,opencv_java 已重命名为 opencv_java3.此外,虽然我没有研究可能影响这一点的具体更改,但似乎不再需要在最后一步中关于该库的解决方法.应用程序在没有 ANDROID_PACKAGE_SOURCE_DIR 行的情况下编译和运行

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 的评论:

使用 OpenCV 3.2 为我工作.要解决胡萝卜素的构建问题,请使用以下命令完成 LIBS+ 部分:-lopencv_videostab -ltegra_hal – myk 2 小时前

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

这篇关于如何将 OpenCV 集成到 Qt Creator Android 项目中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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