Refactoring the image_pool for android, and adding some common utils for camera configuration. Also experimenting with optimization - grayscale preview is way faster than color right now.
This commit is contained in:
@@ -5,92 +5,97 @@
|
||||
#include <android/log.h>
|
||||
#include <opencv2/imgproc/imgproc.hpp>
|
||||
|
||||
using namespace cv;
|
||||
|
||||
#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)
|
||||
{
|
||||
JNIEnv *env;
|
||||
LOGI("JNI_OnLoad called for opencv");
|
||||
return JNI_VERSION_1_4;
|
||||
JNIEnv *env;
|
||||
LOGI("JNI_OnLoad called for opencv");
|
||||
return JNI_VERSION_1_4;
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL Java_com_opencv_jni_opencvJNI_addYUVtoPool(JNIEnv * env,
|
||||
jclass thiz, jlong ppool, jobject _jpool, jbyteArray jbuffer,
|
||||
jint jidx, jint jwidth, jint jheight, jboolean jgrey) {
|
||||
image_pool *pool = (image_pool *) ppool;
|
||||
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;
|
||||
|
||||
Ptr<Mat> mat = pool->getYUV(jidx);
|
||||
Mat mat = pool->getYUV(jidx);
|
||||
|
||||
if (mat.empty() || mat->cols != jwidth || mat->rows != jheight * 2) {
|
||||
//pool->deleteGrey(jidx);
|
||||
mat = new Mat(jheight * 2, jwidth, CV_8UC1);
|
||||
}
|
||||
if (mat.empty() || mat.size() != buff_size )
|
||||
{
|
||||
mat.create(buff_size, CV_8UC1);
|
||||
}
|
||||
|
||||
jsize sz = env->GetArrayLength(jbuffer);
|
||||
uchar* buff = mat->ptr<uchar> (0);
|
||||
jsize sz = env->GetArrayLength(jbuffer);
|
||||
uchar* buff = mat.ptr<uchar> (0);
|
||||
|
||||
env->GetByteArrayRegion(jbuffer, 0, sz, (jbyte*) buff);
|
||||
env->GetByteArrayRegion(jbuffer, 0, sz, (jbyte*) buff);
|
||||
|
||||
pool->addYUVMat(jidx, mat);
|
||||
Ptr<Mat> color = pool->getImage(jidx);
|
||||
if (color.empty() || color->cols != jwidth || color->rows != jheight) {
|
||||
//pool->deleteImage(jidx);
|
||||
color = new Mat(jheight, jwidth, CV_8UC3);
|
||||
}
|
||||
if (!jgrey) {
|
||||
pool->addYUVMat(jidx, mat);
|
||||
|
||||
//doesn't work unfortunately..
|
||||
//cvtColor(*mat,*color, CV_YCrCb2RGB);
|
||||
color_convert_common(buff, buff + jwidth * jheight, jwidth, jheight,
|
||||
color->ptr<uchar> (0), false);
|
||||
Mat color = pool->getImage(jidx);
|
||||
|
||||
}
|
||||
if (!jgrey)
|
||||
{
|
||||
|
||||
if (jgrey) {
|
||||
Mat grey;
|
||||
pool->getGrey(jidx, grey);
|
||||
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);
|
||||
}
|
||||
|
||||
cvtColor(grey, *color, CV_GRAY2RGB);
|
||||
if (jgrey)
|
||||
{
|
||||
Mat grey = pool->getGrey(jidx);
|
||||
color = grey;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pool->addImage(jidx, color);
|
||||
pool->addImage(jidx, color);
|
||||
|
||||
}
|
||||
|
||||
image_pool::image_pool() {
|
||||
image_pool::image_pool()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
image_pool::~image_pool() {
|
||||
__android_log_print(ANDROID_LOG_INFO, "image_pool", "destructor called");
|
||||
image_pool::~image_pool()
|
||||
{
|
||||
__android_log_print(ANDROID_LOG_INFO, "image_pool", "destructor called");
|
||||
}
|
||||
|
||||
cv::Ptr<Mat> image_pool::getImage(int i) {
|
||||
return imagesmap[i];
|
||||
Mat image_pool::getImage(int i)
|
||||
{
|
||||
return imagesmap[i];
|
||||
}
|
||||
void image_pool::getGrey(int i, Mat & grey) {
|
||||
|
||||
cv::Ptr<Mat> tm = yuvImagesMap[i];
|
||||
if (tm.empty())
|
||||
return;
|
||||
grey = (*tm)(Range(0, tm->rows / 2), Range::all());
|
||||
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());
|
||||
}
|
||||
cv::Ptr<Mat> image_pool::getYUV(int i) {
|
||||
|
||||
return yuvImagesMap[i];
|
||||
|
||||
Mat image_pool::getYUV(int i)
|
||||
{
|
||||
return yuvImagesMap[i];
|
||||
}
|
||||
void image_pool::addYUVMat(int i, cv::Ptr<Mat> mat) {
|
||||
|
||||
yuvImagesMap[i] = mat;
|
||||
|
||||
void image_pool::addYUVMat(int i, Mat mat)
|
||||
{
|
||||
yuvImagesMap[i] = mat;
|
||||
}
|
||||
void image_pool::addImage(int i, cv::Ptr<Mat> mat) {
|
||||
|
||||
imagesmap[i] = mat;
|
||||
|
||||
void image_pool::addImage(int i, Mat mat)
|
||||
{
|
||||
imagesmap[i] = mat;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user