2013-05-13 57 views
0

我做了一个Opencv的应用程序窗口,现在我正在使用JNI将此代码转换为Android,但我有一些问题。 具体而言,我的本地代码不会做任何事情。JNI,C++问题

这是我的Java类,我定义我的本地方法:

package com.example.telo3; 

import org.opencv.core.Mat; 

public class Process { 

    static { 
     System.loadLibrary("nativo"); 
    } 

    public Process(){ 

     dir=inicializar_nativo(); 
    } 

    public void Procesar(Mat framedetect, Mat framedraw){ 

     procesar_nativo(dir,framedetect.getNativeObjAddr(),framedraw.getNativeObjAddr()); 
    } 


    private long dir; 
    private static native long inicializar_nativo(); 
    private static native void procesar_nativo(long thiz, long framedetect, long framedraw); 

}

这是我的JNI代码:

#include "nativo.h" 
#include <opencv2/objdetect/objdetect.hpp> 
#include <opencv2/highgui/highgui.hpp> 
#include <opencv2/imgproc/imgproc.hpp> 
#include "opencv2/video/tracking.hpp" 

#include <iostream> 
#include <stdio.h> 
#include "FaceDetector.h" 
#include "Draw.h" 
#include "Almacena.h" 
#include "Runnable.h" 



using namespace std; 
using namespace cv; 

#include <android/log.h> 

#define LOG_TAG "NATIVO" 
#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) 

struct variables { 
    Almacena almacena; 
    Draw draw; 
    FaceDetector face_detector; 
}; 

JNIEXPORT jlong JNICALL Java_com_example_telo3_Process_inicializar_1nativo(
     JNIEnv *, jobject) { 

    long dir = (long) new variables(); 

    return (dir); 


} 

JNIEXPORT void JNICALL Java_com_example_telo3_Process_procesar_1nativo(JNIEnv *, 
     jobject, jlong dir, jlong framedetect, jlong framedraw) { 



Mat* telo =(Mat*)framedetect; 
Mat* telo2= (Mat*)framedraw; 

((variables*)dir)->almacena = ((variables*)dir)->face_detector.Detect(*telo); 


//almacena = face_detector.Detect(frame_gray); 


((variables*)dir)->draw.Dibujar(*telo2,((variables*)dir)->almacena); 


//frame_capturado = draw.Dibujar(frame_capturado, almacena); 



if((((variables*)dir)->almacena.get_faces()).size() ==0){ 

    LOGD("no detecto caras"); 
} 


} 

我认为我正确使用JNI,但功能检测不正常,因为当我使用这个,如果返回0.

回答

0

framedetect 0?我做了一个基于这个测试的应用程序,它工作得很好(就是说,jlong​​被转换成和从罚款,所以这不应该是问题)。

尝试使用ndk-gdb捕捉错误以更好地理解问题。附加到进程可能是一个问题,但如果它立即崩溃,那么我喜欢在崩溃前在java端放置断点,使用调试器在那里暂停执行,附加ndk-gdb并继续让java进程继续。

另外我建议使用reinterpret_cast<jlong>reinterpret_cast<variables*>而不是C风格转换,并将转换结果保存到单独的变量以避免始终投射!更清洁的代码