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?










share|improve this question






















  • 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















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?










share|improve this question






















  • 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













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?










share|improve this question













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 android jni native






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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


















  • 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
















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












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);
}





share|improve this answer



















  • 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






  • 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 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













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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);
}





share|improve this answer



















  • 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






  • 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 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

















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);
}





share|improve this answer



















  • 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






  • 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 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















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);
}





share|improve this answer














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);
}






share|improve this answer














share|improve this answer



share|improve this answer








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 over new Byte(byte), as the former caches Byte instances of all possible byte values.
    – 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 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
















  • 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






  • 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 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










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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

To store a contact into the json file from server.js file using a class in NodeJS

Redirect URL with Chrome Remote Debugging Android Devices

Dieringhausen