我需要将一个回调传递给一个自定义对象中的java代码。这是我想从C++代码编写的自定义类。
public class Result {
private String utterance[][];
private float confidence[][];
public String[][] getUtterance() {
return utterance;
}
public float[][] getConfidence() {
return confidence;
}
public void setUtterance(String utterance[][]) {
this.utterance = utterance;
}
public void setConfidence(float confidence[][]) {
this.confidence = confidence;
}
}班级识别器与JNI进行通信。所以,这个回调给她。
public class Recognizer {
private Result result;
Recognizer(){
System.load(LibNameLoader.libRoot(jarLibPath);
}
...
private void newResultCallback(Result result){//C++ send this object to java
this.result = result;
}
}现在,这是我的C++代码,它与我的C++对象一起提供,与我想要的java相同。记住这是一个回调,我不能改变签名。
void Recognized(RecoResult *result){
//class package
jclass cls = env->FindClass("Lbr/ufpa/laps/jlapsapi/recognizer/Result;");
jobject theObject; //i don't how to manipulate this one and set it arrays
int row = result->sentenceNumber; //Vertical array lenght
int column = result->wordsNumber; //Horizontal array lenght
string *ptrUtterance = result->getUtterancePtr(); //simple acess ptrUtterance[i][j]
float *ptrConfidence = result->getConfidencePtr(); // " " ptrconfidence[i][j]
jstring utteranceArray[row][column];
jfloat floatArray[row][column];
env->CallVoidMethod(obj_, midU, theObject);
//obj_ is global and has the reference to java Recognizer class
//midU is the reference to java method called
//theObject is the object that java will receive
}我只需要帮助设置object内的数组字段。或者我可以使用此方法发送jstring和jfloat数组env-> CallVoidMethod(obj_,midU1,jstringArray);和env-> CallVoidMethod(obj_,midU2,jfloatArray);
感谢帮助!
编辑!
识别器就是这样,我首先尝试一个单维数组
...
jclass testecls;
jmethodID testemtdid;
jobject testeobj;
jstring testestr;
jobjectArray testearray;
int i;
cout << "Rec cls"<< endl;
testecls = env->FindClass("Ljava/lang/String;");
if(testecls==NULL)
cout << "Rec cls = NULL"<< endl;
cout << "Rec mtdid"<< endl;
testemtdid = env->GetMethodID(testecls,"","(Ljava/lang/String;)V");
if(testemtdid==NULL)
cout << "Rec mtdid = NULL"<< endl;
cout << "Rec obj"<< endl;
testeobj = env->NewObject(testecls, NULL);
if(testeobj==NULL)
cout << "Rec obj = NULL"<< endl;
cout << "Rec str"<< endl;
testestr = env->NewStringUTF(result->getUtterance().c_str());
if(testestr==NULL)
cout << "Rec str = NULL"<< endl;
cout << "Rec array"<< endl;
testearray = env->NewObjectArray(result->wordsNumber, testecls, testeobj);
if(testearray==NULL)
cout << "Rec array = NULL"<< endl;
for(i=0;iwordsNumber;i++){
cout << "Rec setarray"<< endl;
env->SetObjectArrayElement(testearray, i, testestr);
}
cout << "Rec before call"<< endl;
env->CallVoidMethod(obj_, midUArray, testearray);
cout << "Rec after call"<< endl;输出是,当我尝试安装字符串类型的对象时它崩溃。
Rec cls
Rec mtdid
Rec obj
#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0xb6dbf389, pid=15337, tid=1769995120
#
# JRE version: 6.0_24-b07
# Java VM: Java HotSpot(TM) Server VM (19.1-b02 mixed mode linux-x86 )
# Problematic frame:
# V [libjvm.so+0x3e0389]
#
# An error report file with more information is saved as:
# /home/10080000701/workspace/jlapsapi/hs_err_pid15337.log
#
# If you would like to submit a bug report, please visit:
# http://java.sun.com/webapps/bugreport/crash.jsp
#你有什么建议?