Ask Your Question
1

Convert Bitmap to Mat for JNI interface

asked 2012-11-24 01:49:26 -0600

Haris gravatar image

updated 2012-11-24 02:01:45 -0600

Hi all I need to pass my bitmap image JNI for some image manipulation. For that first I convert bitmap to byte array then passed to JNI and then converted to Mat, but i am getting distorted image at the JNI side. My code look like below.

   //Java part
Bitmap mBitmap = Bitmap.createBitmap(previewWidtd, previewHeight, Bitmap.Config.ARGB_8888);

   //

ByteArrayOutputStream src_stream = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, src_stream);
byte[] src_array = src_stream.toByteArray();
int[] src_mRGBA=new int[240*320];
WatershedSegmentation(240,320,src_array,,src_mRGBA); // JNI call


//JNI part
JNIEXPORT jintArray JNICALL Java_com_measure_sizemesurment2_MyView_WatershedSegmentation(
                JNIEnv* env, jobject thiz, jint width, jint height, jbyteArray s_yuv,jintArray s_bgra) {

        jbyte* _s_yuv = env->GetByteArrayElements(s_yuv, 0);
        jint* _s_bgra = env->GetIntArrayElements(s_bgra, 0);

        Mat sorcemyuv(height + height / 2, width, CV_8UC1, (unsigned char *) _s_yuv);
        Mat source(height, width, CV_8UC4, (unsigned char *) _s_bgra);

        cvtColor(sorcemyuv, source, CV_YUV420sp2BGR, 4);
        imwrite( "/sdcard/sorce.jpg", source );

    env->ReleaseIntArrayElements(s_bgra, _s_bgra, 0);
    env->ReleaseByteArrayElements(s_yuv, _s_yuv, 0);
  }

Thanks in advance...

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
3

answered 2012-11-24 10:23:02 -0600

You can use Utils.bitmapToMat function to convert you bitmap to OpenCV Mat on Java size directly. Then you can pass Mat.nativeObj to your jni function. Mat.nativeObj is a pointer to native C++ Mat Object. You get something like that:

// Java
Core.Mat tmpMat = new Mat();
Utils.bitmapToMat(srcBitmap);
Foo(tmpMat);

// JNI
JNIEXPORT void JNICALL Java_com_my_class_foo(
                JNIEnv* env, jobject thiz, jlong in, jlong out)
{
    cv::Mat* inMat = (cv::Mat*)in;
    cv::Mat* outMat = (cv::Mat*)out;
    // processing
    process_frame(*inMat, *outMat);
}
edit flag offensive delete link more

Comments

Hi Haris I saw you post,and I am also trying to pass bitmap to jni,when I am passing by converting into byte array I am getting distort image as you. Can you please help me how did you send bitmap to jni interface.

Nidhi Gondhia gravatar imageNidhi Gondhia ( 2013-06-29 01:00:45 -0600 )edit

I had to also add this: Foo(tmpMat.nativeObj); instead of just Foo(tmpMat); assuming one Mat parameter (above there are two in JNI c++ function, but only one in Java code), and the Java function header being: private static native void foo(long mat); then.

zigah gravatar imagezigah ( 2013-07-25 07:30:25 -0600 )edit

Hi this is what I done, int[] mRGBA=new int[previewWidtd * previewHeight]; // will be on onPreviewStarted method int[] rgba = mRGBA; byte[] mFrame; // this should contain your frame to process Foo(int frame_width, int frame_height , data, rgba); //call the jni At the jni side JNIEXPORT jintArray JNICALL Java_package_name_Foo(JNIEnv* env, jobject thiz, jint width, jint height, jbyteArray yuv,jintArray bgra) { jbyte* _yuv = env->GetByteArrayElements(yuv, 0); jint* _bgra = env->GetIntArrayElements(bgra, 0); Get to mat like this Mat myuv(height + height / 2, width, CV_8UC1, (unsigned char *) _yuv); Mat mbgra(height, width, CV_8UC4, (unsigned char *) _bgra); Mat mgray(height, width, CV_8UC1, (unsigned char *) _yuv); cvtColor(myuv, mbgra, CV_YUV420sp2BGR, 4);

Haris gravatar imageHaris ( 2013-07-27 01:40:54 -0600 )edit
0

answered 2014-10-13 02:43:52 -0600

panan160 gravatar image

you can try this :http://blog.csdn.net/panan160/article/details/23828591 just read onClick() and Java_com_pan_test_ImageProc_grayProc() method

edit flag offensive delete link more

Question Tools

Stats

Asked: 2012-11-24 01:49:26 -0600

Seen: 7,752 times

Last updated: Oct 13 '14