简介

前两天刚好更新了4.10, 这里以4.10作为示例进行编译, Qt版本是Qt6.6.2。

准备OpenCV的Android库

一. 使用官方编译好的库

1. 下载OpenCV android SDK

opencv-4.10.0-android-sdk.zip

2. 解压缩

官方提供的包含了多个架构的opencv android库
在这里插入图片描述

二. 自行编译

注意:
本篇只编译下载的源码, 并没有更新扩展, 有需要的话自行下载并按照如下方式设置编译: 添加OpenCV扩展模块:
1) 下载最新扩展 opencv_contrib
2) 在cmake-gui中添加值, 如下图
OPENCV_EXTRA_MODULES_PATH-》PATH-》<opencv_contrib>/modules
在这里插入图片描述

1. 下载OpenCV

opencv-4.10.0-windows.exe
下载完毕之后运行, 程序将源码解压缩到本地你自己设定的路径。

2. 编译

参考 官方OpenCV-Android-SDK编译

编译前准备
Qt for Android 编译套件使用的NDK是25.1.x, 所以后面NDK版本选择由此而来
在这里插入图片描述

1)打开终端 cmd.exe
2) 设置将编译器环境添加到环境变量
注: 这样添加只对打开的这个cmd.exe生效
SET PATH=%PATH%;D:\Softwares\Paths\msys64\mingw64\bin
此环境包含mingw32-make.exe, gcc, g++, python3, cmake-gui等编译所需

  1. 设置Android环境变量
    SET OPENCV_SRC_FOLDER=E:\test\opencv\sources
    SET CONTRIB_SRC_FOLDER=E:\test\opencv_contrib-4.x
    SET OPENCV_BUILD_FOLDER=E:\test\opencv\sources\build
    SET ANDROID_SDK=D:\Softwares\Paths\Android\android-sdk
    SET ANDROID_NDK_HOME=D:\Softwares\Paths\Android\android-sdk\ndk\25.1.8937393

  2. 执行编译指令

ndk-25.config.py 此次ndk是25版本, 所以选择这个文件执行 no_samples_build 不需要编译示例
–extra_modules_path %CONTRIB_SRC_FOLDER%/modules # 如果不需要更新扩展模块, 去掉这部分
执行:
D:\Softwares\Paths\msys64\mingw64\bin\python3
%OPENCV_SRC_FOLDER%/platforms/android/build_sdk.py
%OPENCV_BUILD_FOLDER% %OPENCV_SRC_FOLDER% --ndk_path
%ANDROID_NDK_HOME% --sdk_path %ANDROID_SDK% --extra_modules_path
%CONTRIB_SRC_FOLDER%/modules --no_samples_build --config
%OPENCV_SRC_FOLDER%/platforms/android/ndk-25.config.py

  1. 等待编译完毕
    编译画面截图, 编译中的错误解决参考下文“编译中遇到的错误”, 编译之后安装路径如下图所示:
    在这里插入图片描述
    编译后目录支持架构:
    在这里插入图片描述

三. 编译中遇到的错误

  1. gradle-7.5.1 下载失败
    在这里插入图片描述
    解决, 手动下载, 并放到C:\Users\Administrator.gradle\wrapper\dists\gradle-7.5.1-all\1ehga6e77gqps5uk2kc5kf1vc 目录下
    https://services.gradle.org/distributions/gradle-7.5.1-all.zip

提示: ndk-25.config.py文件中有需要的gradle版本,可以提前自己看下环境是否包含

四. 查看编译参数

使用cmake-gui直观的查看官方配置文件生成的编译参数,如下图:
在这里插入图片描述

Qt for Android项目导入OpenCV

Note : .pro或.pri不要写中文备注,容易出现莫名错误
.pro 中添加如下内容

android {
    # OPENCV_ANDROID=E:/test/opencv/sources/build/OpenCV-android-sdk
    OPENCV_ANDROID=E:/OpenCV-android-sdk

    # ANDROID_TARGET_ABIS = armeabi-v7a arm64-v8a x86 x86_64
    # for(a, ANDROID_TARGET_ABIS) {
    #     android_add_jni_libs.path = $$PWD/libs/$$a
    #     android_add_jni_libs.sources = $$a/libopencv_java4.so
    # }

    contains(ANDROID_TARGET_ARCH, armeabi-v7a) {
        isEmpty(OPENCV_ANDROID) {
            error("Let OPENCV_ANDROID point to the opencv-android-sdk, recommended: v4.0")
        }
        INCLUDEPATH += $$OPENCV_ANDROID/sdk/native/jni/include
        #OPENCV3RDPARTYLIBS = $$OPENCV_ANDROID/sdk/native/3rdparty/libs/armeabi-v7a
        OPENCVNATIVESTATICLIBS = $$OPENCV_ANDROID/sdk/native/staticlibs/armeabi-v7a
        OPENCVNATIVELIBS = $$OPENCV_ANDROID/sdk/native/libs/armeabi-v7a
        OPENCV3RDPARTYLIBS=$$OPENCV_ANDROID/sdk/native/3rdparty/libs/armeabi-v7a

        android_add_jni_libs.path = $$OPENCVNATIVELIBS
        android_add_jni_libs.sources = $$OPENCVNATIVELIBS/libopencv_java4.so

        LIBS += \
            $$OPENCVNATIVESTATICLIBS/libopencv_features2d.a \
            $$OPENCVNATIVESTATICLIBS/libopencv_flann.a \
            $$OPENCVNATIVESTATICLIBS/libopencv_gapi.a \
            $$OPENCVNATIVESTATICLIBS/libopencv_highgui.a \
            $$OPENCVNATIVESTATICLIBS/libopencv_imgcodecs.a \
            $$OPENCVNATIVESTATICLIBS/libopencv_imgproc.a \
            $$OPENCVNATIVESTATICLIBS/libopencv_ml.a \
            $$OPENCVNATIVESTATICLIBS/libopencv_objdetect.a \
            $$OPENCVNATIVESTATICLIBS/libopencv_photo.a \
            $$OPENCVNATIVESTATICLIBS/libopencv_stitching.a \
            $$OPENCVNATIVESTATICLIBS/libopencv_video.a \
            $$OPENCVNATIVESTATICLIBS/libopencv_videoio.a \
            $$OPENCVNATIVESTATICLIBS/libopencv_calib3d.a \
            $$OPENCVNATIVESTATICLIBS/libopencv_core.a \
            $$OPENCVNATIVESTATICLIBS/libopencv_dnn.a


        LIBS += \
            $$OPENCV3RDPARTYLIBS/libtegra_hal.a \
            $$OPENCV3RDPARTYLIBS/libade.a \
            $$OPENCV3RDPARTYLIBS/libcpufeatures.a \
            $$OPENCV3RDPARTYLIBS/libIlmImf.a \
            $$OPENCV3RDPARTYLIBS/libittnotify.a \
            $$OPENCV3RDPARTYLIBS/liblibjpeg-turbo.a \
            $$OPENCV3RDPARTYLIBS/liblibopenjp2.a \
            $$OPENCV3RDPARTYLIBS/liblibpng.a \
            $$OPENCV3RDPARTYLIBS/liblibprotobuf.a \
            $$OPENCV3RDPARTYLIBS/liblibtiff.a \
            $$OPENCV3RDPARTYLIBS/liblibwebp.a \
            $$OPENCV3RDPARTYLIBS/libtbb.a
    } else {
            error("Unsupported architecture: $$ANDROID_TARGET_ARCH")
    }
}

其它

OpenCV 4.10官方文档
Custom-OpenCV-Android-SDK-and-AAR-package-build
开源Android投屏软件scrcpy,需要开启USB调试
OpenCV for android编译, 需要梯子
OpenCV for android编译, 需要梯子

Logo

欢迎加入西安开发者社区!我们致力于为西安地区的开发者提供学习、合作和成长的机会。参与我们的活动,与专家分享最新技术趋势,解决挑战,探索创新。加入我们,共同打造技术社区!

更多推荐