Qt+openCV学习笔记(八)openCV4.5.4+android如何使用动、静态库

前言:

在Android下使用openCV的库,与在其他系统下的原理基本相同,但又有点差异。本次笔记记录下具体的使用方法,供小伙伴们参考

一、使用官方的静态库

静态库使用时,要注意,若库与库之前有相应的依赖,加载的先后顺序不变,若是无法预知需要使用哪些库,可以按照如下的方法测试需要哪些库

1.先放入$$ANDROID_OPENCV/libs/arm64-v8a/libopencv_java4.so在最下面

2.把第三方库放在前面

3.把$$ANDROID_OPENCV/staticlibs/arm64-v8a/libopencv_core.a放在最前面

4.直接编译程序,等到源码报错时,查看报错的函数是存放于哪个库中,并直接添加到最前面

5.不断重复第4步,直到加入所有需要的库

6.若确认可以正常编译,开如注释掉第三方库,找出程序运行不需要的静态库,最终得到最小的库配置

以arm64-v8a为例,若需要使用二维码识别,最终要加入pro文件的内容如下

unix:!macx{
#添加opencv库(android arm64-v8a版)
contains(ANDROID_TARGET_ARCH,arm64-v8a) {
    message("arm64-v8a")

ANDROID_OPENCV = opencv_dir/opencv4.5.4/OpenCV-android-sdk/sdk/native

    INCLUDEPATH += $$ANDROID_OPENCV/jni/include/opencv2 \
                   $$ANDROID_OPENCV/jni/include

    LIBS += \
        $$ANDROID_OPENCV/staticlibs/arm64-v8a/libopencv_objdetect.a \
        $$ANDROID_OPENCV/staticlibs/arm64-v8a/libopencv_imgproc.a \
        $$ANDROID_OPENCV/staticlibs/arm64-v8a/libopencv_core.a \
        $$ANDROID_OPENCV/3rdparty/libs/arm64-v8a/libcpufeatures.a \
        $$ANDROID_OPENCV/3rdparty/libs/arm64-v8a/libittnotify.a \
        $$ANDROID_OPENCV/3rdparty/libs/arm64-v8a/libquirc.a \
        $$ANDROID_OPENCV/3rdparty/libs/arm64-v8a/libtegra_hal.a \
        $$ANDROID_OPENCV/libs/arm64-v8a/libopencv_java4.so

}
#若不添加,打包时不加入
ANDROID_EXTRA_LIBS = opencv_dir/opencv4.5.4/OpenCV-android-sdk/sdk/native/libs/arm64-v8a/libopencv_java4.so
}

二、使用自行编译的静态库

 自行编译的静态库,同样需要注意有先后顺序。若库与库之前有相应的依赖,加载的先后顺序不变,若是无法预知需要使用哪些库,可以按照如下的方法测试需要哪些库

1.把第三方库放在前面

2.把$$ANDROID_OPENCV/staticlibs/arm64-v8a/libopencv_core.a放在最前面

3.直接编译程序,等到源码报错时,查看报错的函数是存放于哪个库中,并直接添加到最前面

4.不断重复第4步,直到加入所有需要的库

5.若确认可以正常编译,开如注释掉第三方库,找出程序运行不需要的静态库,最终得到最小的库配置

以arm64-v8a为例,若需要使用微信二维码识别,最终要加入pro文件的内容如下

unix:!macx{
contains(ANDROID_TARGET_ARCH,arm64-v8a) {
    message("arm64-v8a")
ANDROID_OPENCV = opencv_dir/opencv4.5.4/build_android_arm64-v8a/install/sdk/native

    INCLUDEPATH += $$ANDROID_OPENCV/jni/include/opencv2 \
                   $$ANDROID_OPENCV/jni/include


LIBS += \
    $$ANDROID_OPENCV/staticlibs/arm64-v8a/libopencv_wechat_qrcode.a \
    $$ANDROID_OPENCV/staticlibs/arm64-v8a/libopencv_dnn.a \
    $$ANDROID_OPENCV/staticlibs/arm64-v8a/libopencv_imgproc.a \
    $$ANDROID_OPENCV/staticlibs/arm64-v8a/libopencv_calib3d.a \
    $$ANDROID_OPENCV/staticlibs/arm64-v8a/libopencv_flann.a \
    $$ANDROID_OPENCV/staticlibs/arm64-v8a/libopencv_features2d.a \
    $$ANDROID_OPENCV/staticlibs/arm64-v8a/libopencv_core.a \
    $$ANDROID_OPENCV/3rdparty/libs/arm64-v8a/libcpufeatures.a \
    $$ANDROID_OPENCV/3rdparty/libs/arm64-v8a/libittnotify.a \
    $$ANDROID_OPENCV/3rdparty/libs/arm64-v8a/libquirc.a \
    $$ANDROID_OPENCV/3rdparty/libs/arm64-v8a/libtegra_hal.a
}

三、使用自行编译的动态库

动态库无加载顺序,直接加入即可,但要注意加入ANDROID_EXTRA_LIBS中,否则打包时不会加入

直接编译程序,等到源码报错时,查看报错的函数是存放于哪个库中,并直接添加即可

以arm64-v8a为例,若需要使用微信二维码识别,最终要加入pro文件的内容如下

unix:!macx{
contains(ANDROID_TARGET_ARCH,arm64-v8a) {
    message("arm64-v8a")
ANDROID_OPENCV = opencv_dir/opencv4.5.4/build_android_arm64-v8a/install/sdk/native

    INCLUDEPATH += $$ANDROID_OPENCV/jni/include/opencv2 \
                   $$ANDROID_OPENCV/jni/include


LIBS += \
    $$ANDROID_OPENCV/libs/arm64-v8a/libopencv_wechat_qrcode.so \
    $$ANDROID_OPENCV/libs/arm64-v8a/libopencv_dnn.so \
    $$ANDROID_OPENCV/libs/arm64-v8a/libopencv_imgproc.so \
    $$ANDROID_OPENCV/libs/arm64-v8a/libopencv_calib3d.so \
    $$ANDROID_OPENCV/libs/arm64-v8a/libopencv_flann.so \
    $$ANDROID_OPENCV/libs/arm64-v8a/libopencv_features2d.so \
    $$ANDROID_OPENCV/libs/arm64-v8a/libopencv_core.so

ANDROID_EXTRA_LIBS = \
    opencv_dir/opencv4.5.4/build_android_arm64-v8a/install/sdk/native/libs/arm64-v8a/libopencv_core.so \
    opencv_dir/opencv4.5.4/build_android_arm64-v8a/install/sdk/native/libs/arm64-v8a/libopencv_features2d.so \
    opencv_dir/opencv4.5.4/build_android_arm64-v8a/install/sdk/native/libs/arm64-v8a/libopencv_flann.so \
    opencv_dir/opencv4.5.4/build_android_arm64-v8a/install/sdk/native/libs/arm64-v8a/libopencv_calib3d.so \
    opencv_dir/opencv4.5.4/build_android_arm64-v8a/install/sdk/native/libs/arm64-v8a/libopencv_imgproc.so \
    opencv_dir/opencv4.5.4/build_android_arm64-v8a/install/sdk/native/libs/arm64-v8a/libopencv_dnn.so \
    opencv_dir/opencv4.5.4/build_android_arm64-v8a/install/sdk/native/libs/arm64-v8a/libopencv_wechat_qrcode.so

}

后记:

在编译时,不勾选world,各个库要分散成多个文件,虽然可以减少打包后的程序体积,但不利于初学者学习

建议初学者,在编译时勾选world,以降低学习难度

你可能感兴趣的:(Qt,for,android,Qt+openCV,opencv,qt,android)