将地图转换为位图

时间:2015-01-20 16:31:26

标签: android opencv bitmap mat

我需要在Bitmap对象中转换Mat对象。这是我的代码:

public void convertor(Mat tmp1){
    Mat tmp = new Mat(tmp1.size(), CvType.CV_8UC1);
    Imgproc.cvtColor(tmp1, tmp, Imgproc.COLOR_RGB2BGRA);
    Bitmap bp = Bitmap.createBitmap(tmp.rows(), tmp.cols(), Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(tmp, bp);}

这就是错误:

01-20 17:23:56.315: E/cv::error()(17783): OpenCV Error: Assertion failed (src.dims == 2 && info.height == (uint32_t)src.rows && info.width == (uint32_t)src.cols) in void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean), file /home/reports/ci/slave_desktop/50-SDK/opencv/modules/java/generator/src/cpp/utils.cpp, line 97
01-20 17:23:56.325: E/org.opencv.android.Utils(17783): nMatToBitmap catched cv::Exception: /home/reports/ci/slave_desktop/50-SDK/opencv/modules/java/generator/src/cpp/utils.cpp:97: error: (-215) src.dims == 2 && info.height == (uint32_t)src.rows && info.width == (uint32_t)src.cols in function void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean)
01-20 17:23:56.325: E/AndroidRuntime(17783): FATAL EXCEPTION: Thread-3783
01-20 17:23:56.325: E/AndroidRuntime(17783): Process: com.example.motionsnap, PID: 17783
01-20 17:23:56.325: E/AndroidRuntime(17783): CvException [org.opencv.core.CvException: /home/reports/ci/slave_desktop/50-SDK/opencv/modules/java/generator/src/cpp/utils.cpp:97: error: (-215) src.dims == 2 && info.height == (uint32_t)src.rows && info.width == (uint32_t)src.cols in function void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean)
01-20 17:23:56.325: E/AndroidRuntime(17783): ]

我该如何解决这个问题?没有使用matToBitmap还有另一种方法吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

固定。必须颠倒位图大小的参数。

public Bitmap createBitmapfromMat(Mat snap){
    Bitmap bp = Bitmap.createBitmap(snap.cols(), snap.rows(), Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(snap, bp);
    return bp;
}