|
|
|
@ -21,14 +21,14 @@ extern "C" { |
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Class: org_opencv_android_Utils |
|
|
|
|
* Method: void nBitmapToMat(Bitmap b, long m_addr) |
|
|
|
|
* Method: void nBitmapToMat(Bitmap b, long m_addr, boolean unPremultiplyAlpha) |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
JNIEXPORT void JNICALL Java_org_opencv_android_Utils_nBitmapToMat |
|
|
|
|
(JNIEnv * env, jclass, jobject bitmap, jlong m_addr); |
|
|
|
|
(JNIEnv * env, jclass, jobject bitmap, jlong m_addr, jboolean needUnPremultiplyAlpha); |
|
|
|
|
|
|
|
|
|
JNIEXPORT void JNICALL Java_org_opencv_android_Utils_nBitmapToMat |
|
|
|
|
(JNIEnv * env, jclass, jobject bitmap, jlong m_addr) |
|
|
|
|
(JNIEnv * env, jclass, jobject bitmap, jlong m_addr, jboolean needUnPremultiplyAlpha) |
|
|
|
|
{ |
|
|
|
|
AndroidBitmapInfo info; |
|
|
|
|
void* pixels = 0; |
|
|
|
@ -46,7 +46,8 @@ JNIEXPORT void JNICALL Java_org_opencv_android_Utils_nBitmapToMat |
|
|
|
|
{ |
|
|
|
|
LOGD("nBitmapToMat: RGBA_8888 -> CV_8UC4"); |
|
|
|
|
Mat tmp(info.height, info.width, CV_8UC4, pixels); |
|
|
|
|
tmp.copyTo(dst); |
|
|
|
|
if(needUnPremultiplyAlpha) cvtColor(tmp, dst, COLOR_mRGBA2RGBA); |
|
|
|
|
else tmp.copyTo(dst); |
|
|
|
|
} else { |
|
|
|
|
// info.format == ANDROID_BITMAP_FORMAT_RGB_565
|
|
|
|
|
LOGD("nBitmapToMat: RGB_565 -> CV_8UC4"); |
|
|
|
@ -73,54 +74,55 @@ JNIEXPORT void JNICALL Java_org_opencv_android_Utils_nBitmapToMat |
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Class: org_opencv_android_Utils |
|
|
|
|
* Method: void nMatToBitmap(long m_addr, Bitmap b) |
|
|
|
|
* Method: void nMatToBitmap(long m_addr, Bitmap b, boolean premultiplyAlpha) |
|
|
|
|
*/ |
|
|
|
|
JNIEXPORT void JNICALL Java_org_opencv_android_Utils_nMatToBitmap |
|
|
|
|
(JNIEnv * env, jclass, jlong m_addr, jobject bitmap); |
|
|
|
|
(JNIEnv * env, jclass, jlong m_addr, jobject bitmap, jboolean needPremultiplyAlpha); |
|
|
|
|
|
|
|
|
|
JNIEXPORT void JNICALL Java_org_opencv_android_Utils_nMatToBitmap |
|
|
|
|
(JNIEnv * env, jclass, jlong m_addr, jobject bitmap) |
|
|
|
|
(JNIEnv * env, jclass, jlong m_addr, jobject bitmap, jboolean needPremultiplyAlpha) |
|
|
|
|
{ |
|
|
|
|
AndroidBitmapInfo info; |
|
|
|
|
void* pixels = 0; |
|
|
|
|
Mat& dst = *((Mat*)m_addr); |
|
|
|
|
Mat& src = *((Mat*)m_addr); |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
LOGD("nMatToBitmap"); |
|
|
|
|
CV_Assert( AndroidBitmap_getInfo(env, bitmap, &info) >= 0 ); |
|
|
|
|
CV_Assert( info.format == ANDROID_BITMAP_FORMAT_RGBA_8888 || |
|
|
|
|
info.format == ANDROID_BITMAP_FORMAT_RGB_565 ); |
|
|
|
|
CV_Assert( dst.dims == 2 && info.height == (uint32_t)dst.rows && info.width == (uint32_t)dst.cols ); |
|
|
|
|
CV_Assert( dst.type() == CV_8UC1 || dst.type() == CV_8UC3 || dst.type() == CV_8UC4 ); |
|
|
|
|
CV_Assert( src.dims == 2 && info.height == (uint32_t)src.rows && info.width == (uint32_t)src.cols ); |
|
|
|
|
CV_Assert( src.type() == CV_8UC1 || src.type() == CV_8UC3 || src.type() == CV_8UC4 ); |
|
|
|
|
CV_Assert( AndroidBitmap_lockPixels(env, bitmap, &pixels) >= 0 ); |
|
|
|
|
CV_Assert( pixels ); |
|
|
|
|
if( info.format == ANDROID_BITMAP_FORMAT_RGBA_8888 ) |
|
|
|
|
{ |
|
|
|
|
Mat tmp(info.height, info.width, CV_8UC4, pixels); |
|
|
|
|
if(dst.type() == CV_8UC1) |
|
|
|
|
if(src.type() == CV_8UC1) |
|
|
|
|
{ |
|
|
|
|
LOGD("nMatToBitmap: CV_8UC1 -> RGBA_8888"); |
|
|
|
|
cvtColor(dst, tmp, CV_GRAY2RGBA); |
|
|
|
|
} else if(dst.type() == CV_8UC3){ |
|
|
|
|
cvtColor(src, tmp, CV_GRAY2RGBA); |
|
|
|
|
} else if(src.type() == CV_8UC3){ |
|
|
|
|
LOGD("nMatToBitmap: CV_8UC3 -> RGBA_8888"); |
|
|
|
|
cvtColor(dst, tmp, CV_RGB2RGBA); |
|
|
|
|
} else if(dst.type() == CV_8UC4){ |
|
|
|
|
cvtColor(src, tmp, CV_RGB2RGBA); |
|
|
|
|
} else if(src.type() == CV_8UC4){ |
|
|
|
|
LOGD("nMatToBitmap: CV_8UC4 -> RGBA_8888"); |
|
|
|
|
dst.copyTo(tmp); |
|
|
|
|
if(needPremultiplyAlpha) cvtColor(src, tmp, COLOR_RGBA2mRGBA); |
|
|
|
|
else src.copyTo(tmp); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
// info.format == ANDROID_BITMAP_FORMAT_RGB_565
|
|
|
|
|
Mat tmp(info.height, info.width, CV_8UC2, pixels); |
|
|
|
|
if(dst.type() == CV_8UC1) |
|
|
|
|
if(src.type() == CV_8UC1) |
|
|
|
|
{ |
|
|
|
|
LOGD("nMatToBitmap: CV_8UC1 -> RGB_565"); |
|
|
|
|
cvtColor(dst, tmp, CV_GRAY2BGR565); |
|
|
|
|
} else if(dst.type() == CV_8UC3){ |
|
|
|
|
cvtColor(src, tmp, CV_GRAY2BGR565); |
|
|
|
|
} else if(src.type() == CV_8UC3){ |
|
|
|
|
LOGD("nMatToBitmap: CV_8UC3 -> RGB_565"); |
|
|
|
|
cvtColor(dst, tmp, CV_RGB2BGR565); |
|
|
|
|
} else if(dst.type() == CV_8UC4){ |
|
|
|
|
cvtColor(src, tmp, CV_RGB2BGR565); |
|
|
|
|
} else if(src.type() == CV_8UC4){ |
|
|
|
|
LOGD("nMatToBitmap: CV_8UC4 -> RGB_565"); |
|
|
|
|
cvtColor(dst, tmp, CV_RGBA2BGR565); |
|
|
|
|
cvtColor(src, tmp, CV_RGBA2BGR565); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
AndroidBitmap_unlockPixels(env, bitmap); |
|
|
|
|