本文介绍了JNI检测到应用程序中的错误:使用已删除的弱全局引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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错误.

But when I am trying predict, app crashes with following logcat error.

推荐答案

之所以会发生这种情况,是因为本机方法路径为
Java_com_opencv_test_FaceRecognizer_predict
-> com.opencv.test.FaceRecognizer#predict,但此类路径为com.idesign.opencvmaketest.FaceRecognizer

This happens because native method path is
Java_com_opencv_test_FaceRecognizer_predict
->com.opencv.test.FaceRecognizer#predict but this class path is com.idesign.opencvmaketest.FaceRecognizer

这篇关于JNI检测到应用程序中的错误:使用已删除的弱全局引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 02:18