[英]JNI, C++ problems
我在Windows上做了一个Opencv的应用程序,现在我正在使用JNI将此代码转换为Android,但是遇到了一些问题。 具体来说,我的本机代码什么也不做。
这是我定义本地方法的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,但是功能Detect无法正常工作,因为当返回0时我使用它。
framedetect
0吗? 我基于此开发了一个测试应用程序,它运行良好(也就是说,将jlong转换为正常值,也可以将其转换为正常值,因此这不应该成为问题)。
尝试使用ndk-gdb
捕获错误,以更好地理解问题。 将它附加到进程可能会出现问题,即使它立即崩溃,在这种情况下,我想在崩溃前在Java端放置断点,使用调试器在此处暂停执行,附加ndk-gdb
并继续让Java进程运行继续。
另外,我建议使用reinterpret_cast<jlong>
和reinterpret_cast<variables*>
而不是C样式强制转换,并将该强制转换保存到单独的变量中,以避免一直转换! 更干净的代码!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.