OpenCV和C ++:“无法解析变量'Mat'”

时间:2019-10-30 13:20:09

标签: android c++ opencv java-native-interface

我正在尝试使用C ++语言将OpenCV集成到Android Java本机接口中。我已经将OpenCV放在了编写C ++代码的jni文件夹内。我已经在我的HelloNative.c文件中包含了Opencv头文件。但是尝试访问Mat对象时仍然出现此错误。 “ Can't resolve variable Mat”。

我尝试过using namespace cv,但是预先声明usingnamespace时出现错误;这不是解决方案。我在下面发布我的代码,请有人看看,让我知道我做错了什么。

#include <string.h>
#include <jni.h>
#include <stdio.h>
#include <stdlib.h>
#include <android/asset_manager.h>
#include <android/asset_manager_jni.h>
#include <android/log.h>
#include <malloc.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <OpenCV/include/opencv2/opencv.hpp>
#include <OpenCV/include/opencv/cv.h>
#include <OpenCV/modules/core/include/opencv2/core.hpp>
#include <OpenCV/modules/core/include/opencv2/core/core_c.h>
#include <OpenCV/modules/core/include/opencv2/core/mat.hpp>
#include <OpenCV/modules/imgproc/include/opencv2/imgproc/imgproc.hpp>
#include <OpenCV/modules/imgproc/include/opencv2/imgproc/imgproc_c.h>
#include <OpenCV/modules/legacy/include/opencv2/legacy/legacy.hpp>
#include <OpenCV/modules/videostab/include/opencv2/videostab/motion_stabilizing.hpp>
#include <OpenCV/modules/videostab/include/opencv2/videostab/global_motion.hpp>

JNIEXPORT jint JNICALL
Java_com_example_soimporttest_MainActivity_getStringFromJNI(JNIEnv *env, jobject thisObj,
                                                            jstring imagepath) {
    const char *jnamestr = (*env)->GetStringUTFChars(env, imagepath, 0);
    int width, height, gray;
    FILE *fp = fopen(jnamestr, "rb");
    if (fp) {
        //Read .pgm file
        int scan = fscanf(fp, "P5%d%d%d", &width, &height, &gray);
        if (scan != 3 || &gray > 256 || &width > 0xfffff || &height > 0xfffff || &gray <= 1 ||
            &width < 32 || &height < 32) {
            fclose(fp);
            return 9;
        } else {
            return 0;
        }

    } else {
        return -1;
    }
}

JNIEXPORT void JNICALL
Java_com_example_soimporttest_MainActivity_displayImage(JNIEnv *env, jobject thisObj, jclass type, jlong inpAddr, jlong outAddr) {
    ::Mat &src = *(Mat*)inpAddr;
    Mat &dst = *(Mat*)outAddr;
    applyFilter(src , dst);
}

此文件中有两个函数,第一个函数getStringFromJNI运行良好。 谢谢。

1 个答案:

答案 0 :(得分:0)

经过大量研究,我终于找到了解决我问题的好方法。请遵循以下提到的教程: https://github.com/leadrien/opencv_native_androidstudio 集成时会遇到一些问题。您可以在评论中发布这些解决方案,我将指导您完成每一个。 谢谢。