change Byte[] values in JNI with android studio
up vote
0
down vote
favorite
i want to reference a parameter, Byte, in a JNI function and replace values of them.
The declaration of JNI is below.
public native void imageprocessing(long inputImage, long inputImage2, long outputImage, long outputImage2, Byte sim);
The sim is the target what i want to change.
The interface of it is below.
Java_com_example_duru_opencvtest_MainActivity_imageprocessing(JNIEnv *env, jobject instance,
jlong inputImage, jlong inputImage2, jlong outputImage, jlong outputImage2, jobjectArray sim)
it uses jobjectArray type and i want to put int type values of native language into sim object.
so i my method is
jbyteArray byte_array = env->NewByteArray(4);
env->SetByteArrayRegion(byte_array, 0, 4, (jbyte*)tempSim);
jobjectArray object_array = env->NewObjectArray(4, env->FindClass("java/lang/Byte"), byte_array);
/* ERROR
(*env).SetObjectArrayElement(sim, 0, (jobject)object_array[0]);
(*env).SetObjectArrayElement(sim, 1, (jobject)object_array[1]);
(*env).SetObjectArrayElement(sim, 2, (jobject)object_array[2]);
(*env).SetObjectArrayElement(sim, 3, (jobject)object_array[3]);
*/
tempSim is 'int tempSim[4]' and Sim also has 4 length.
(*env).SetObjectArrayElement(sim, 0, (jobject)object_array[0]);
The bold part occur syntax error than the other part have no problem?
java
add a comment |
up vote
0
down vote
favorite
i want to reference a parameter, Byte, in a JNI function and replace values of them.
The declaration of JNI is below.
public native void imageprocessing(long inputImage, long inputImage2, long outputImage, long outputImage2, Byte sim);
The sim is the target what i want to change.
The interface of it is below.
Java_com_example_duru_opencvtest_MainActivity_imageprocessing(JNIEnv *env, jobject instance,
jlong inputImage, jlong inputImage2, jlong outputImage, jlong outputImage2, jobjectArray sim)
it uses jobjectArray type and i want to put int type values of native language into sim object.
so i my method is
jbyteArray byte_array = env->NewByteArray(4);
env->SetByteArrayRegion(byte_array, 0, 4, (jbyte*)tempSim);
jobjectArray object_array = env->NewObjectArray(4, env->FindClass("java/lang/Byte"), byte_array);
/* ERROR
(*env).SetObjectArrayElement(sim, 0, (jobject)object_array[0]);
(*env).SetObjectArrayElement(sim, 1, (jobject)object_array[1]);
(*env).SetObjectArrayElement(sim, 2, (jobject)object_array[2]);
(*env).SetObjectArrayElement(sim, 3, (jobject)object_array[3]);
*/
tempSim is 'int tempSim[4]' and Sim also has 4 length.
(*env).SetObjectArrayElement(sim, 0, (jobject)object_array[0]);
The bold part occur syntax error than the other part have no problem?
java
You can't access Java arrays like that from native code. In this case you need to use theGetObjectArrayElementfunction. Although I don't really see what the purpose ofbyte_arrayandobject_arrayis. Why don't you just pass each oftempSim's values toByte.valueOfinstead?
– Michael
Nov 20 at 7:03
Note:NewByteArrayis for a primative array (Java:byte; JNI:jbytearray), not an object array (Java:Byte).
– Tom Blodget
Nov 25 at 3:46
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
i want to reference a parameter, Byte, in a JNI function and replace values of them.
The declaration of JNI is below.
public native void imageprocessing(long inputImage, long inputImage2, long outputImage, long outputImage2, Byte sim);
The sim is the target what i want to change.
The interface of it is below.
Java_com_example_duru_opencvtest_MainActivity_imageprocessing(JNIEnv *env, jobject instance,
jlong inputImage, jlong inputImage2, jlong outputImage, jlong outputImage2, jobjectArray sim)
it uses jobjectArray type and i want to put int type values of native language into sim object.
so i my method is
jbyteArray byte_array = env->NewByteArray(4);
env->SetByteArrayRegion(byte_array, 0, 4, (jbyte*)tempSim);
jobjectArray object_array = env->NewObjectArray(4, env->FindClass("java/lang/Byte"), byte_array);
/* ERROR
(*env).SetObjectArrayElement(sim, 0, (jobject)object_array[0]);
(*env).SetObjectArrayElement(sim, 1, (jobject)object_array[1]);
(*env).SetObjectArrayElement(sim, 2, (jobject)object_array[2]);
(*env).SetObjectArrayElement(sim, 3, (jobject)object_array[3]);
*/
tempSim is 'int tempSim[4]' and Sim also has 4 length.
(*env).SetObjectArrayElement(sim, 0, (jobject)object_array[0]);
The bold part occur syntax error than the other part have no problem?
java
i want to reference a parameter, Byte, in a JNI function and replace values of them.
The declaration of JNI is below.
public native void imageprocessing(long inputImage, long inputImage2, long outputImage, long outputImage2, Byte sim);
The sim is the target what i want to change.
The interface of it is below.
Java_com_example_duru_opencvtest_MainActivity_imageprocessing(JNIEnv *env, jobject instance,
jlong inputImage, jlong inputImage2, jlong outputImage, jlong outputImage2, jobjectArray sim)
it uses jobjectArray type and i want to put int type values of native language into sim object.
so i my method is
jbyteArray byte_array = env->NewByteArray(4);
env->SetByteArrayRegion(byte_array, 0, 4, (jbyte*)tempSim);
jobjectArray object_array = env->NewObjectArray(4, env->FindClass("java/lang/Byte"), byte_array);
/* ERROR
(*env).SetObjectArrayElement(sim, 0, (jobject)object_array[0]);
(*env).SetObjectArrayElement(sim, 1, (jobject)object_array[1]);
(*env).SetObjectArrayElement(sim, 2, (jobject)object_array[2]);
(*env).SetObjectArrayElement(sim, 3, (jobject)object_array[3]);
*/
tempSim is 'int tempSim[4]' and Sim also has 4 length.
(*env).SetObjectArrayElement(sim, 0, (jobject)object_array[0]);
The bold part occur syntax error than the other part have no problem?
java
java
asked Nov 20 at 6:31
KJS_999
133
133
You can't access Java arrays like that from native code. In this case you need to use theGetObjectArrayElementfunction. Although I don't really see what the purpose ofbyte_arrayandobject_arrayis. Why don't you just pass each oftempSim's values toByte.valueOfinstead?
– Michael
Nov 20 at 7:03
Note:NewByteArrayis for a primative array (Java:byte; JNI:jbytearray), not an object array (Java:Byte).
– Tom Blodget
Nov 25 at 3:46
add a comment |
You can't access Java arrays like that from native code. In this case you need to use theGetObjectArrayElementfunction. Although I don't really see what the purpose ofbyte_arrayandobject_arrayis. Why don't you just pass each oftempSim's values toByte.valueOfinstead?
– Michael
Nov 20 at 7:03
Note:NewByteArrayis for a primative array (Java:byte; JNI:jbytearray), not an object array (Java:Byte).
– Tom Blodget
Nov 25 at 3:46
You can't access Java arrays like that from native code. In this case you need to use the
GetObjectArrayElement function. Although I don't really see what the purpose of byte_array and object_array is. Why don't you just pass each of tempSim's values to Byte.valueOf instead?– Michael
Nov 20 at 7:03
You can't access Java arrays like that from native code. In this case you need to use the
GetObjectArrayElement function. Although I don't really see what the purpose of byte_array and object_array is. Why don't you just pass each of tempSim's values to Byte.valueOf instead?– Michael
Nov 20 at 7:03
Note:
NewByteArray is for a primative array (Java: byte; JNI: jbytearray), not an object array (Java: Byte).– Tom Blodget
Nov 25 at 3:46
Note:
NewByteArray is for a primative array (Java: byte; JNI: jbytearray), not an object array (Java: Byte).– Tom Blodget
Nov 25 at 3:46
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
jclass javaLangByteClass = env->FindClass("java/lang/Byte");
jmethodID javaLangByteConstructor = env->GetMethodID(javaLangByteClass , "<init>", "(B)Ljava/lang/Byte;")
for (int i=0; i<3; i++) {
jobject nextElement = env->NewObject(javaLangByteClass, javaLangByteConstructor, (jbyte)tempSim[i]);
env->SetObjectArrayElement(sim, i, nextElement);
env->DeleteLocalRef(nextElement);
}
See the comment below: Byte.valueOf() may be more efficient than the constructor:
jclass javaLangByteClass = env->FindClass("java/lang/Byte");
jmethodID javaLangByteStaticValueOf = env->GetStaticMethodID(javaLangByteClass , "valueOf", "(B)Ljava/lang/Byte;")
for (int i=0; i<3; i++) {
jobject nextElement = env->CallStaticObjectMethod(javaLangByteClass, javaLangByteStaticValueOf, (jbyte)tempSim[i]);
env->SetObjectArrayElement(sim, i, nextElement);
env->DeleteLocalRef(nextElement);
}
1
Byte.valueOf(byte)should generally be preferred overnew Byte(byte), as the former cachesByteinstances of all possiblebytevalues.
– Michael
Nov 20 at 10:05
1
how can i use Byte.valueof() in JNI?? i try to it but nothing works. and is it possible the method, 'Byte.valueof()', replace the javaLangByteConstructor line? since this statement occur runtime error.
– KJS_999
Nov 20 at 14:00
thank you for your help, but still have a runtime error atjmethodID javaLangByteStaticValueOf = env->GetMethodID(javaLangByteClass , "valueOf", "(B)Ljava/lang/Byte;");i think that signature is problem so i change the return type,Ljava/lang/Byte, to String since it is strange that same as return type parameter type. but it didn't work..
– KJS_999
Nov 20 at 14:39
Sorry, copy/paste mistake. We needGetStaticMethodID()
– Alex Cohn
Nov 20 at 15:42
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
jclass javaLangByteClass = env->FindClass("java/lang/Byte");
jmethodID javaLangByteConstructor = env->GetMethodID(javaLangByteClass , "<init>", "(B)Ljava/lang/Byte;")
for (int i=0; i<3; i++) {
jobject nextElement = env->NewObject(javaLangByteClass, javaLangByteConstructor, (jbyte)tempSim[i]);
env->SetObjectArrayElement(sim, i, nextElement);
env->DeleteLocalRef(nextElement);
}
See the comment below: Byte.valueOf() may be more efficient than the constructor:
jclass javaLangByteClass = env->FindClass("java/lang/Byte");
jmethodID javaLangByteStaticValueOf = env->GetStaticMethodID(javaLangByteClass , "valueOf", "(B)Ljava/lang/Byte;")
for (int i=0; i<3; i++) {
jobject nextElement = env->CallStaticObjectMethod(javaLangByteClass, javaLangByteStaticValueOf, (jbyte)tempSim[i]);
env->SetObjectArrayElement(sim, i, nextElement);
env->DeleteLocalRef(nextElement);
}
1
Byte.valueOf(byte)should generally be preferred overnew Byte(byte), as the former cachesByteinstances of all possiblebytevalues.
– Michael
Nov 20 at 10:05
1
how can i use Byte.valueof() in JNI?? i try to it but nothing works. and is it possible the method, 'Byte.valueof()', replace the javaLangByteConstructor line? since this statement occur runtime error.
– KJS_999
Nov 20 at 14:00
thank you for your help, but still have a runtime error atjmethodID javaLangByteStaticValueOf = env->GetMethodID(javaLangByteClass , "valueOf", "(B)Ljava/lang/Byte;");i think that signature is problem so i change the return type,Ljava/lang/Byte, to String since it is strange that same as return type parameter type. but it didn't work..
– KJS_999
Nov 20 at 14:39
Sorry, copy/paste mistake. We needGetStaticMethodID()
– Alex Cohn
Nov 20 at 15:42
add a comment |
up vote
2
down vote
accepted
jclass javaLangByteClass = env->FindClass("java/lang/Byte");
jmethodID javaLangByteConstructor = env->GetMethodID(javaLangByteClass , "<init>", "(B)Ljava/lang/Byte;")
for (int i=0; i<3; i++) {
jobject nextElement = env->NewObject(javaLangByteClass, javaLangByteConstructor, (jbyte)tempSim[i]);
env->SetObjectArrayElement(sim, i, nextElement);
env->DeleteLocalRef(nextElement);
}
See the comment below: Byte.valueOf() may be more efficient than the constructor:
jclass javaLangByteClass = env->FindClass("java/lang/Byte");
jmethodID javaLangByteStaticValueOf = env->GetStaticMethodID(javaLangByteClass , "valueOf", "(B)Ljava/lang/Byte;")
for (int i=0; i<3; i++) {
jobject nextElement = env->CallStaticObjectMethod(javaLangByteClass, javaLangByteStaticValueOf, (jbyte)tempSim[i]);
env->SetObjectArrayElement(sim, i, nextElement);
env->DeleteLocalRef(nextElement);
}
1
Byte.valueOf(byte)should generally be preferred overnew Byte(byte), as the former cachesByteinstances of all possiblebytevalues.
– Michael
Nov 20 at 10:05
1
how can i use Byte.valueof() in JNI?? i try to it but nothing works. and is it possible the method, 'Byte.valueof()', replace the javaLangByteConstructor line? since this statement occur runtime error.
– KJS_999
Nov 20 at 14:00
thank you for your help, but still have a runtime error atjmethodID javaLangByteStaticValueOf = env->GetMethodID(javaLangByteClass , "valueOf", "(B)Ljava/lang/Byte;");i think that signature is problem so i change the return type,Ljava/lang/Byte, to String since it is strange that same as return type parameter type. but it didn't work..
– KJS_999
Nov 20 at 14:39
Sorry, copy/paste mistake. We needGetStaticMethodID()
– Alex Cohn
Nov 20 at 15:42
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
jclass javaLangByteClass = env->FindClass("java/lang/Byte");
jmethodID javaLangByteConstructor = env->GetMethodID(javaLangByteClass , "<init>", "(B)Ljava/lang/Byte;")
for (int i=0; i<3; i++) {
jobject nextElement = env->NewObject(javaLangByteClass, javaLangByteConstructor, (jbyte)tempSim[i]);
env->SetObjectArrayElement(sim, i, nextElement);
env->DeleteLocalRef(nextElement);
}
See the comment below: Byte.valueOf() may be more efficient than the constructor:
jclass javaLangByteClass = env->FindClass("java/lang/Byte");
jmethodID javaLangByteStaticValueOf = env->GetStaticMethodID(javaLangByteClass , "valueOf", "(B)Ljava/lang/Byte;")
for (int i=0; i<3; i++) {
jobject nextElement = env->CallStaticObjectMethod(javaLangByteClass, javaLangByteStaticValueOf, (jbyte)tempSim[i]);
env->SetObjectArrayElement(sim, i, nextElement);
env->DeleteLocalRef(nextElement);
}
jclass javaLangByteClass = env->FindClass("java/lang/Byte");
jmethodID javaLangByteConstructor = env->GetMethodID(javaLangByteClass , "<init>", "(B)Ljava/lang/Byte;")
for (int i=0; i<3; i++) {
jobject nextElement = env->NewObject(javaLangByteClass, javaLangByteConstructor, (jbyte)tempSim[i]);
env->SetObjectArrayElement(sim, i, nextElement);
env->DeleteLocalRef(nextElement);
}
See the comment below: Byte.valueOf() may be more efficient than the constructor:
jclass javaLangByteClass = env->FindClass("java/lang/Byte");
jmethodID javaLangByteStaticValueOf = env->GetStaticMethodID(javaLangByteClass , "valueOf", "(B)Ljava/lang/Byte;")
for (int i=0; i<3; i++) {
jobject nextElement = env->CallStaticObjectMethod(javaLangByteClass, javaLangByteStaticValueOf, (jbyte)tempSim[i]);
env->SetObjectArrayElement(sim, i, nextElement);
env->DeleteLocalRef(nextElement);
}
edited Nov 20 at 15:41
answered Nov 20 at 8:27
Alex Cohn
40.5k551183
40.5k551183
1
Byte.valueOf(byte)should generally be preferred overnew Byte(byte), as the former cachesByteinstances of all possiblebytevalues.
– Michael
Nov 20 at 10:05
1
how can i use Byte.valueof() in JNI?? i try to it but nothing works. and is it possible the method, 'Byte.valueof()', replace the javaLangByteConstructor line? since this statement occur runtime error.
– KJS_999
Nov 20 at 14:00
thank you for your help, but still have a runtime error atjmethodID javaLangByteStaticValueOf = env->GetMethodID(javaLangByteClass , "valueOf", "(B)Ljava/lang/Byte;");i think that signature is problem so i change the return type,Ljava/lang/Byte, to String since it is strange that same as return type parameter type. but it didn't work..
– KJS_999
Nov 20 at 14:39
Sorry, copy/paste mistake. We needGetStaticMethodID()
– Alex Cohn
Nov 20 at 15:42
add a comment |
1
Byte.valueOf(byte)should generally be preferred overnew Byte(byte), as the former cachesByteinstances of all possiblebytevalues.
– Michael
Nov 20 at 10:05
1
how can i use Byte.valueof() in JNI?? i try to it but nothing works. and is it possible the method, 'Byte.valueof()', replace the javaLangByteConstructor line? since this statement occur runtime error.
– KJS_999
Nov 20 at 14:00
thank you for your help, but still have a runtime error atjmethodID javaLangByteStaticValueOf = env->GetMethodID(javaLangByteClass , "valueOf", "(B)Ljava/lang/Byte;");i think that signature is problem so i change the return type,Ljava/lang/Byte, to String since it is strange that same as return type parameter type. but it didn't work..
– KJS_999
Nov 20 at 14:39
Sorry, copy/paste mistake. We needGetStaticMethodID()
– Alex Cohn
Nov 20 at 15:42
1
1
Byte.valueOf(byte) should generally be preferred over new Byte(byte), as the former caches Byte instances of all possible byte values.– Michael
Nov 20 at 10:05
Byte.valueOf(byte) should generally be preferred over new Byte(byte), as the former caches Byte instances of all possible byte values.– Michael
Nov 20 at 10:05
1
1
how can i use Byte.valueof() in JNI?? i try to it but nothing works. and is it possible the method, 'Byte.valueof()', replace the javaLangByteConstructor line? since this statement occur runtime error.
– KJS_999
Nov 20 at 14:00
how can i use Byte.valueof() in JNI?? i try to it but nothing works. and is it possible the method, 'Byte.valueof()', replace the javaLangByteConstructor line? since this statement occur runtime error.
– KJS_999
Nov 20 at 14:00
thank you for your help, but still have a runtime error at
jmethodID javaLangByteStaticValueOf = env->GetMethodID(javaLangByteClass , "valueOf", "(B)Ljava/lang/Byte;"); i think that signature is problem so i change the return type,Ljava/lang/Byte, to String since it is strange that same as return type parameter type. but it didn't work..– KJS_999
Nov 20 at 14:39
thank you for your help, but still have a runtime error at
jmethodID javaLangByteStaticValueOf = env->GetMethodID(javaLangByteClass , "valueOf", "(B)Ljava/lang/Byte;"); i think that signature is problem so i change the return type,Ljava/lang/Byte, to String since it is strange that same as return type parameter type. but it didn't work..– KJS_999
Nov 20 at 14:39
Sorry, copy/paste mistake. We need
GetStaticMethodID()– Alex Cohn
Nov 20 at 15:42
Sorry, copy/paste mistake. We need
GetStaticMethodID()– Alex Cohn
Nov 20 at 15:42
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387465%2fchange-byte-values-in-jni-with-android-studio%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You can't access Java arrays like that from native code. In this case you need to use the
GetObjectArrayElementfunction. Although I don't really see what the purpose ofbyte_arrayandobject_arrayis. Why don't you just pass each oftempSim's values toByte.valueOfinstead?– Michael
Nov 20 at 7:03
Note:
NewByteArrayis for a primative array (Java:byte; JNI:jbytearray), not an object array (Java:Byte).– Tom Blodget
Nov 25 at 3:46