Ask Your Question

Revision history [back]

Android OpenCV error when converting Mat to Android bitmap

I'm writing an application that streams video from a webcam to an android device. The streaming client is written in C++ and uses imencode to compress images so they can be sent over UDP. I have a mock "receiver", also in C++, that can receive the images and display them. I'm porting the receiver to Android, and using only the opencv java sdk (I am not writing my own JNI stuff). The code that converts the received byte array into a Mat and then a bitmap is as follows -

Mat frame = Highgui.imdecode(new MatOfByte(packet.getData()), 0);
bm = Bitmap.createBitmap(frame.cols(), frame.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(frame, bm);
im.post(new Runnable() {
    @Override
    public void run() {
        im.setImageBitmap(bm);
     }
 });

The call to Util.matToBitmap gives the following error -

OpenCV Error: Assertion failed (AndroidBitmap_lockPixels(env, bitmap, &pixels) >= 0) in void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean), file /hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/java/generator/src/cpp/utils.cpp, line 99 03-04 19:04:46.337  15034-15148/ E/org.opencv.android.Utils﹕ nMatToBitmap catched cv::Exception: /hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/java/generator/src/cpp/utils.cpp:99: error: (-215) AndroidBitmap_lockPixels(env, bitmap, &pixels) >= 0 in function void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean) 03-04 19:04:46.452  15034-15148/E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-924

I have no idea what this means or what might be causing it. Any ideas?