(Android OpenCV)将JNI值(浮动)传递给android活动

时间:2016-01-17 08:56:14

标签: java android c++ opencv android-ndk

以下是我的jni的完整代码:

#include <jni.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/imgproc/imgproc_c.h>
#include <opencv2/features2d/features2d.hpp>
#include <vector>

using namespace std;
using namespace cv;

extern "C" {


JNIEXPORT void JNICALL Java_com_example_alexies_objecttrackertest_UBackTrackViewer_UBackObjectTrack (JNIEnv * env , jobject ubackObject,
        jint width , jint height, jbyteArray yuv, jintArray bgra, jboolean debug)
{

jbyte *_yuv = env->GetByteArrayElements(yuv, 0);
jint *_bgra = env->GetIntArrayElements(bgra, 0);

Mat mYuv(height + height / 2, width, CV_8UC1, (unsigned char *) _yuv);
Mat mBgra(height, width, CV_8UC4, (unsigned char *) _bgra);
Mat mGray(height, width, CV_8UC1, (unsigned char *) _yuv);

CvSize size = cvSize(width, height);
IplImage *hsv_frame = cvCreateImage(size, IPL_DEPTH_8U, 3);
IplImage *hsv_frame2 = cvCreateImage(size, IPL_DEPTH_8U, 3);

IplImage *thresholded = cvCreateImage(size, IPL_DEPTH_8U, 1);

IplImage *thresholded2 = cvCreateImage(size, IPL_DEPTH_8U, 1);

IplImage img_color = mBgra;
IplImage img_gray = mGray;

//Please make attention about BGRA byte order
//ARGB stored in java as int array becomes BGRA at native level
cvtColor(mYuv, mBgra, CV_YUV420sp2BGR, 4 ) ;

// convert to HSV color-space
cvCvtColor(&img_color, hsv_frame, CV_BGR2HSV ) ;
cvCvtColor(&img_color, hsv_frame2, CV_BGR2HSV ) ;


cvInRangeS(hsv_frame, cvScalar(87, 80, 115, 0), cvScalar(137, 255, 255, 0), thresholded ) ; //blue

cvInRangeS(hsv_frame2, cvScalar(43, 65, 79, 0), cvScalar(74, 255, 255, 0), thresholded2 ) ; //green


// Memory for hough circles
CvMemStorage *storage = cvCreateMemStorage(0);
CvMemStorage *storage2 = cvCreateMemStorage(0);

// some smoothing of the image
cvSmooth( thresholded, thresholded, CV_GAUSSIAN, 9 , 9 ) ;
cvSmooth( thresholded2, thresholded2, CV_GAUSSIAN, 9 , 9 ) ;

// show thresholded
if ( debug ) cvCvtColor(thresholded, &img_color, CV_GRAY2BGR ) ;
if ( debug ) cvCvtColor(thresholded2, &img_color, CV_GRAY2BGR ) ;
// find circle pattterns
CvSeq *circles = cvHoughCircles(thresholded, storage, CV_HOUGH_GRADIENT, 1.5,
                                thresholded->height / 4, 100, 40, 15, 80);

CvSeq *circles2 = cvHoughCircles(thresholded2, storage2, CV_HOUGH_GRADIENT, 1.5,
                                 thresholded2->height / 4, 100, 40, 15, 80);
// draw found circles
//for (int i = 0; i<circles->total; i++)

//blue - yellow
for ( int i = 0; i<circles -> total &&i< 1 ; i ++ ) // max 3 circles
{


float *p = (float *) cvGetSeqElem(circles, i);

//draw blue
circle(mBgra, Point(p[0], p[1]), 3 , Scalar( 0 , 255 , 0 , 255 ) , 2 ) ;
circle(mBgra, Point(p[0], p[1]), p[2], Scalar(0, 0, 255, 255), 4 ) ;


    for ( int k = 0; k<circles3 -> total &&k< 1 ; k ++ ) // max 3 circles
    {

    float *p3 = (float *) cvGetSeqElem(circles3, k);

    //draw yellow
    circle(mBgra, Point(p3[0], p3[1]),3 , Scalar( 0 , 255 , 0 , 255 ) , 2 );
    circle(mBgra, Point(p3[0], p3[1]), p3[2], Scalar(0, 0, 255, 255),4 );


    //draw line between blue and yellow
    line(mBgra, Point(p3[0], p3[1]), Point(p[0], p[1]), Scalar(0, 255, 0, 255),2, 8);

    //distance formula.
    float dist2 = sqrt(pow(p3[1] - p[1], 2) + pow(p3[0] - p[0], 2));

    //conversion .28cm = 1px sa 9.5in
    **float conv2 = dist2 * 0.11;**



//put text
    putText(mBgra, format("blue-yellow distance: %.2f cm ", conv2), Point(50, 150), FONT_HERSHEY_SIMPLEX,1, Scalar(0 , 255 , 0 , 255), 4);


    }

}




// cleanup resources
cvReleaseMemStorage(&storage);
cvReleaseMemStorage(&storage2);
cvReleaseImage(&hsv_frame);
cvReleaseImage(&hsv_frame2);
cvReleaseImage(&thresholded);
cvReleaseImage(&thresholded2);

env->
ReleaseIntArrayElements(bgra, _bgra,
0);
env->
ReleaseByteArrayElements(yuv, _yuv,
0);
}




}

它在图像框架中查找并绘制蓝色和黄色圆圈图案,并测量它们之间的距离。我想传递距离float conv2 = dist2 * 0.11的值;到我将用于Sqlite数据库的android活动类。

0 个答案:

没有答案