fixed: windows/linux双系统适配

This commit is contained in:
Guan Yuankai 2025-11-14 18:29:51 +08:00
parent 999a4e8c59
commit aaa71e0598
3 changed files with 56 additions and 19 deletions

View File

@ -1,7 +1,7 @@
#pragma once
#include <opencv2/opencv.hpp>
#include <onnxruntime_cxx_api.h>
#include <onnxruntime_cxx_api.h>
#include <vector>
#include <string>
#include <memory>
@ -42,7 +42,9 @@ public:
std::vector<const char*> 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<Detection> detect(
unsigned char* image_bytes,

View File

@ -88,7 +88,7 @@ std::vector<Detection> 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)

View File

@ -5,6 +5,27 @@
#include <stdexcept>
#include <iostream>
#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<const wchar_t*>(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<jlong>(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;