Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Convert Bitmap to Mat over JNI interface

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

Convert Bitmap to Mat over for JNI interface

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