参考下http://blog.youkuaiyun.com/u013547134/article/details/41009603
1,YUV->灰度
JNIEXPORT jint JNICALL Java_Tracker_Tracker_init
(JNIEnv *env, jobject , jbyteArray frame0, jint frame_w, jint frame_h, jint x, jint y, jint w, jint h)
{
jboolean isCopy;
jbyte* yuv = env->GetByteArrayElements(frame0,&isCopy);
cv::Mat frame(frame_h,frame_w,CV_8UC1,yuv,0);
2,返回jintarray:
JNIEXPORT jintArray JNICALL Java_Tracker_Tracker_update(JNIEnv * env, jobject, jbyteArray frame0 , jint frame_w, jint frame_h)
{
//KCFTrackResult result;
jintArray newArray = env->NewIntArray(6);
jint *narr = env->GetIntArrayElements(newArray, NULL);
jboolean isCopy;
jbyte* yuv = env->GetByteArrayElements(frame0,&isCopy);
cv::Mat frame(frame_h,frame_w,CV_8UC1,yuv,0);
//cvtColor(frame, frame, CV_GRAY2BGR);
cv::Rect cvrect;
cvrect = tracker.update(frame);
narr[0] = cvrect.x;
narr[1] = cvrect.y;
narr[2] = cvrect.width;
narr[3] = cvrect.height;
narr[4] = 3;
narr[5] = 1;
if(tracker._isLost == 1) narr[4] = 1;
env->ReleaseIntArrayElements(newArray, narr,NULL);
3,track.cpp:
//
// Created by kylefan on 2017/7/18.
//
#include <Tracker_Tracker.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <kcftracker.hpp>
#include "KCFTrack.h"
#include <jni.h>
#include <android/log.h>
#ifndef LOGE
#define LOGE(...) __android_log_print(ANDROID_LOG_INFO,"@",__VA_ARGS__)
#endif
//JNIEXPORT jobject JNICALL
//Java_Pano_Pano_stitchimage(JNIEnv *env, jclass,jint quality, jobjectArray bmps)
KCFTracker tracker(true, true, false, false);
// hog fixed_window multiscale lab
JNIEXPORT jint JNICALL Java_Tracker_Tracker_init
(JNIEnv *env, jobject , jbyteArray frame0, jint frame_w, jint frame_h, jint x, jint y, jint w, jint h)
{
jboolean isCopy;
jbyte* yuv = env->GetByteArrayElements(frame0,&isCopy);
cv::Mat frame(frame_h,frame_w,CV_8UC1,yuv,0);
//LOGE("System_ frame value=%d,%d,%d,%d",frame.at<unsigned char>(0,0),frame.at<unsigned char>(1,1),frame.at<unsigned char>(2,2),frame.at<unsigned char>(3,3));
//LOGE("System_ frame :%d-%d-%d-%d",frame.rows,frame.cols,frame.depth(),frame.channels());
cv::Rect cvrect;
cvrect.height = h;
cvrect.width = w;
cvrect.x = x;
cvrect.y = y;
//2,init
//LOGE("System_ tracker rect :%d-%d-%d-%d",cvrect.x,cvrect.y,cvrect.height,cvrect.width);
int sta = tracker.init( cvrect, frame );//here break
//LOGE("System_ tracker init done ");
if(sta == 1)
{
return 3;
}
else
return 1;
}
JNIEXPORT jintArray JNICALL Java_Tracker_Tracker_update(JNIEnv * env, jobject, jbyteArray frame0 , jint frame_w, jint frame_h)
{
//KCFTrackResult result;
jintArray newArray = env->NewIntArray(6);
jint *narr = env->GetIntArrayElements(newArray, NULL);
jboolean isCopy;
jbyte* yuv = env->GetByteArrayElements(frame0,&isCopy);
cv::Mat frame(frame_h,frame_w,CV_8UC1,yuv,0);
//cvtColor(frame, frame, CV_GRAY2BGR);
cv::Rect cvrect;
cvrect = tracker.update(frame);
narr[0] = cvrect.x;
narr[1] = cvrect.y;
narr[2] = cvrect.width;
narr[3] = cvrect.height;
narr[4] = 3;
narr[5] = 1;
if(tracker._isLost == 1) narr[4] = 1;
env->ReleaseIntArrayElements(newArray, narr,NULL);
//LOGE("System_ result :%d-%d-%d-%d",cvrect.x,cvrect.y,cvrect.width,cvrect.height);
return newArray;
}