目录
['qrcodeInfo.decodes' is possibly 'undefined'.](#'qrcodeInfo.decodes' is possibly 'undefined'.)
[warning: backslash and newline separated by space [-Wbackslash-newline-escape]](#warning: backslash and newline separated by space [-Wbackslash-newline-escape])
引言
在前面的博文(【鸿睿创智开发板试用】移植OpenCV 4到OpenHarmony 4.1-CSDN博客)中,我已经介绍了如何将OpenCV库移植到RK3568开发板上。今天来测试一下OpenCV示例:samples/ohos_samples · OpenHarmony-SIG/third_party_opencv - 码云 - 开源中国。原来的工程是针对API 9的,这个版本的工程在DevEco Studio 4.1下会有一些问题,所以需要做一些修改。
修改工程环境
原来的工程是针对API 9的,我们需要先将其改为API 11。API 9和API 10以上版本,工程级build-profile.json5文件中compileSdkVersion 和compatibleSdkVersion 字段的位置是不一样的。需要将这两个字段从app下迁移到当前选中的product中。
在我的环境中,如果不升级到API 11,DevEco Studio总是尝试安装老版本的ArkTs,而每次都失败,所以必须升级。
配置OpenCV库和头文件
除了前面介绍的方法生成OpenCV的库文件,还有个办法就是直接下载官方准备好的库文件。现在的地址是:opencv-4.5.5-ndk · OpenHarmony-SIG/third_party_opencv - Gitee.com
下载opencv-4.5.5-ndk.zip 这个文件即可。
解压这个文件后将其中的头文件复制到 entry/src/main/cpp/include,将其中的库文件需要复制到 entry/libs 目录下。
修改entry/build-profile.json5文件中的abiFilters为"arm64-v8a"。
{
"apiType": "stageMode",
"buildOption": {
"externalNativeOptions": {
"path": "./src/main/cpp/CMakeLists.txt",
"arguments": "",
"abiFilters": [ //用于设置本机的ABI编译环境
//"armeabi-v7a"
"arm64-v8a"
],
entry/libs 目录下的库文件有"armeabi-v7a"、"arm64-v8a"和"x86_64"三个版本,对于我们的开发板,保留"arm64-v8a"这个目录就行,其他两个用不到,除非你打算制作一个在所有OpenHarmony平台上都能工作的版本。
修改代码
编译的过程中,还发现一些问题,大多数是因为OpenHarmony的新版本对语法要求更严了,所以需要做一些小修改。
CMakeList.txt文件
entry\src\main\cpp\CMakeList.txt这个文件有点小问题,修改如下:
# the minimum version of CMake.
cmake_minimum_required(VERSION 3.4.1)
project(opencv_sample)
set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR})
set(OPENCV_LIB_PATH ${NATIVERENDER_ROOT_PATH}/../../../libs/${OHOS_ARCH})
link_directories(${OPENCV_LIB_PATH})
#add_subdirectory(aki)
set(CMAKE_BUILD_WITH_INSTALL_RPATH On)
# opencv test, perf_test, examples, docs不需要编译,开启opencl并行运行
set(BUILD_TESTS OFF)
set(BUILD_PERF_TESTS OFF)
set(BUILD_EXAMPLES OFF)
set(BUILD_DOCS OFF)
set(WITH_OPENCL ON)
# 设置编译类型为Release
set(CMAKE_BUILD_TYPE RELEASE)
set(CMAKE_CXX_FLAGS_RELEASE -g0)
# 按需选择要编译的opencv module, 与target_link_libraries中的一致
set(BUILD_LIST core imgproc imgcodecs objdetect)
#add_subdirectory(opencv)
add_library(ohos_opencv_sample SHARED
common/init.cpp
common/common.cpp
image_basic/image2Gray.cpp
image_basic/QRCode_identify.cpp
image_basic/faceDetect.cpp
image_basic/edgeDetection.cpp
)
#debug日志编译宏
#add_definitions(-DOHOS_OPENCV_SAMPLE_DEBUG)
target_include_directories(ohos_opencv_sample PUBLIC
include
opencv/include
opencv/includecv4
opencv/modules/core/include
opencv/modules/calib3d/include
opencv/modules/features2d/include
opencv/modules/dnn/include
opencv/modules/flann/include
opencv/modules/highgui/include
opencv/modules/imgcodecs/include
opencv/modules/imgproc/include
opencv/modules/ml/include
opencv/modules/objdetect/include
opencv/modules/photo/include
opencv/modules/stitching/include
opencv/modules/video/include
opencv/modules/videoio/include
opencv/build)
target_link_libraries(ohos_opencv_sample PUBLIC
libace_napi.z.so
librawfile.z.so
libhilog_ndk.z.so
opencv_core
opencv_imgcodecs
opencv_imgproc
opencv_objdetect)
'qrcodeInfo.decodes' is possibly 'undefined'.
G:\DevEcoStudioProjects\opencv_app_sample\entry\src\main\ets\pages\image\basicSample\qrcode.ets:76
ERROR: ArkTS:ERROR File: G:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/ets/pages/image/basicSample/qrcode.ets:76:40
'qrcodeInfo.decodes' is possibly 'undefined'.
这个错误因为ArkTS怀疑访问一个可能是undefined的对象属性或方法。将代码修改如下:
if(qrcodeInfo.decodes != undefined)
{
this.qrcodeStr = qrcodeInfo.decodes[0];
}
warning: backslash and newline separated by space [-Wbackslash-newline-escape]
G:\HUAWEI\OpenHarmony\Sdk\11\native\llvm\bin\clang++.exe --target=aarch64-linux-ohos --gcc-toolchain=G:/HUAWEI/OpenHarmony/Sdk/11/native/llvm --sysroot=G:/HUAWEI/OpenHarmony/Sdk/11/native/sysroot -Dohos_opencv_sample_EXPORTS -IG:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/include -IG:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/opencv/include -IG:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/opencv/includecv4 -IG:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/opencv/modules/core/include -IG:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/opencv/modules/calib3d/include -IG:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/opencv/modules/features2d/include -IG:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/opencv/modules/dnn/include -IG:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/opencv/modules/flann/include -IG:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/opencv/modules/highgui/include -IG:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/opencv/modules/imgcodecs/include -IG:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/opencv/modules/imgproc/include -IG:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/opencv/modules/ml/include -IG:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/opencv/modules/objdetect/include -IG:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/opencv/modules/photo/include -IG:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/opencv/modules/stitching/include -IG:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/opencv/modules/video/include -IG:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/opencv/modules/videoio/include -IG:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/opencv/build -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fno-addrsig -Wa,--noexecstack -Wformat -Werror=format-security -D__MUSL__ -g0 -fPIC -MD -MT CMakeFiles/ohos_opencv_sample.dir/image_basic/QRCode_identify.cpp.o -MF CMakeFiles\ohos_opencv_sample.dir\image_basic\QRCode_identify.cpp.o.d -o CMakeFiles/ohos_opencv_sample.dir/image_basic/QRCode_identify.cpp.o -c G:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/image_basic/QRCode_identify.cpp
clang++: warning: argument unused during compilation: '--gcc-toolchain=G:/HUAWEI/OpenHarmony/Sdk/11/native/llvm' [-Wunused-command-line-argument]
In file included from G:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/image_basic/QRCode_identify.cpp:17:
G:/DevEcoStudioProjects/opencv_app_sample/entry/src/main/cpp/include/common.h:85:47: warning: backslash and newline separated by space [-Wbackslash-newline-escape]
if ((theCall) != 0) { \
删除这行反斜杠后面的空格。
运行
编译成功后,就可以连上真机运行了。
总体效果还不错,OpenCV的人脸识别速度不错,但是二维码识别有点慢。
结语
完整的示例文件参见文章顶部的资源。至此,我们完成了OpenCV基本功能的测试,后面就可以再次基础上完成更有趣的项目了。