How to integrate OpenCV into Qt Creator Android project

后端 未结 3 727
谎友^
谎友^ 2020-11-27 05:21

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

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-27 06:23

    For OpenCV 4, sashoalm's approach did not work for me until I adapted it:

    1. Download the Android-Pack and unzip it somewhere. We'll create a qmake-variable OPENCV_ANDROID which points to that directory later.
    2. Add the following snippet to your *.pro-file:

      android {
          contains(ANDROID_TARGET_ARCH,arm64-v8a) {
              isEmpty(OPENCV_ANDROID) {
                  error("Let OPENCV_ANDROID point to the opencv-android-sdk, recommended: v4.0")
              }
              INCLUDEPATH += "$$OPENCV_ANDROID/sdk/native/jni/include"
              LIBS += \
                  -L"$$OPENCV_ANDROID/sdk/native/libs/arm64-v8a" \
                  -L"$$OPENCV_ANDROID/sdk/native/3rdparty/libs/arm64-v8a" \
                  -llibtiff \
                  -llibjpeg-turbo \
                  -llibjasper \
                  -llibpng \
                  -lIlmImf \
                  -ltbb \
                  -lopencv_java4 \
      
              ANDROID_EXTRA_LIBS = $$OPENCV_ANDROID/sdk/native/libs/arm64-v8a/libopencv_java4.so
          } else {
              error("Unsupported architecture: $$ANDROID_TARGET_ARCH")
          }
      }
      

      This will work for the arm64-v8a only. If you happen to build for another architecture (apparently 32-Bit is still the default for Qt@Android), you must change the .../libs/arm64-v8a part of the paths (occurs 3 times) and the same to match your actual target-architecture (the contains(...)-part in the second line of the snippet).

    3. Tell qmake where to find the SDK. Add the following to qmake-call: "OPENCV_ANDROID=/path/to/OpenCV-android-sdk".
      • e.g., this looks like qmake example.pro "OPENCV_ANDROID=/home/user/OpenCV-android-sdk" from command line.
      • when you use QtCreator, add "OPENCV_ANDROID=..." to the "Additional arguments"-field. You can find it after enabling the Project-Mode in the Build-section of the android-kit. Expand the qmake-field under Build Steps

提交回复
热议问题