如何使用 Eclipse 在 JNI 的“include”文件夹中添加 openCV 文件

Posted

技术标签:

【中文标题】如何使用 Eclipse 在 JNI 的“include”文件夹中添加 openCV 文件【英文标题】:How to add openCV files in "include" folder in JNI using Eclipse 【发布时间】:2016-01-20 22:27:57 【问题描述】:

我正在实施以下项目: https://github.com/matthill/darwinwallet 在 Eclipse 中执行 OpenCV android 项目时出现以下错误:

    jni/jni_recognizer.cpp:2:33: fatal error: opencv2/core/core.hpp: No such file or directory
 #include <opencv2/core/core.hpp>
                                 ^
compilation terminated.
make.exe: *** [obj/local/armeabi-v7a/objs/native_wallet/jni_recognizer.o] Error 1

在我的 Eclipse 中的 jni/include 文件夹中,没有 openCV 文件。只有 NDK 文件,所以,也许这就是问题所在。所以,请告诉我如何在包含文件夹中添加 openCV 文件。

这是我的识别器.cpp 文件

`

#include <jni.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <vector>
#include "NativeVision/vision.h"
#include <time.h>
#include <android/log.h>
using namespace std;
using namespace cv;
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,   "JNI_DEBUGGING", __VA_ARGS__)
double diffclock(clock_t clock1,clock_t clock2)

    double diffticks=clock2-clock1;
    double diffms=(diffticks*1000)/ CLOCKS_PER_SEC;
    return diffms;

static Ptr<ORB> detector;
static Ptr<DescriptorMatcher> descriptorMatcher;
static vector<Mat> trainImages;
static bool training_complete = false;
static vector<string> billMapping;
extern "C" 
    JNIEXPORT void JNICALL Java_com_ndu_mobile_darwinwallet_Recognizer_nvInitialize(JNIEnv* env, jobject thiz)
    
        LOGD( "Started nvInitialize" );
        detector = getQueryDetector();
        descriptorMatcher = getMatcher();
        LOGD( "Finished nvInitialize" );
    

extern "C" 
    JNIEXPORT void JNICALL Java_com_ndu_mobile_darwinwallet_Recognizer_nvResetTrainedDatabase(JNIEnv* env, jobject thiz)
    
        LOGD( "Started nvResetTrainedDatabase" );
        training_complete = false;
        descriptorMatcher = getMatcher();
        trainImages.clear();
        billMapping.clear();
        LOGD( "Finished nvResetTrainedDatabase" );
    

extern "C" 
    JNIEXPORT void JNICALL Java_com_ndu_mobile_darwinwallet_Recognizer_nvTrainImage(JNIEnv* env, jobject thiz, jstring billname, jstring billpath)
    
        //char* _imgBytes  = (char*) env->GetPrimitiveArrayCritical(imgBytes, 0);
        const char * _billpath = env->GetStringUTFChars(billpath, 0);
        const char * _billname = env->GetStringUTFChars(billname, 0);
        LOGD( "Started nvTrainImage" );
        std::ostringstream out;
        //std::ostringstream out;
        //out << " : billname: " << _billname << ": BILLPATH: " << billpath << endl;
        LOGD( out.str().c_str() );
        //LOGD( "nvTrainImage: 1" );
        //Mat mgray(1, bytelength, CV_8U, (unsigned char *)_imgBytes);
        //LOGD( "nvTrainImage: 2" );
        Mat img = imread(_billpath, 0);
        //Mat img = imread("/sdcard/wallet/us/100b/full_pic.jpg", 0);
        //LOGD( "nvTrainImage: 3" );
        Mat trainData = trainImage( img,  detector, descriptorMatcher );
        out << "nvTrainImage: " << _billpath << " (" << trainData.rows << " x " << trainData.cols << ")" << endl;
        LOGD( out.str().c_str() );
        trainImages.push_back(trainData);
        string billstr(_billname);
        billMapping.push_back(billstr);
        LOGD( "Finished nvTrainImage" );
        env->ReleaseStringUTFChars(billpath, _billpath);
        env->ReleaseStringUTFChars(billname, _billname);
        //env->ReleasePrimitiveArrayCritical(imgBytes, _imgBytes, 0);
    

extern "C" 
    JNIEXPORT void JNICALL Java_com_ndu_mobile_darwinwallet_Recognizer_nvFinalizeTraining(JNIEnv* env, jobject thiz)
    
        LOGD( "Started nvFinalizeTraining" );
        descriptorMatcher->add(trainImages);
        descriptorMatcher->train();
        training_complete = true;
        LOGD( "Finished nvFinalizeTraining" );
    

extern "C" 
    JNIEXPORT jstring JNICALL Java_com_ndu_mobile_darwinwallet_Recognizer_nvRecognize(JNIEnv* env, jobject thiz, jint width, jint height, jbyteArray yuv)
    
        jbyte* _yuv  = env->GetByteArrayElements(yuv, 0);
        //jint*  _bgra = env->GetIntArrayElements(bgra, 0);
        LOGD( "Started nvFindFeatures" );
        jstring response = env->NewStringUTF("");
        if (training_complete == true)
        
            clock_t begin;
            clock_t end;
            //Mat myuv(height + height/2, width, CV_8UC1, (unsigned char *)_yuv);
            //Mat mbgra(height, width, CV_8UC4);
            Mat mgray(height, width, CV_8UC1, (unsigned char *)_yuv);
            //Mat myuv(width, 1, CV_8U, (unsigned char *)_yuv);
            //Mat mgray = imdecode(myuv, 0);
            //Please make attention about BGRA byte order
            //ARGB stored in java as int array becomes BGRA at native level
            //cvtColor(myuv, mbgra, CV_YUV420sp2BGR, 4);
            vector<KeyPoint> v;
            //FastFeatureDetector detector(50);
            //detector.detect(mgray, v);
            float divisor = 1;
            if (height < width)
            
                divisor = (((double) height) / 240);
            
            else
            
                divisor = (((double) width) / 240);
            
            if (divisor == 0)
                divisor = 1;
            Size idealSize(width/divisor, height/divisor);
            Mat mgray_small;
            resize(mgray, mgray_small, idealSize);
            Mat descriptors;
            vector<DMatch> matches;
            begin=clock();
            //detector->detect(mgray_small, v);
            //descriptorExtractor->compute( mgray_small, v, descriptors );
            //surfStyleMatching( descriptorMatcher, descriptors, matches );
            Mat* dummy;
            //imwrite("/sdcard/wallet_debug.jpg", mgray_small );
            bool debug_on = true;
            int debug_matches[billMapping.size()];
            RecognitionResult result = recognize( mgray_small, false, dummy, detector, descriptorMatcher, billMapping,
                    debug_on, debug_matches);
            end=clock();
            std::ostringstream out;
            out << "time: " << diffclock(begin, end) << " ms | matches: " << matches.size() << endl;
            if (debug_on)
            
                for (int k = 0; k < billMapping.size(); k++)
                    out << " --" << billMapping[k] << " : " << debug_matches[k] << endl;
            
            out << "orig_width: " << width << "orig_height: " << height << endl;
            out << "divisor: " << divisor << endl;
            //LOGD( (char*) out.str().c_str());
            if (result.haswinner == false)
                out << "No winner frown emoticon" << endl;
            else
            
                out << "Big Winner!  " << result.winner << " : " << result.confidence << endl;
                std::ostringstream responsetext;
                responsetext << result.winner << "," << result.confidence;
                response = env->NewStringUTF(responsetext.str().c_str());
            
            LOGD( (char*) out.str().c_str());
            //for( size_t i = 0; i < v.size(); i++ )
            //  circle(mbgra, Point(v[i].pt.x, v[i].pt.y), 10, Scalar(0,0,255,255));
        
        LOGD( "Finished nvFindFeatures" );
        //env->ReleaseIntArrayElements(bgra, _bgra, 0);
        env->ReleaseByteArrayElements(yuv, _yuv, 0);
        return response;
    

`

这是我的 android.mk 文件:

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
OPENCV_CAMERA_MODULES:= off
#OPENCV_MK_PATH:D:\FYP\darwinwallet-master\OpenCV-2.4.9-android-sdk\sdk\native\jni
OPENCV_MK_PATH:D:\FYP\darwinwallet-master\OpenCV-2.4.9-android-sdk\sdk\native\jni\OpenCV.mk
OPENCV_LIB_TYPE:=STATIC
OPENCV_INSTALL_MODULES:=on
include $(OPENCV_MK_PATH)
#Profiler 
#-include android-ndk-profiler.mk
#include ../includeOpenCV.mk
#ifeq ("$(wildcard $(OPENCV_MK_PATH))","")
#try to load OpenCV.mk from default install location
#include $(TOOLCHAIN_PREBUILT_ROOT)/user/share/OpenCV/OpenCV.mk
#else
#include $(OPENCV_MK_PATH)
#endif
#LOCAL_C_INCLUDES:=D:\FYP\darwinwallet-master\OpenCV-2.4.9-android-sdk\sdk\native\jni\include
LOCAL_MODULE:=native_wallet
LOCAL_SRC_FILES:=jni_recognizer.cpp NativeVision\vision.cpp
LOCAL_CFLAGS=-ffast-math -O3 -funroll-loops
#LOCAL_CFLAGS=-O3 -funroll-loops
LOCAL_LDLIBS+=-llog -ldl
#Profiling
#LOCAL_CFLAGS:=-pg
#LOCAL_STATIC_LIBRARIES:=andprof
include $(BUILD_SHARED_LIBRARY)

【问题讨论】:

Android ndk giving errors while compiling in eclipse 和/或 OpenCV opencv2/core/core.hpp: No such file or directory 的可能重复项 那些链接没有解决我的问题。 【参考方案1】:

缺少一个字符

OPENCV_MK_PATH:D…

应该阅读

OPENCV_MK_PATH:=D…

【讨论】:

以上是关于如何使用 Eclipse 在 JNI 的“include”文件夹中添加 openCV 文件的主要内容,如果未能解决你的问题,请参考以下文章

如何编译jni

Eclipse:无法加载 JNI 共享库 [重复]

Eclipse JNI环境搭建及使用

eclipse JNI项目迁移android studio相关问题

Eclipse集成JNI与AndroidNDK操作

解决eclipse启动时出现“failed to load the jni shared library”