2017-02-21 34 views
1

這裏是目前我在做什麼:苔絲,兩者不會初始化,即使有正確的權限

  • 苔絲二是建立在我的Android項目

File directory with tess-two installed

  • 我有我的主應用程序(不是tess-two AndroidManifest.xml)的AndroidManifest.xml中指定的權限:

Permissions

  • 我還要檢查權限,明確地在我的代碼:當我運行應用程序

    private void initTess() 
    { 
        // Check we have the eng.traineddata file in the correct place 
        mTessDataPath = getFilesDir() + "/tesseract/"; 
        checkTessFile(new File(mTessDataPath + "tessdata/")); 
    
        // Initialise TessBaseAPI 
        mTess = new TessBaseAPI(); 
        mTess.init(mTessDataPath, "eng"); 
    } 
    
    private void checkTessFile(File dir) 
    { 
        // Check if directory already exists 
        if (dir.exists()) 
        { 
         // Check if file already exists 
         String dataFilePath = mTessDataPath + "tessdata/eng.traineddata"; 
         File datafile = new File(dataFilePath); 
    
         if (!datafile.exists()) 
         { 
          // If file doesn't exist, copy it over from assets folder 
          copyTessFiles(); 
         } 
        } 
        else 
        { 
         if (dir.mkdirs()) 
         { 
          // If directory doesn't exist, but we can create it, copy file from assets folder 
          copyTessFiles(); 
         } 
        } 
    } 
    
    private void copyTessFiles() 
    { 
        try 
        { 
         // Location we want the file to be at 
         String filepath = mTessDataPath + "tessdata/eng.traineddata"; 
    
         // Get access to AssetManager 
         AssetManager assetManager = getAssets(); 
    
         // Open byte streams for reading/writing 
         InputStream instream = assetManager.open("tessdata/eng.traineddata"); 
         OutputStream outstream = new FileOutputStream(filepath); 
    
         // Copy the file to the location specified by filepath 
         byte[] buffer = new byte[1024]; 
         int read; 
         while ((read = instream.read(buffer)) != -1) 
         { 
          outstream.write(buffer, 0, read); 
         } 
         outstream.flush(); 
         outstream.close(); 
         instream.close(); 
        } 
        catch (FileNotFoundException e) 
        { 
         e.printStackTrace(); 
        } 
        catch (IOException e) 
        { 
         e.printStackTrace(); 
        } 
    } 
    
    @Override 
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) 
    { 
        switch (requestCode) 
        { 
         case REQUEST_EXTERNAL_STORAGE: 
         { 
          if (grantResults.length > 0 
            && grantResults[0] == PackageManager.PERMISSION_GRANTED) 
          { 
           // Initialise Tesseract API 
           initTess(); 
          } 
    
          return; 
         } 
        } 
    } 
    

int readPermission = ActivityCompat.checkSelfPermission(this, READ_EXTERNAL_STORAGE); 
int writePermission = ActivityCompat.checkSelfPermission(this, WRITE_EXTERNAL_STORAGE); 

// Check we have both read and write permissions 
if (readPermission != PackageManager.PERMISSION_GRANTED 
     || writePermission != PackageManager.PERMISSION_GRANTED) 
{ 
    // We don't have permission so prompt the user 
    ActivityCompat.requestPermissions(
      this, 
      new String[] {READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE}, 
      REQUEST_EXTERNAL_STORAGE 
    ); 
} 
else 
{ 
    Log.d(TAG, "Read and write external permissions granted"); 
    initTess(); 
} 
  • 嘗試初始化TessBaseAPI ,我在我的中得到以下錯誤日誌:

    E/Tesseract(native): Could not initialize Tesseract API with language=eng! 
    

    我不知道在哪裏,從這裏走,所以任何幫助或提醒會非常感激,謝謝:)

  • +0

    你在哪裏處理權限的onRequestPermissionsResult方法? –

    +0

    我已經編輯了這個問題來顯示這個問題,並且如果我們已經擁有兩個權限(我都這麼做),將調用添加到initTess()方法中。 – TinyDancer

    回答