| 41 | } // anonymous |
| 42 | |
| 43 | void |
| 44 | BitmapToMat(JNIEnv *env, jobject bitmap, cv::Mat &mat) { |
| 45 | AndroidBitmapInfo bmInfo; |
| 46 | if (AndroidBitmap_getInfo(env, bitmap, &bmInfo) != ANDROID_BITMAP_RESULT_SUCCESS) { |
| 47 | LOGE("nBitmapToMat: get bitmap info error"); |
| 48 | return; |
| 49 | }; |
| 50 | |
| 51 | void *pixels = nullptr; |
| 52 | if (AndroidBitmap_lockPixels(env, bitmap, &pixels) == ANDROID_BITMAP_RESUT_SUCCESS) { |
| 53 | AutoUnlockPixels autounlock(env, bitmap); |
| 54 | |
| 55 | if (bmInfo.format == ANDROID_BITMAP_FORMAT_RGBA_8888) { |
| 56 | LOGE("nBitmapToMat: RGB_8888 -> CV_8UC4"); |
| 57 | cv::Mat tmp(bmInfo.height, bmInfo.width, CV_8UC4, pixels); |
| 58 | tmp.copyTo(mat); |
| 59 | } else { |
| 60 | // info.format == ANDROID_BITMAP_FORMAT_RGB_565 |
| 61 | LOGE("nBitmapToMat: RGB_565 -> CV_8UC4"); |
| 62 | cv::Mat tmp(bmInfo.height, bmInfo.width, CV_8UC4, pixels); |
| 63 | tmp.copyTo(mat); |
| 64 | } |
| 65 | } else { |
| 66 | throw std::runtime_error("Failed to read bitmap's data"); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void ThrowJavaException(JNIEnv *env, const char *message) { |
| 71 | static jclass jcls = env->FindClass("java/lang/RuntimeException"); |