简体   繁体   中英

Sending drawable as byte array to jni part of android

I am working on Image Processing with OpenCv on Android. In OpenCv examples as Tutorial 3, they are using native code, and they are sending frame data from java part to jni part as byte array. Then they are converting byte array to mat. Lastly they are processing mat variable and return this for show.

Like this, i want to send drawable to jni part. So firstly i convert drawable to bitmap, and bitmap to byte array like this;

private Bitmap my_bitmap;
private byte[] process_data;
-o-
Drawable myDrawable = getResources().getDrawable(R.drawable.test);
my_bitmap = ((BitmapDrawable) myDrawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
mybitmap.compress(CompressFormat.PNG, 100, stream);
process_data = stream.toByteArray();

Then i send this byte array to jni part then i give int array from jni and create bitmap;

int[] rgba = mRGBA;
Test(p_width, p_height, process_data, rgba); //This is jni function

Bitmap bmp = Bitmap.createBitmap(p_width, p_height, Bitmap.Config.ARGB_8888);
bmp.setPixels(rgba, 0, p_width, 0, 0, p_width, p_height);

But the return bitmap is like noise.

This is my c++ code;

JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial3_Sample3View_Test(JNIEnv* env, jobject thiz, 
jint width, jint height, jbyteArray p_data, jintArray bgra)
{

jbyte* _p_data= env->GetByteArrayElements(p_data, 0);
jint* _bgra = env->GetIntArrayElements(bgra, 0);

Mat mdata(height, width, CV_8UC4, (unsigned char *)_p_data);
Mat mbgra(height, width, CV_8UC4, (unsigned char *)_bgra);

mdata.copyTo(mbgra);

env->ReleaseIntArrayElements(bgra, _bgra, 0);
env->ReleaseByteArrayElements(p_data, _p_data, 0);
}

Probably, i make a mistake type of converting. Because i can't find any information about how many channel is my drawable after converting byte. I am waiting for your help to solve my problem.

Thanks in advance.

I solved my problem. If there are someone that faces same problem, they can solve problem like this;

Problem is on jni part of my code. We have to decode image byte array on jni part. I edited my code like this;

JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial3_Sample3View_Test(JNIEnv* env, jobject thiz, 
jint width, jint height, jbyteArray p_data, jintArray bgra)
{

jbyte* _p_data= env->GetByteArrayElements(p_data, 0);
jint* _bgra = env->GetIntArrayElements(bgra, 0);

Mat mdata(height, width, CV_8UC4, (unsigned char *)_p_data);
Mat mbgra(height, width, CV_8UC4, (unsigned char *)_bgra);


Mat tmp_mat= imdecode(mdata,1);
cvtColor(tmp_mat, mbgra, CV_RGB2RGBA);

env->ReleaseIntArrayElements(bgra, _bgra, 0);
env->ReleaseByteArrayElements(p_data, _p_data, 0);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM