JNI和c++的问题

Problems with JNI and C++

本文关键字:问题 c++ JNI      更新时间:2023-10-16

我在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,但函数检测不正确工作,因为当我使用这个如果返回0.

我看只是个简单的打字错误。

你的包是"com.example"。telo3"
你的类是"Process"
函数是"initializar_native()"

c++函数必须声明为

JNIEXPORT jlong JNICALL Java_com_example_telo3_Process_inicializar_nativo(...)

您已将其声明为

JNIEXPORT jlong JNICALL Java_com_example_telo3_Process_inicializar_1nativo(...)

你得到一个不满意的链接异常或类似的吗?