[postlink]
https://the-best-way-of-life-is-islam.blogspot.com/2015/07/jni-bitmap-from-unsigned-char-always.html
[/postlink]
I would like to pass an image (via jni) from C++ to an android application. I am starting from an unsigned char* array. This array is not corrupted whatsoever; I am even able to save it into a ppm file and display it properly on my laptop.
Then, I convert it to a jByteArray by using this function:
jbyteArray imgByte=as_byte_array(env,imgRaw,img.getRawImageSize());
...
jbyteArray as_byte_array(JNIEnv *env, unsigned char* buf, int len) {
jbyteArray array = env->NewByteArray (len);
env->SetByteArrayRegion (array, 0, len, reinterpret_cast<jbyte*>(buf));
return array;
}
Afterward, I send this jByteArray to the java side. This variable has been properly populated, as I can see by printing its hex values on LogCat:
07-01 18:02:45.941 7017-8238/com.myapp.myapp W/C++ side﹕ 798a95798b9677889371838d6b7d8664757e5d6e7860717b5e...
07-01 18:02:46.941 7017-8238/com.myapp.myapp W/Java side﹕ 798a95798b9677889371838d6b7d8664757e5d6e7860717b5e...
The final step would be to show it on an ImageView. To this end, I do the following code (taken from another question on SO):
public void setImageViewWithByteArray(final ImageView view, byte[] data) {
final Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (bitmap==null) {
Log.e(TAG,"Bitmap is NULL!");
view.setImageResource(R.drawable.abc_btn_radio_material);
}
else {
view.setImageBitmap(bitmap);
}
}
});
}
but the bitmap variable is always null. What am I doing wrong? Is there a way to debug decodeByteArray?
Enregistrer un commentaire