diff --git a/napi/BUILD.gn b/napi/BUILD.gn index 85beb0f2cfb9bb789879bd9b842bfe89f91896f7..8c9ad1fdb7fc598c752f21abd145e57ddefee021 100644 --- a/napi/BUILD.gn +++ b/napi/BUILD.gn @@ -27,19 +27,19 @@ config("native_module_config") { include_dirs = [] defines = [] - if(OPENCV_NAPI_DEBUG_DEFINE) { + if(opencv_debug_enable) { defines += ["OPENCV_NAPI_DEBUG"] } - if(OPENCV_NAPI_WITH_CORE) { + if(opencv_napi_with_core) { defines += ["OPENCV_NAPI_SUPPORT_CORE"] } - if(OPENCV_NAPI_WITH_IMGPROC) { + if(opencv_napi_with_imgproc) { defines += ["OPENCV_NAPI_SUPPORT_IMGPROC"] } - if(OPENCV_NAPI_WITH_IMGCODECS) { + if(opencv_napi_with_imgcodecs) { defines += ["OPENCV_NAPI_SUPPORT_IMGCODECS"] } - if(OPENCV_NAPI_WITH_OBJDETECT) { + if(opencv_napi_with_objdetect) { defines += ["OPENCV_NAPI_SUPPORT_OBJDETECT"] } } @@ -50,17 +50,17 @@ ohos_shared_library("opencv_napi") { "//third_party/opencv/napi/js/napi/src/init.cpp", "//third_party/opencv/napi/js/napi/src/common.cpp", ] - if(OPENCV_NAPI_WITH_CORE){ + if(opencv_napi_with_core){ sources += napi_sources_core } - if(OPENCV_NAPI_WITH_IMGPROC){ + if(opencv_napi_with_imgproc){ sources += napi_sources_imgproc } - if(OPENCV_NAPI_WITH_IMGCODECS){ + if(opencv_napi_with_imgcodecs){ sources += napi_sources_imgcodecs } - if(OPENCV_NAPI_WITH_OBJDETECT){ + if(opencv_napi_with_objdetect){ sources += napi_sources_objdetect } @@ -95,14 +95,14 @@ ohos_shared_library("opencv_napi") { "//third_party/libuv:uv", ] - if(OPENCV_NAPI_WITH_IMGPROC){ + if(opencv_napi_with_imgproc){ deps += ["//third_party/opencv/modules/imgproc:libopencv_imgproc"] } - if(OPENCV_NAPI_WITH_IMGCODECS){ + if(opencv_napi_with_imgcodecs){ deps += ["//third_party/opencv/modules/imgcodecs:libopencv_imgcodecs"] } - if(OPENCV_NAPI_WITH_OBJDETECT){ + if(opencv_napi_with_objdetect){ deps += [ "//third_party/opencv/modules/objdetect:libopencv_objdetect", "//third_party/opencv/modules/dnn:libopencv_dnn", diff --git a/napi/js/napi/include/init.h b/napi/js/napi/include/init.h index 47aa1869c8f02a0d90952dec3eb1dc153d7f7322..528b5daadf4cd35aaa6e4b899d1e6acd45390b34 100644 --- a/napi/js/napi/include/init.h +++ b/napi/js/napi/include/init.h @@ -31,7 +31,7 @@ #include "n_imgproc.h" #endif -#if defined OPENCV_NAPI_WITH_OBJDETECT +#if defined OPENCV_NAPI_SUPPORT_OBJDETECT #include "n_objdetect.h" #endif @@ -58,11 +58,11 @@ napi_module _module = { .nm_flags = 0, .nm_filename = nullptr, .nm_register_func = Init, - .nm_modname = "opencv_core", + .nm_modname = "opencv_napi", .nm_priv = ((void *)0), .reserved = {0} }; } // namespace OpencvNapi } // namespace OHOS -#endif // OPENCV_JS_NAPI_INCLUDE_INIT_H \ No newline at end of file +#endif // OPENCV_JS_NAPI_INCLUDE_INIT_H diff --git a/napi/js/napi/src/common.cpp b/napi/js/napi/src/common.cpp index 7f516f4e720813d0627c24dc53a42c07139d14b0..d960620f4445fd64da7f0ee5944aac5e6a22d17a 100644 --- a/napi/js/napi/src/common.cpp +++ b/napi/js/napi/src/common.cpp @@ -81,7 +81,7 @@ void Common::PaddingCallbackPromiseInfo( void Common::ReturnCallbackPromise(const napi_env &env, const CallbackPromiseInfo &info, const napi_value &result) { - OPENCV_LOGI("Enter ReturnCallbackPromise errorCode=%d", info.errorCode); + OPENCV_LOGI("Enter ReturnCallbackPromise errorCode=%{public}d", info.errorCode); if (info.isCallback) { SetCallback(env, info.callback, info.errorCode, result); @@ -95,12 +95,17 @@ void Common::ReturnCallbackPromise(const napi_env &env, const CallbackPromiseInf void Common::SetCallback( const napi_env &env, const napi_ref &callbackIn, const int32_t &errorCode, const napi_value &result) { + OPENCV_LOGI("Enter SetCallback with error code"); napi_value undefined = nullptr; napi_get_undefined(env, &undefined); napi_value callback = nullptr; napi_value resultout = nullptr; napi_get_reference_value(env, callbackIn, &callback); + if (callback == nullptr) { + OPENCV_LOGE("get reference of callback failed"); + return; + } napi_value results[ARGS_TWO] = {nullptr}; results[PARAM0] = GetCallbackErrorValue(env, errorCode); results[PARAM1] = result; @@ -113,21 +118,26 @@ void Common::SetCallback( OPENCV_LOGE("napi_call_function callback exception"); } - OPENCV_LOGI("SetCallback end"); + OPENCV_LOGI("SetCallback with error code end"); } void Common::SetCallback( const napi_env &env, const napi_ref &callbackIn, const napi_value &result) { + OPENCV_LOGI("Enter SetCallback without error code"); napi_value undefined = nullptr; napi_get_undefined(env, &undefined); napi_value callback = nullptr; napi_value resultout = nullptr; napi_get_reference_value(env, callbackIn, &callback); + if (callback == nullptr) { + OPENCV_LOGE("get reference of callback failed"); + return; + } NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_ONE, &result, &resultout)); - OPENCV_LOGI("end"); + OPENCV_LOGI("SetCallback without error code"); } void Common::SetPromise( @@ -228,7 +238,7 @@ napi_value Common::SetMat(const napi_env &env, const cv::Mat& mat, napi_value &r OPENCV_LOGE("napi_create_arraybuffer failed!"); napi_get_undefined(env, &result); } else { - OPENCV_LOGE("napi_create_arraybuffer sucessful!"); + OPENCV_LOGI("napi_create_arraybuffer sucessful!"); napi_set_named_property(env, result, "byteBuffer", array); } diff --git a/napi/js/napi/src/imgproc/n_HoughLines.cpp b/napi/js/napi/src/imgproc/n_HoughLines.cpp index 126b90a04ece8172a988636460764baa2427bd2d..05f338a41f7467720606004860203fce1a6c552d 100644 --- a/napi/js/napi/src/imgproc/n_HoughLines.cpp +++ b/napi/js/napi/src/imgproc/n_HoughLines.cpp @@ -52,29 +52,30 @@ napi_value SetHoughLinesOut(const napi_env &env, const vector &lines uint32_t array_len = 0; napi_value value = nullptr; - - napi_status result_status = napi_create_array(env, &result); // 创建一个napi数组contours + napi_value hlArray = nullptr; + napi_status result_status = napi_create_array(env, &hlArray); if (result_status != napi_ok) { napi_get_undefined(env, &result); return Common::NapiGetBoolean(env, false); } - for (int i = 0; i < linesInfo.size(); i++) { + for (uint32_t i = 0; i < linesInfo.size(); i++) { napi_value point = nullptr; napi_create_object(env, &point); napi_create_double(env, linesInfo[i].rho, &value); napi_set_named_property(env, point, "rho", value); - napi_create_double(env, linesInfo[i].theta, &value); napi_set_named_property(env, point, "theta", value); + napi_set_element(env, hlArray, i, point); - napi_set_element(env, result, i, point); + OPENCV_LOGI("houghLine[%{public}u] info: rho : %{public}f, theta: %{public}f", + i, linesInfo[i].rho, linesInfo[i].theta); } - - napi_get_array_length(env, result, &array_len); + napi_get_array_length(env, hlArray, &array_len); OPENCV_LOGI("SetHoughLinesOut array length:%{public}d", array_len); + napi_set_named_property(env, result, "houghLine", hlArray); return Common::NapiGetBoolean(env, true); } @@ -129,7 +130,7 @@ void AsyncCompleteCallbackHoughLines(napi_env env, napi_status status, void *dat if (asynccallbackinfo->outLines.size() == 0) { asynccallbackinfo->info.errorCode = ERROR; } else { - napi_value result; + napi_create_object(env, &result); if (!SetHoughLinesOut(env, asynccallbackinfo->outLines, result)) { asynccallbackinfo->info.errorCode = ERROR; result = Common::NapiGetNull(env); @@ -218,7 +219,7 @@ napi_value n_HoughLines(napi_env env, napi_callback_info info) #if defined OPENCV_NAPI_DEBUG bool ret = imwrite("/data/storage/el2/base/haps/entry/files/HoughLines.jpg", outMat); - OPENCV_LOGE("resize result imwrite ret: %{public}d", ret); + OPENCV_LOGI("resize result imwrite ret: %{public}d", ret); #endif } }, @@ -235,4 +236,4 @@ napi_value n_HoughLines(napi_env env, napi_callback_info info) } } } // OpencvNapi -} // OHOS \ No newline at end of file +} // OHOS diff --git a/napi/js/napi/src/init.cpp b/napi/js/napi/src/init.cpp index 162ff2ffd8f0688c0edfa268b58f6dfa49d6b1b0..7349b8bb6156dffb3d0934f581ef6413941ce89e 100644 --- a/napi/js/napi/src/init.cpp +++ b/napi/js/napi/src/init.cpp @@ -63,7 +63,7 @@ napi_value OpencvInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("HoughLines", n_HoughLines), #endif -#if defined OPENCV_NAPI_WITH_OBJDETECT +#if defined OPENCV_NAPI_SUPPORT_OBJDETECT DECLARE_NAPI_FUNCTION("qrcode", n_qrcode), DECLARE_NAPI_FUNCTION("detectMultiScale", n_detectMultiScale), #endif diff --git a/napi/js/napi/src/objdetect/n_qrcode.cpp b/napi/js/napi/src/objdetect/n_qrcode.cpp index 1aebb142b91c71cd30004c9daea67a133901eb89..360c316fa5aecac9f37280745971a56ab55fc21f 100644 --- a/napi/js/napi/src/objdetect/n_qrcode.cpp +++ b/napi/js/napi/src/objdetect/n_qrcode.cpp @@ -121,8 +121,9 @@ napi_value SetQrcode(const napi_env &env, bool detected, const vector &d napi_get_undefined(env, &result); return Common::NapiGetBoolean(env, false); } - + OPENCV_LOGI("decodeInfos size:%{public}d", decodeInfos.size()); for (int i = 0; i < decodeInfos.size(); i++) { + OPENCV_LOGI("decodeInfos[%{public}d] info %{public}s", i, decodeInfos[i].c_str()); napi_value decodeStr = nullptr; napi_create_string_utf8(env, decodeInfos[i].c_str(), NAPI_AUTO_LENGTH, &decodeStr); napi_set_element(env, decodeArray, i, decodeStr); // 添加一个napi元素到napi数组 @@ -211,4 +212,4 @@ napi_value n_qrcode(napi_env env, napi_callback_info info) } } } // OpencvNapi -} // OHOS \ No newline at end of file +} // OHOS diff --git a/napi/opencv_napi_cfg.gni b/napi/opencv_napi_cfg.gni index b0484caa62b28800a1d6328cd437df76c5c2d7b5..8615587bc53b99276943d023d5d4d395a40dac7e 100644 --- a/napi/opencv_napi_cfg.gni +++ b/napi/opencv_napi_cfg.gni @@ -15,14 +15,14 @@ import("//build/version.gni") import("//build/ohos_var.gni") declare_args() { - OPENCV_NAPI_WITH_CORE = true - OPENCV_NAPI_WITH_IMGPROC = true - OPENCV_NAPI_WITH_IMGCODECS = true - OPENCV_NAPI_WITH_OBJDETECT = true + opencv_napi_with_core = true + opencv_napi_with_imgproc = true + opencv_napi_with_imgcodecs = true + opencv_napi_with_objdetect = true } declare_args() { - OPENCV_NAPI_DEBUG_DEFINE = true + opencv_debug_enable = true } napi_sources_core = [