通过 JNI 的数据未正确传递
Data through JNI is not passing properly
我正在使用JNI来调用本机C++层。
爪哇层
int res= recog(audioFilePath, grammarFilePath, contextID, subContextID);
C++层
JNIEXPORT void JNICALL Java_com_uniphore_voice_recogniser_NuanceOfflineRecogniser_recog(JNIEnv *jenv, jobject jobj, jstring jaudioFilePath,
jstring jgrammarFilePath, <br/>
jstring jcontextID,
jstring jsubContext)
{
const char* _audioFilePath = (char*)jenv->GetStringChars(jaudioFilePath, JNI_FALSE);
const char* _grammarFilePath = (char*)jenv->GetStringChars(jgrammarFilePath, JNI_FALSE);
const char* _contextId = (char*)jenv->GetStringChars(jcontextID, JNI_FALSE);
const char* _subContextId = (char*)jenv->GetStringChars(jsubContext, JNI_FALSE);
std::wcout << "audio file path: " << _audioFilePath <<" "<< std::strlen(_audioFilePath) <<std::endl
<< "grammar file path: "<< _grammarFilePath <<" "<<std::strlen(_grammarFilePath) << std::endl
<< "contextId: " << _contextId << std::endl
<< "subContextId: " << _subContextId << std::endl << std::endl;
我可以看到java层的值被正确传递到较低级别,但是在c ++层中,在C++层中打印该值时,我可以看到它只打印整个字符串的第一个字符。
假设如果我像"c:\test.wav"一样传递音频文件路径,我只像 c 一样在 c++ 层中打印
我正在尝试Visual Studio 2013和项目字符支持,我选择作为Unicode支持。
我是 c++ 环境的新手,请帮助了解这个环境的原因。
根据 JNI 文档GetStringChars
返回给定字符串的 unicode 字符,这是一个unsigned short *
jchar *
。你把它投射到一个char *
.当您将 cout
与 char *
一起使用时,它需要一个带有空终止符的 ASCII 格式的字符串。您可以向它传递一个指向 unicode 格式字符串的指针,该字符串具有纯 ASCII 字符的所有其他字符0
。因此,为什么只打印字符串中的第一个字符。
GetStringChars 不返回指向单字节字符的指针,而是返回两个字节的 unicode 字符
const jchar * GetStringChars(JNIEnv *env, jstring string,
jboolean *isCopy);
Returns a pointer to the array of Unicode characters of the string.
相反,请尝试
GetStringUTFChars
这也将以空终止。
相关文章:
- 防止主数据类型C++的隐式转换
- 用于访问容器<T>数据成员的正确 API
- 嵌套在类中时无法设置成员数据
- 使用流处理接收到的数据
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 在cuda线程之间共享大量常量数据
- C++将文本文件中的数据读取到结构数组中
- 通过JNI传递数据数组的最快方法是什么
- 如何将两个 jlong 数据类型转换为 jstring,然后将两个字符串连接在一起以便从 JNI 将字符串返回给 jav
- 将 JSON 对象作为参数在 JNI 中传递给 CPP,并在 CPP 中检索 JSON 对象的数据
- 准备要传递给 jni 的位图字节 [] 数据
- 如何使用JNI将数据传递到构造函数
- 如何使用JNI + OpenCV + NDK获取原始的android相机传感器图像数据
- 使用 JNI 通过从C++到 Java 的指针返回数据
- JNI:如何将一组数据从c++转换为Java
- JNI:RGB565位图到灰度 - 如何将JNI更改的数据传递回Java
- 通过 JNI 的数据未正确传递
- Android & JNI 如何将 Byte[] 数据数组传递给 JNI 并取回 Byte []
- 如何使用jni在c++中将数据从javaInputStream移动到char*