2010-09-22 03:47:11 +02:00
|
|
|
#include "image_pool.h"
|
|
|
|
|
|
|
|
#include "yuv420sp2rgb.h"
|
|
|
|
|
|
|
|
#include <android/log.h>
|
|
|
|
#include <opencv2/imgproc/imgproc.hpp>
|
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
using namespace cv;
|
|
|
|
|
2010-09-26 20:12:42 +02:00
|
|
|
#define LOG_TAG "libandroid-opencv"
|
|
|
|
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
|
|
|
|
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
|
|
|
|
|
|
|
|
JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved)
|
|
|
|
{
|
2010-11-27 08:59:22 +01:00
|
|
|
JNIEnv *env;
|
|
|
|
LOGI("JNI_OnLoad called for opencv");
|
|
|
|
return JNI_VERSION_1_4;
|
2010-09-26 20:12:42 +02:00
|
|
|
}
|
2010-09-22 03:47:11 +02:00
|
|
|
|
|
|
|
JNIEXPORT void JNICALL Java_com_opencv_jni_opencvJNI_addYUVtoPool(JNIEnv * env,
|
2010-11-27 08:59:22 +01:00
|
|
|
jclass thiz, jlong ppool, jobject _jpool, jbyteArray jbuffer,
|
|
|
|
jint jidx, jint jwidth, jint jheight, jboolean jgrey)
|
|
|
|
{
|
|
|
|
int buff_height = jheight + (jheight/2);
|
|
|
|
Size buff_size(jwidth,buff_height);
|
|
|
|
image_pool *pool = (image_pool *) ppool;
|
2010-09-22 03:47:11 +02:00
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
Mat mat = pool->getYUV(jidx);
|
2010-09-22 03:47:11 +02:00
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
if (mat.empty() || mat.size() != buff_size )
|
|
|
|
{
|
|
|
|
mat.create(buff_size, CV_8UC1);
|
|
|
|
}
|
2010-09-22 03:47:11 +02:00
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
jsize sz = env->GetArrayLength(jbuffer);
|
|
|
|
uchar* buff = mat.ptr<uchar> (0);
|
2010-09-22 03:47:11 +02:00
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
env->GetByteArrayRegion(jbuffer, 0, sz, (jbyte*) buff);
|
2010-09-22 03:47:11 +02:00
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
pool->addYUVMat(jidx, mat);
|
2010-09-22 03:47:11 +02:00
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
Mat color = pool->getImage(jidx);
|
2010-09-22 03:47:11 +02:00
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
if (!jgrey)
|
|
|
|
{
|
2010-09-22 03:47:11 +02:00
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
if (color.cols != jwidth || color.rows != jheight || color.channels() != 3)
|
|
|
|
{
|
|
|
|
color.create(jheight, jwidth, CV_8UC3);
|
|
|
|
}
|
|
|
|
//doesn't work unfortunately..
|
|
|
|
//TODO cvtColor(mat,color, CV_YCrCb2RGB);
|
|
|
|
color_convert_common(buff, buff + jwidth * jheight, jwidth, jheight,
|
|
|
|
color.ptr<uchar> (0), false);
|
|
|
|
}
|
2010-09-22 03:47:11 +02:00
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
if (jgrey)
|
|
|
|
{
|
|
|
|
Mat grey = pool->getGrey(jidx);
|
|
|
|
color = grey;
|
|
|
|
}
|
2010-09-22 03:47:11 +02:00
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
pool->addImage(jidx, color);
|
2010-09-22 03:47:11 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
image_pool::image_pool()
|
|
|
|
{
|
2010-09-22 03:47:11 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
image_pool::~image_pool()
|
|
|
|
{
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, "image_pool", "destructor called");
|
2010-09-22 03:47:11 +02:00
|
|
|
}
|
|
|
|
|
2010-11-27 08:59:22 +01:00
|
|
|
Mat image_pool::getImage(int i)
|
|
|
|
{
|
|
|
|
return imagesmap[i];
|
2010-09-22 03:47:11 +02:00
|
|
|
}
|
2010-11-27 08:59:22 +01:00
|
|
|
Mat image_pool::getGrey(int i)
|
|
|
|
{
|
|
|
|
Mat tm = yuvImagesMap[i];
|
|
|
|
if (tm.empty())
|
|
|
|
return tm;
|
|
|
|
return tm(Range(0, tm.rows * (2.0f/3)), Range::all());
|
2010-09-22 03:47:11 +02:00
|
|
|
}
|
2010-11-27 08:59:22 +01:00
|
|
|
Mat image_pool::getYUV(int i)
|
|
|
|
{
|
|
|
|
return yuvImagesMap[i];
|
2010-09-22 03:47:11 +02:00
|
|
|
}
|
2010-11-27 08:59:22 +01:00
|
|
|
void image_pool::addYUVMat(int i, Mat mat)
|
|
|
|
{
|
|
|
|
yuvImagesMap[i] = mat;
|
2010-09-22 03:47:11 +02:00
|
|
|
}
|
2010-11-27 08:59:22 +01:00
|
|
|
void image_pool::addImage(int i, Mat mat)
|
|
|
|
{
|
|
|
|
imagesmap[i] = mat;
|
2010-09-22 03:47:11 +02:00
|
|
|
}
|
|
|
|
|