mirror of https://github.com/opencv/opencv.git
Open Source Computer Vision Library
https://opencv.org/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
2.8 KiB
131 lines
2.8 KiB
14 years ago
|
#include "image_pool.h"
|
||
|
|
||
|
#include "yuv420sp2rgb.h"
|
||
|
|
||
14 years ago
|
#include "android_logger.h"
|
||
|
|
||
14 years ago
|
#include <opencv2/imgproc/imgproc.hpp>
|
||
14 years ago
|
|
||
|
#include <cstdlib>
|
||
14 years ago
|
#include <jni.h>
|
||
14 years ago
|
#ifdef __cplusplus
|
||
|
extern "C"
|
||
|
{
|
||
|
#endif
|
||
|
|
||
|
JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved);
|
||
|
//
|
||
|
//JNIEXPORT jobject JNICALL Java_com_opencv_jni_opencvJNI_getBitmapBuffer(
|
||
|
// JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_);
|
||
14 years ago
|
|
||
14 years ago
|
|
||
|
JNIEXPORT void JNICALL Java_com_opencv_jni_opencvJNI_addYUVtoPool(JNIEnv *, jclass, jlong, jobject, jbyteArray, jint,
|
||
|
jint, jint, jboolean);
|
||
|
|
||
|
#ifdef __cplusplus
|
||
|
}
|
||
|
#endif
|
||
|
|
||
|
using namespace cv;
|
||
14 years ago
|
|
||
|
JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved)
|
||
|
{
|
||
14 years ago
|
JNIEnv *env;
|
||
|
LOGI("JNI_OnLoad called for opencv");
|
||
|
return JNI_VERSION_1_4;
|
||
14 years ago
|
}
|
||
14 years ago
|
|
||
14 years ago
|
JNIEXPORT void JNICALL Java_com_opencv_jni_opencvJNI_addYUVtoPool(JNIEnv * env, jclass thiz, jlong ppool,
|
||
14 years ago
|
jobject _jpool, jbyteArray jbuffer, jint jidx,
|
||
|
jint jwidth, jint jheight, jboolean jgrey)
|
||
14 years ago
|
{
|
||
14 years ago
|
int buff_height = jheight + (jheight / 2);
|
||
|
Size buff_size(jwidth, buff_height);
|
||
|
image_pool *pool = (image_pool *)ppool;
|
||
14 years ago
|
|
||
14 years ago
|
Mat mat = pool->getYUV(jidx);
|
||
14 years ago
|
//create is smart and only copies if the buffer size is different
|
||
|
mat.create(buff_size, CV_8UC1);
|
||
14 years ago
|
{
|
||
14 years ago
|
uchar* buff = mat.ptr<uchar> (0);
|
||
|
jsize sz = env->GetArrayLength(jbuffer);
|
||
|
//http://elliotth.blogspot.com/2007/03/optimizing-jni-array-access.html
|
||
|
env->GetByteArrayRegion(jbuffer, 0, sz, (jbyte*)buff);
|
||
14 years ago
|
}
|
||
|
pool->addYUVMat(jidx, mat);
|
||
14 years ago
|
|
||
14 years ago
|
Mat color;
|
||
14 years ago
|
if (jgrey)
|
||
|
{
|
||
|
Mat grey = pool->getGrey(jidx);
|
||
|
color = grey;
|
||
|
}
|
||
14 years ago
|
else
|
||
|
{
|
||
|
color = pool->getImage(jidx);
|
||
|
pool->convertYUVtoColor(jidx, color);
|
||
|
}
|
||
14 years ago
|
pool->addImage(jidx, color);
|
||
14 years ago
|
}
|
||
|
|
||
14 years ago
|
image_pool::image_pool()
|
||
|
{
|
||
14 years ago
|
|
||
|
}
|
||
|
|
||
14 years ago
|
image_pool::~image_pool()
|
||
|
{
|
||
14 years ago
|
|
||
14 years ago
|
}
|
||
|
|
||
14 years ago
|
Mat image_pool::getImage(int i)
|
||
|
{
|
||
|
return imagesmap[i];
|
||
14 years ago
|
}
|
||
14 years ago
|
Mat image_pool::getGrey(int i)
|
||
|
{
|
||
|
Mat tm = yuvImagesMap[i];
|
||
|
if (tm.empty())
|
||
|
return tm;
|
||
14 years ago
|
return tm(Range(0, tm.rows * (2.0f / 3)), Range::all());
|
||
14 years ago
|
}
|
||
14 years ago
|
Mat image_pool::getYUV(int i)
|
||
|
{
|
||
|
return yuvImagesMap[i];
|
||
14 years ago
|
}
|
||
14 years ago
|
void image_pool::addYUVMat(int i, Mat mat)
|
||
|
{
|
||
|
yuvImagesMap[i] = mat;
|
||
14 years ago
|
}
|
||
14 years ago
|
void image_pool::addImage(int i, Mat mat)
|
||
|
{
|
||
|
imagesmap[i] = mat;
|
||
14 years ago
|
}
|
||
|
|
||
14 years ago
|
void image_pool::convertYUVtoColor(int i, cv::Mat& out)
|
||
|
{
|
||
|
Mat yuv = getYUV(i);
|
||
|
if (yuv.empty())
|
||
|
return;
|
||
|
int width = yuv.cols;
|
||
|
int height = yuv.rows * (2.0f / 3);
|
||
|
out.create(height, width, CV_8UC3);
|
||
|
const unsigned char* buff = yuv.ptr<unsigned char> (0);
|
||
|
unsigned char* out_buff = out.ptr<unsigned char> (0);
|
||
|
color_convert_common(buff, buff + width * height, width, height, out_buff, false);
|
||
|
}
|
||
14 years ago
|
|
||
|
void copyMatToBuffer(char* buffer, const cv::Mat& mat)
|
||
|
{
|
||
|
memcpy(buffer, mat.data, mat.rows * mat.cols * mat.step1());
|
||
|
}
|
||
|
void copyBufferToMat(cv::Mat& mat, const char* buffer)
|
||
|
{
|
||
|
memcpy(mat.data, buffer, mat.rows * mat.cols * mat.step1());
|
||
|
}
|
||
|
|
||
|
void RGB2BGR(const Mat& in, Mat& out)
|
||
|
{
|
||
|
cvtColor(in, out, CV_RGB2BGR);
|
||
|
}
|