QT for Android+OpenCV的配置详细图解

参考:https://blog.csdn.net/u012230798/article/details/86620400
参考:https://blog.csdn.net/bootleader/article/details/80875602

环境:Qt5.5.1+MinGW+Windows8.1
目的:想用Qt实现拍照后将图片转换成灰度图,所以进行相关的配置

第一步:准备工作

QT下载:http://download.qt.io/archive/qt/ QT下关于android的配置这里不再赘述,网上超多资料。
opencv下载:https://opencv.org/releases/
下载的最新的一版opencv-4.1.0 Android版,下载后是.zip格式,解压即安装,路径自己选择,注意不要包含中文与空格。
QT for Android+OpenCV的配置详细图解_第1张图片
目录打开如下QT for Android+OpenCV的配置详细图解_第2张图片
都准备好之后就可以开始配置了。

第二步:在QT项目中添加库

在项目下的.pro中添加如下代码,把静态链接添加到项目中。

unix {
ANDROID_OPENCV = F:/OpenCV-android-sdk/sdk/native

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

LIBS += $$ANDROID_OPENCV/staticlibs/armeabi-v7a/libopencv_calib3d.a \
        $$ANDROID_OPENCV/staticlibs/armeabi-v7a/libopencv_core.a \
        $$ANDROID_OPENCV/staticlibs/armeabi-v7a/libopencv_dnn.a \
        $$ANDROID_OPENCV/staticlibs/armeabi-v7a/libopencv_features2d.a \
        $$ANDROID_OPENCV/staticlibs/armeabi-v7a/libopencv_flann.a \
        $$ANDROID_OPENCV/staticlibs/armeabi-v7a/libopencv_highgui.a \
        $$ANDROID_OPENCV/staticlibs/armeabi-v7a/libopencv_imgcodecs.a \
        $$ANDROID_OPENCV/staticlibs/armeabi-v7a/libopencv_imgproc.a \
        $$ANDROID_OPENCV/staticlibs/armeabi-v7a/libopencv_ml.a \
        $$ANDROID_OPENCV/staticlibs/armeabi-v7a/libopencv_objdetect.a \
        $$ANDROID_OPENCV/staticlibs/armeabi-v7a/libopencv_photo.a \
        $$ANDROID_OPENCV/staticlibs/armeabi-v7a/libopencv_stitching.a \
        $$ANDROID_OPENCV/staticlibs/armeabi-v7a/libopencv_video.a \
        $$ANDROID_OPENCV/staticlibs/armeabi-v7a/libopencv_videoio.a \
        $$ANDROID_OPENCV/3rdparty/libs/armeabi-v7a/libcpufeatures.a \
        $$ANDROID_OPENCV/3rdparty/libs/armeabi-v7a/libIlmImf.a \
        $$ANDROID_OPENCV/3rdparty/libs/armeabi-v7a/liblibjasper.a \
        $$ANDROID_OPENCV/3rdparty/libs/armeabi-v7a/liblibjpeg-turbo.a \
        $$ANDROID_OPENCV/3rdparty/libs/armeabi-v7a/liblibpng.a \
        $$ANDROID_OPENCV/3rdparty/libs/armeabi-v7a/liblibprotobuf.a \
        $$ANDROID_OPENCV/3rdparty/libs/armeabi-v7a/liblibtiff.a \
        $$ANDROID_OPENCV/3rdparty/libs/armeabi-v7a/liblibwebp.a \
        $$ANDROID_OPENCV/3rdparty/libs/armeabi-v7a/libquirc.a \
        $$ANDROID_OPENCV/3rdparty/libs/armeabi-v7a/libtbb.a \
        $$ANDROID_OPENCV/3rdparty/libs/armeabi-v7a/libtegra_hal.a \
        $$ANDROID_OPENCV/libs/armeabi-v7a/libopencv_java4.so
}

在电脑系统环境变量中添加对应的路径。(会加的可以略过)
右键我的电脑→属性→高级系统设置QT for Android+OpenCV的配置详细图解_第3张图片
→环境变量→系统变量下的Path→编辑
QT for Android+OpenCV的配置详细图解_第4张图片
在变量中添加如下路径,以英文分号间隔,路径要与你电脑上的路径 对应,不要单纯的复制粘贴。

F:\OpenCV-android-sdk\sdk\native\libs\armeabi-v7a;F:\OpenCV-android-sdk\sdk\native\jni;F:\OpenCV-android-sdk\sdk\native\jni\include\opencv2;F:\OpenCV-android-sdk\sdk\native\jni\abi-armeabi-v7a;F:\OpenCV-android-sdk\sdk\native\3rdparty\libs\armeabi-v7a;F:\OpenCV-android-sdk\sdk\native\staticlibs\armeabi-v7a;F:\OpenCV-android-sdk\sdk\native\libs\armeabi-v7a

第三步:添加动态链接库进APK

最后一个库libopencv_java4.so是动态库,要加入到安卓的apk里。
对应下面的图,项目→构建→Build Android APK下的详细→Additional Libraries右侧的Add…选择libopencv_java4.so的路径
QT for Android+OpenCV的配置详细图解_第5张图片
加入路径后,项目会自动构建一遍,在.pro 中会出现如下代码

contains(ANDROID_TARGET_ARCH,armeabi-v7a) {
    ANDROID_EXTRA_LIBS = \
        $$PWD/F:/OpenCV-android-sdk/sdk/native/libs/armeabi-v7a/libopencv_java4.so

此时构建是会报错的,原因是找不到该文件,问题就在PWD上面

$$PWD代表项目所在根目录

这是自动生成的,具体原因我不知道,有了解的麻烦不吝赐教。
然后我将该代码段修改成下面的样子就可以找到文件了,项目就可以构建成功了,

注意$$后面有个空格

contains(ANDROID_TARGET_ARCH,armeabi-v7a) {
    ANDROID_EXTRA_LIBS = \
        $$ F:/OpenCV-android-sdk/sdk/native/libs/armeabi-v7a/libopencv_java4.so

你可能感兴趣的:(自我总结记录)