2024-06-04 15:25:02 +08:00
|
|
|
|
#include "CplusToJava.h"
|
|
|
|
|
|
|
|
|
|
jobjectArray CplusToJava::GetjobjectArray(JNIEnv* env, jclass jcls, jobject jstruct, std::string var)
|
|
|
|
|
{
|
|
|
|
|
// <20><>ȡ<EFBFBD><C8A1>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD>var<61><72>ֵ
|
|
|
|
|
jfieldID fieldIDlist = env->GetFieldID(jcls, var.c_str(), "Ljava/util/List;");
|
|
|
|
|
jobject listObject = env->GetObjectField(jstruct, fieldIDlist);
|
|
|
|
|
jclass listClass = env->FindClass("java/util/List");
|
|
|
|
|
jmethodID toArrayMethodID = env->GetMethodID(listClass, "toArray", "()[Ljava/lang/Object;");
|
|
|
|
|
jobjectArray listValue = (jobjectArray)env->CallObjectMethod(listObject, toArrayMethodID);
|
|
|
|
|
return listValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jobjectArray CplusToJava::QVectorD2jobjectArray(QVector<double> vec, JNIEnv* env)
|
|
|
|
|
{
|
|
|
|
|
jint arraySize = vec.size();
|
|
|
|
|
jclass doubleClass = env->FindClass("java/lang/Double");
|
|
|
|
|
jobjectArray array = env->NewObjectArray(arraySize, doubleClass, NULL);
|
|
|
|
|
jmethodID constructorID = env->GetMethodID(doubleClass, "<init>", "(D)V");
|
|
|
|
|
for (int i = 0; i < arraySize; i++) {
|
|
|
|
|
jobject doubleObject = env->NewObject(doubleClass, constructorID, vec[i]);
|
|
|
|
|
env->SetObjectArrayElement(array, i, doubleObject);
|
|
|
|
|
}
|
|
|
|
|
return array;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jobjectArray CplusToJava::QVectorQS2jobjectArray(QVector<QString> vec, JNIEnv* env)
|
|
|
|
|
{
|
|
|
|
|
jint arraySize = vec.size();
|
|
|
|
|
jclass stringClass = env->FindClass("java/lang/String");
|
|
|
|
|
jobjectArray array = env->NewObjectArray(arraySize, stringClass, NULL);
|
|
|
|
|
jmethodID constructorID = env->GetMethodID(stringClass, "<init>", "(Ljava/lang/String;)V");
|
|
|
|
|
for (int i = 0; i < arraySize; i++) {
|
|
|
|
|
jstring tempstr = env->NewStringUTF(vec[i].toStdString().c_str());
|
|
|
|
|
jobject stringObject = env->NewObject(stringClass, constructorID, tempstr);
|
|
|
|
|
env->SetObjectArrayElement(array, i, stringObject);
|
|
|
|
|
}
|
|
|
|
|
return array;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jobjectArray CplusToJava::QVectorL2jobjectArray(QVector<long> vec, JNIEnv* env)
|
|
|
|
|
{
|
|
|
|
|
jint arraySize = vec.size();
|
|
|
|
|
jclass longClass = env->FindClass("java/lang/Long");
|
|
|
|
|
jobjectArray array = env->NewObjectArray(arraySize, longClass, NULL);
|
|
|
|
|
jmethodID constructorID = env->GetMethodID(longClass, "<init>", "(J)V");
|
|
|
|
|
for (int i = 0; i < arraySize; i++) {
|
|
|
|
|
jobject longObject = env->NewObject(longClass, constructorID, vec[i]);
|
|
|
|
|
env->SetObjectArrayElement(array, i, longObject);
|
|
|
|
|
}
|
|
|
|
|
return array;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jobjectArray CplusToJava::QVectorI2jobjectArray(QVector<int> vec, JNIEnv* env)
|
|
|
|
|
{
|
|
|
|
|
jint arraySize = vec.size();
|
|
|
|
|
jclass intClass = env->FindClass("java/lang/Double");
|
|
|
|
|
jobjectArray array = env->NewObjectArray(arraySize, intClass, NULL);
|
|
|
|
|
jmethodID constructorID = env->GetMethodID(intClass, "<init>", "(D)V");
|
|
|
|
|
for (int i = 0; i < arraySize; i++) {
|
|
|
|
|
jobject intObject = env->NewObject(intClass, constructorID, vec[i]);
|
|
|
|
|
env->SetObjectArrayElement(array, i, intObject);
|
|
|
|
|
}
|
|
|
|
|
return array;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jobjectArray CplusToJava::QVectorS2jobjectArray(QVector<short> vec, JNIEnv* env)
|
|
|
|
|
{
|
|
|
|
|
jint arraySize = vec.size();
|
|
|
|
|
jclass intClass = env->FindClass("java/lang/Short");
|
|
|
|
|
jobjectArray array = env->NewObjectArray(arraySize, intClass, NULL);
|
|
|
|
|
jmethodID constructorID = env->GetMethodID(intClass, "<init>", "(S)V");
|
|
|
|
|
for (int i = 0; i < arraySize; i++) {
|
|
|
|
|
jobject intObject = env->NewObject(intClass, constructorID, vec[i]);
|
|
|
|
|
env->SetObjectArrayElement(array, i, intObject);
|
|
|
|
|
}
|
|
|
|
|
return array;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jobject CplusToJava::Createjobject(JNIEnv* env, jint esize, jobjectArray array)
|
|
|
|
|
{
|
|
|
|
|
jobject arrayList = env->NewObject(env->FindClass("java/util/ArrayList"), env->GetMethodID(env->FindClass("java/util/ArrayList"), "<init>", "()V"));
|
|
|
|
|
jmethodID addMethodID = env->GetMethodID(env->FindClass("java/util/ArrayList"), "add", "(Ljava/lang/Object;)Z");
|
|
|
|
|
for (int i = 0; i < esize; i++) {
|
|
|
|
|
jobject element = env->GetObjectArrayElement(array, i);
|
|
|
|
|
env->CallBooleanMethod(arrayList, addMethodID, element);
|
|
|
|
|
}
|
|
|
|
|
return arrayList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jobjectArray CplusToJava::jobject2jobjectArray(JNIEnv* env, jobject array)
|
|
|
|
|
{
|
|
|
|
|
jclass listClass = env->FindClass("java/util/List");
|
|
|
|
|
jmethodID toArrayMethodID = env->GetMethodID(listClass, "toArray", "()[Ljava/lang/Object;");
|
|
|
|
|
jobjectArray listValue = (jobjectArray)env->CallObjectMethod(array, toArrayMethodID);
|
|
|
|
|
return listValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVector<double> CplusToJava::jobjectArray2QVectorD(JNIEnv* env, jobjectArray listValue)
|
|
|
|
|
{
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
|
|
|
|
|
jsize arrayLen = env->GetArrayLength(listValue);
|
|
|
|
|
QVector<double> results;
|
|
|
|
|
for (int i = 0; i < arrayLen; i++) {
|
|
|
|
|
jobject element = env->GetObjectArrayElement(listValue, i);
|
|
|
|
|
jdouble value = env->CallDoubleMethod(element, env->GetMethodID(env->FindClass("java/lang/Double"), "doubleValue", "()D"));
|
|
|
|
|
results.push_back(value);
|
|
|
|
|
}
|
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString CplusToJava::jstring2QString(JNIEnv* env, jstring jstr)
|
|
|
|
|
{
|
|
|
|
|
const char* str = env->GetStringUTFChars(jstr, NULL);
|
|
|
|
|
std::string filename(str);
|
|
|
|
|
QString qstr = QString::fromStdString(filename);
|
|
|
|
|
|
|
|
|
|
// <20>ͷ<EFBFBD><CDB7>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>ڴ<EFBFBD>
|
|
|
|
|
env->ReleaseStringUTFChars(jstr, str);
|
|
|
|
|
return qstr;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-21 10:28:30 +08:00
|
|
|
|
|
2024-06-04 15:25:02 +08:00
|
|
|
|
std::vector<double> CplusToJava::jobjectArray2vectorD(JNIEnv* env, jobjectArray listValue)
|
|
|
|
|
{
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
|
|
|
|
|
jsize arrayLen = env->GetArrayLength(listValue);
|
|
|
|
|
std::vector<double> results;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < arrayLen; i++) {
|
|
|
|
|
jobject element = env->GetObjectArrayElement(listValue, i);
|
2024-09-21 10:28:30 +08:00
|
|
|
|
if (element)
|
|
|
|
|
{
|
|
|
|
|
jdouble value = env->CallDoubleMethod(element, env->GetMethodID(env->FindClass("java/lang/Double"), "doubleValue", "()D"));
|
|
|
|
|
results.push_back(value);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
results.push_back(qQNaN());
|
|
|
|
|
}
|
2024-06-04 15:25:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jobjectArray CplusToJava::VectorD2jobjectArray(std::vector<double> vec, JNIEnv* env)
|
|
|
|
|
{
|
|
|
|
|
jint arraySize = vec.size();
|
|
|
|
|
jclass doubleClass = env->FindClass("java/lang/Double");
|
|
|
|
|
jobjectArray array = env->NewObjectArray(arraySize, doubleClass, NULL);
|
|
|
|
|
jmethodID constructorID = env->GetMethodID(doubleClass, "<init>", "(D)V");
|
|
|
|
|
for (int i = 0; i < arraySize; i++) {
|
|
|
|
|
jobject doubleObject = env->NewObject(doubleClass, constructorID, vec[i]);
|
|
|
|
|
env->SetObjectArrayElement(array, i, doubleObject);
|
|
|
|
|
}
|
|
|
|
|
return array;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jobjectArray CplusToJava::uvec2jobjectArray(arma::uvec vec, JNIEnv* env)
|
|
|
|
|
{
|
|
|
|
|
jint arraySize = vec.size();
|
|
|
|
|
jclass doubleClass = env->FindClass("java/lang/Double");
|
|
|
|
|
jobjectArray array = env->NewObjectArray(arraySize, doubleClass, NULL);
|
|
|
|
|
jmethodID constructorID = env->GetMethodID(doubleClass, "<init>", "(D)V");
|
|
|
|
|
for (int i = 0; i < arraySize; i++) {
|
|
|
|
|
jobject doubleObject = env->NewObject(doubleClass, constructorID, vec[i]);
|
|
|
|
|
env->SetObjectArrayElement(array, i, doubleObject);
|
|
|
|
|
}
|
|
|
|
|
return array;
|
|
|
|
|
}
|