2016-09-29 11 views
2

C++でOpenCVを使用するAndroidでアプリを作ろうとしています。私が直面している問題は、テキストファイルをネイティブ部分から開いて単語単位で読むことですが、開かないということです。私が使用していますAndroid(ndk)でC++でテキストファイルを開いて読む

JNIEXPORT jboolean JNICALL Java_com_alejandro_nativeopencv_NativeClass_initRecognizer(JNIEnv * env, jclass clazz){ 

    recognizer = Recognizer("ORB","ORB","BruteForce-Hamming"); 
    recognizer.createObject("/storage/emulated/0/TFG/Fotos/Carpeta", true); 
    recognizer.createObject("/storage/emulated/0/TFG/Fotos/Cereales", true); 

    return true; 
} 

そして、認識器:: CreateObjectメソッド:

Recognizer.cppを:

JNI方法:私のコードは次のようである

#include "headers/Recognizer.h" 
#include <fstream> 
#include <iostream> 
#include <string> 
#include <android/log.h> 

using namespace std; 
using namespace cv; 

//...other methods, constructors and stuff... 

Object Recognizer::createObject(String path, bool add) { 
    __android_log_print(ANDROID_LOG_DEBUG,"path","%s",(path + "/info.txt").c_str()); 
    ifstream file((path + "/info.txt").c_str()); 
    if (!file.is_open()){ 
     //always enters here 
     return Object(true); //null object 
    } else{ 
     //never enters here 
     String name; 
     vector < vector<KeyPoint> > keypoints; 
     vector <Mat> descriptors; 
     vector < vector<Point2f> > corners; 
     vector <String> viewNames; 
     bool easy; 

     string word; 
     int i = 0; 
     while (file >> word) 
     { 
      if(i == 0){ 

       /* Object name */ 
       name = word; 

       __android_log_print(ANDROID_LOG_DEBUG,"NOMBRE","%s",name.c_str()); 

      } else if(i == 1){ 

       /* Object easy or not */ 
       easy = (word == "true"); 
       __android_log_print(ANDROID_LOG_DEBUG, "EASY", "%d", easy); 
      } else if(i%2 == 0){ 

       /* Object image view*/ 
       keypoints.push_back(vector <KeyPoint>()); 
       descriptors.push_back(Mat()); 
       corners.push_back(vector <Point2f>(4)); 

       Mat image = imread(path + "/" + word, CV_LOAD_IMAGE_GRAYSCALE); 
       this->detector->detect(image, keypoints[(i/2)-1]); 
       this->extractor->compute(image, keypoints[(i/2)-1], descriptors[(i/2)-1]); 
       corners[(i/2)-1][0] = cvPoint(0,0); 
       corners[(i/2)-1][1] = cvPoint(image.cols,0); 
       corners[(i/2)-1][2] = cvPoint(image.cols, image.rows); 
       corners[(i/2)-1][3] = cvPoint(0, image.rows); 
       __android_log_print(ANDROID_LOG_DEBUG, "VISTA", "%d", (i/2)-1); 
      } else{ 

       /* Object name view */ 
       viewNames.push_back(word); 

       String aux = word; 
       __android_log_print(ANDROID_LOG_DEBUG, "VISTA NOMBRE", "%s", aux.c_str()); 
      } 

      i++; 
     } 
     Object obj = Object(name, keypoints, descriptors, corners, viewNames, easy); 

     if(add){ 
      this->objects.push_back(obj); 
     } 

     file.close(); 

     return obj; 
    } 
} 

問題はここにあります:

__android_log_print(ANDROID_LOG_DEBUG,"path","%s",(path + "/info.txt").c_str()); 
    ifstream file((path + "/info.txt").c_str()); 
    if (!file.is_open()){ 
     //always enters here 
     return Object(true); //null object 
    } else{ 
     //never enters here 
     ...do stuff... 
    } 

JNIメソッドでは、読みたいInfo.txtファイルがあるパスをパラメータとして渡しますが、開かない(エラーはなく、最初のifステートメントに入力するだけです)。私は最初に自分の携帯電話のSDカードにフォルダを入れてみましたが、その後は私の内部の記憶装置に入れましたが、それはまったく動作しません。私は自分の携帯電話でチェックし、私はパスが正しいことを推測:

Link to the image showing the folder path

また、私はAndroidのマニフェスト読み取りを持っているし、外部記憶許可を記述します。

ここでどこに問題があるのか​​分かりますか?

ありがとうございました!

EDIT 1:これまで

変更コード:

__android_log_print(ANDROID_LOG_DEBUG,"path","%s",(path + "/info.txt").c_str()); 
ifstream file; 
file.open((path + "/info.txt").c_str()); 
if (!file.is_open()){ 
    //always enters here 
    return Object(true); //null object 
} else{ 
    //never enters here 
    ...do stuff... 
} 

はまだ同じ問題を有します。

EDIT 2

私は取得していますログの結果は、これらが(彼らはそれがアクセスしようとしているパスに対応)は次のとおりです。

D/path: /storage/emulated/0/TFG/Fotos/Carpeta/info.txt 
D/path: /storage/emulated/0/TFG/Fotos/Cereales/info.txt 

ファイルINFO.TXTは応じてそのパスにあります私のデバイスへ:

Photo

+0

使用しているログの詳細を添付して、このメソッドを試してみてください。http:// stackoverflow。com/questions/20663467/jni-reading-a-text-file-in-c-code-and-sdkに戻る –

+0

あなたのアンドロイド携帯電話のアンドロイド版は何ですか?それがロリポップより大きい場合は、許可モデル –

+0

を実装しているか確認してください。@PavneetSinghはログの詳細を追加しました。あなたが投稿した方法を試してみてください、ありがとう:) PS:Androidのバージョンは6.0.1(マシュマロ)です – AMarquez94

答えて

2

fileがまだあなたはヘクタール、オープンされていませんfile.open()関数を呼び出すと、is_openチェックの前に現在のファイルとストリームを関連付けることができます。

file.open(); 
if (!file.is_open()){ 
     return Object(true); 
    } 

更新:他の理由は、あなたが実行時のアクセス許可モデルを持っていないことが考えられます。 official docsthis link for precise example

+1

魅力的に働いて、ありがとう! (私はあなたの答えをupvoted、しかし、私は15ポイント未満のため、表示されませんと言う、申し訳ありません)。 – AMarquez94

関連する問題