java - JNI 检测到应用程序错误 : use of deleted weak global reference

标签 java android c++ opencv

JNI 文件看起来像,

#include <jni.h>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/features2d.hpp>
#include <opencv2/face.hpp>
#include <opencv2/core/utility.hpp>
#include <vector>
#include <opencv2/highgui.hpp>

#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>

#include <fstream>
#include <sstream>
#include <map>

#include <android/log.h>

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

using namespace std;
using namespace cv;
using namespace cv::face;


extern "C" {

JNIEXPORT void JNICALL Java_com_opencv_test_FaceRecognizer_train
        (JNIEnv *, jobject, jlongArray, jintArray);

JNIEXPORT void JNICALL Java_com_opencv_test_FaceRecognizer_predict
        (JNIEnv *, jobject, long, jintArray, jdoubleArray);

Ptr<LBPHFaceRecognizer> model;

JNIEXPORT void JNICALL Java_com_opencv_test_FaceRecognizer_train
        (JNIEnv *env, jobject thisObj, jlongArray images1, jintArray labels1) {
    model = LBPHFaceRecognizer::create();

    jsize length = (*env).GetArrayLength(images1);
    jlong *mats = (*env).GetLongArrayElements(images1, 0);
    jint *matLabels = (*env).GetIntArrayElements(labels1, 0);

    if (length > 0) {
        vector<Mat> images;
        vector<int> labels;
        for (int i = 0; i < length; i++) {
            Mat &mGr = *(Mat *) mats[i];
            images.push_back(mGr);
            labels.push_back(matLabels[i]);
            mGr.release();
        }
        model->train(images, labels);
        images.clear();
        labels.clear();
    }
    (*env).ReleaseLongArrayElements(images1, mats, 0);
    (*env).ReleaseIntArrayElements(labels1, matLabels, 0);
}

JNIEXPORT void JNICALL Java_com_opencv_test_FaceRecognizer_predict
        (JNIEnv *env, jobject thisObj, long images1, jintArray label, jdoubleArray confidence) {
    Mat &mGr = *(Mat *) images1;
    jint *labelBody = (*env).GetIntArrayElements(label, 0);
    jdouble *confidenceBody = (*env).GetDoubleArrayElements(confidence, 0);
    int &myLabel = labelBody[0];
    double &myConfidence = confidenceBody[0];
    LOGD("Prediction started in JNI");
    model->predict(mGr, myLabel, myConfidence);
    LOGD("Prediction ended in JNI");
    (*env).ReleaseIntArrayElements(label, labelBody, 0);
    (*env).ReleaseDoubleArrayElements(confidence, confidenceBody, 0);
}    
}

FaceRecognizer.java 看起来像

public class FaceRecognizer {

    private static final String TAG = FaceRecognizer.class.getSimpleName();

    public FaceRecognizer() {
    }

    public void predict(Mat mGrey, int[] label, double[] confidence) {
        predict(mGrey.getNativeObjAddr(), label, confidence);
    }

    public boolean train() {
        File root = new File(activity.getFilesDir().getAbsolutePath() + "/" + TRAIN.name() + "/");

        FilenameFilter pngFilter = new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.toLowerCase().endsWith(".jpg");

            }
        };

        File[] imageFiles = root.listFiles(pngFilter);
        long[] mats = new long[imageFiles.length];
        int[] IDs = new int[imageFiles.length];
        int counter = 0;
        for (File file : imageFiles) {
            Log.v(TAG, file.getAbsolutePath());
            Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
            if (bitmap != null) {
                Bitmap bmp32 = bitmap.copy(Bitmap.Config.ARGB_8888, true);
                Mat b = new Mat(bmp32.getWidth(), bmp32.getHeight(), CvType.CV_8UC1);
                Utils.bitmapToMat(bmp32, b);
                Imgproc.cvtColor(b, b, Imgproc.COLOR_RGB2GRAY);
                int id = Integer.parseInt(file.getAbsolutePath().split("_")[1]);
                Log.v(TAG, "id : " + id);
                mats[counter] = b.getNativeObjAddr();
                IDs[counter] = id;
                counter++;
            }
        }
        train(mats, IDs);
        return true;
    }

    public native void train(long[] mats, int[] label);

    public native void predict(long mats, int[] integer, double[] d);
}

但是当我尝试预测时,应用程序崩溃并出现以下 logcat 错误。

JNI DETECTED ERROR IN APPLICATION: use of deleted weak global reference 0xffffffff from void com.opencv_test_FaceRecognizer.predict(long, int[], double[])

最佳答案

发生这种情况是因为本地方法路径是
Java_com_opencv_test_FaceRecognizer_predict
->com.opencv.test.FaceRecognizer#predict 但是这个类路径是 com.idesign.opencvmaketest.FaceRecognizer

关于java - JNI 检测到应用程序错误 : use of deleted weak global reference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46280425/

相关文章:

java - 如何知道操作栏菜单何时展开?

Android 开发 : API to find Rss-Feeds

c++ - 关于STL中的非相等分配器

c++ - C/C++ : What does the comment "/*< */" stand for?

java - 将对象存储在静态类中的散列中是否安全,使用线程 ID 检索该特定线程的值?

java - 使用本地 jar 在 svn 中 checkin maven 项目

android - 连接相机服务失败

php - C++ proc_open 模拟

java - 自动化 Flash 应用程序

java - 初始化 EnumMap 的映射