Android Studio制作追踪库文件

参考下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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值