diff --git a/cpp/include/YoloCore.h b/cpp/include/YoloCore.h index 5f82593..3219cba 100644 --- a/cpp/include/YoloCore.h +++ b/cpp/include/YoloCore.h @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include #include #include @@ -42,7 +42,9 @@ public: std::vector output_node_names; public: - YoloDetector(const char* model_path, int in_width, int in_height); + /** + * */ + YoloDetector(const ORTCHAR_T* model_path, int in_width, int in_height); std::vector detect( unsigned char* image_bytes, diff --git a/cpp/src/YoloCore.cpp b/cpp/src/YoloCore.cpp index 34fd424..94937e6 100644 --- a/cpp/src/YoloCore.cpp +++ b/cpp/src/YoloCore.cpp @@ -88,7 +88,7 @@ std::vector postprocess(Ort::Value& output_tensor, float scale, int p /** * @brief YoloDetector */ -YoloDetector::YoloDetector(const char* model_path, int in_width, int in_height) +YoloDetector::YoloDetector(const ORTCHAR_T* model_path, int in_width, int in_height) : env(ORT_LOGGING_LEVEL_WARNING, "YOLOv8-ONNX-CPU"), input_width(in_width), input_height(in_height) diff --git a/cpp/src/YoloSdk_JNI.cpp b/cpp/src/YoloSdk_JNI.cpp index 1bbfad9..f6c1a25 100644 --- a/cpp/src/YoloSdk_JNI.cpp +++ b/cpp/src/YoloSdk_JNI.cpp @@ -5,6 +5,27 @@ #include #include + +#if defined(_WIN32) +std::wstring jstringToWString(JNIEnv *env, jstring jStr) { + if (!jStr) return L""; + + const jchar *raw = env->GetStringChars(jStr, nullptr); + if (!raw) return L""; + + + jsize len = env->GetStringLength(jStr); + + + std::wstring wStr(reinterpret_cast(raw), len); + + env->ReleaseStringChars(jStr, raw); + + return wStr; +} +#endif + + void throwJavaException(JNIEnv *env, const char *message) { env->ThrowNew(env->FindClass("java/lang/RuntimeException"), message); } @@ -17,25 +38,39 @@ void throwJavaException(JNIEnv *env, const char *message) { JNIEXPORT jlong JNICALL Java_com_bonus_sdk_YoloSdk_nativeInit (JNIEnv *env, jobject thiz, jstring modelPath, jint inputWidth, jint inputHeight) { - - const char* c_model_path = env->GetStringUTFChars(modelPath, nullptr); - if (c_model_path == nullptr) { - throwJavaException(env, "Failed to get model path from Java string."); - return 0; - } - try { - - YoloDetector* detector = new YoloDetector(c_model_path, inputWidth, inputHeight); - - - env->ReleaseStringUTFChars(modelPath, c_model_path); - + YoloDetector* detector = nullptr; + + #if defined(_WIN32) + + std::wstring wpath = jstringToWString(env, modelPath); + if (wpath.empty() && env->ExceptionCheck() == JNI_FALSE) { + throwJavaException(env, "Failed to convert model path to wstring."); + return 0; + } + detector = new YoloDetector(wpath.c_str(), inputWidth, inputHeight); + #else + + const char* c_model_path = env->GetStringUTFChars(modelPath, nullptr); + if (c_model_path == nullptr) { + throwJavaException(env, "Failed to get model path from Java string."); + return 0; + } + + + + try { + detector = new YoloDetector(c_model_path, inputWidth, inputHeight); + } catch (const std::exception& e) { + env->ReleaseStringUTFChars(modelPath, c_model_path); + throw; + } + env->ReleaseStringUTFChars(modelPath, c_model_path); + #endif + return reinterpret_cast(detector); + } catch (const std::exception& e) { - - env->ReleaseStringUTFChars(modelPath, c_model_path); - std::string errMsg = "Failed to initialize C++ YoloDetector: " + std::string(e.what()); throwJavaException(env, errMsg.c_str()); return 0;