#include <jni.h>
#include <android/log.h>
#include <android/bitmap.h>
#define LOG_TAG "test"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
/*
test for color transform
*/
JNIEXPORT void JNICALL Java_com_wzh_test_NDKTestActivity_convertToGray(JNIEnv
* env, jobject obj, jobject bitmapcolor)
{
AndroidBitmapInfo infocolor;
void* pixelscolor;
int ret;
LOGI("convertToGray");
if ((ret = AndroidBitmap_getInfo(env, bitmapcolor, &infocolor)) < 0) {
LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret);
return;
}
LOGI("color image :: width is %d; height is %d; stride is %d; format is %d;flags is %d",
infocolor.width,infocolor.height,infocolor.stride,infocolor.format,infocolor.flags);
if (infocolor.format != ANDROID_BITMAP_FORMAT_RGBA_8888) {
LOGE("Bitmap format is not RGBA_8888 !");
return;
}
if ((ret = AndroidBitmap_lockPixels(env, bitmapcolor, &pixelscolor)) < 0) {
LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);
}
Mat test(heigth,width,CV_8UC4,infocolor);
Mat bgra;
Mat bgr;
Mat rgba;
//转换成BGRA
cvtColor(test,bgra,CV_RGBA2BGRA);
//转换成BGR
cvtColor(test,bgr,CV_RGBA2BGR);
//从BGR转换回RGBA,可再将数据拷贝回bitmap
cvtColor(test,rgba,CV_BGR2RGBA);
AndroidBitmap_unlockPixels(env, bitmapcolor);
}