Мне нужно передать обратный вызов в код 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;
}
}
Класс Recognizer имеет связь с 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
}
Мне просто нужна помощь, чтобы установить поля массива внутри объекта. Или я мог бы отправить jstring и массив jfloat используя этот метод env-> CallVoidMethod (obj_, midU1, jstringArray); и env-> CallVoidMethod (obj_, midU2, jfloatArray);
спасибо за помощь!
Edited!
Распознаватель похож на это, я пытаюсь сначала с одномерным массивом
...
JClass Testecls;
jmethodID testemtdid;
задание на тестирование;
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,"<init>","(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;i<result->wordsNumber;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;
вывод: он падает, когда я пытаюсь установить объект типа String.
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
#
Что вы предлагаете?