2014-02-12 45 views
1

我使用下面的方法來添加的Tesseract在我的項目Tessdata沒有在文件目錄複製

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Set up the tessdata path. This is included in the application bundle 
     // but is copied to the Documents directory on the first run. 
     NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentPath = ([documentPaths count] > 0) ? [documentPaths objectAtIndex:0] : nil; 

     NSString *dataPath = [documentPath stringByAppendingPathComponent:@"tessdata"]; 
     NSFileManager *fileManager = [NSFileManager defaultManager]; 
     // If the expected store doesn't exist, copy the default store. 
     if (![fileManager fileExistsAtPath:dataPath]) { 
      // get the path to the app bundle (with the tessdata dir) 
      NSString *bundlePath = [[NSBundle mainBundle] bundlePath]; 
      NSString *tessdataPath = [bundlePath stringByAppendingPathComponent:@"tessdata"]; 
      if (tessdataPath) { 
       [fileManager copyItemAtPath:tessdataPath toPath:dataPath error:NULL]; 
      } 
     } 

     setenv("TESSDATA_PREFIX", [[documentPath stringByAppendingString:@"/"] UTF8String], 1); 

     // init the tesseract engine. 
     tesseract = new tesseract::TessBaseAPI(); 
     tesseract->Init([dataPath cStringUsingEncoding:NSUTF8StringEncoding], "eng"); 
    } 
    return self; 
} 

訓練的數據得到以下錯誤

Error opening data file /Users/frf/Library/Application Support/ 
iPhone Simulator/6.1/Applications/686F83C8-526C-47FB-9F02- 
E44FBE69F60B/Documents/tessdata/eng.traineddata 

tessdata不被複制到文檔目錄中。我該怎麼辦 ?

請建議編輯我上面的代碼......

+0

你爲什麼不檢查複製成功也許在這裏得到錯誤'[fileManager copyItemAtPath:tessdataPath toPath:dataPath error:NULL];'?錯誤對象不是爲了好玩而傳遞的,所以請使用它們! – Volker

回答

2

我假設tessdata目錄不存在。 在用戶/文檔目錄中。

+0

是的,它不會。我必須手動創建它嗎? – SwapnilPopat

+0

是的,使用'NSFileManager'你必須創建目錄,如果它不存在。所以首先檢查它是否存在,然後在必要時創建。 – Volker

1

它是因爲你的文檔文件夾不包含語言文件。您必須保存添加到文檔文件夾中的語言文件。在啓動tesseract之前保存該文件。Tesseract * tesseract = [[Tesseract alloc] initWithDataPath:@「tessdata」language:@「eng」];請參考答案here

2

這段代碼工作對我來說...

if (tessdataPath) { 

       [[NSFileManager defaultManager] createDirectoryAtPath:[documentPath stringByAppendingPathComponent:@"/tessdata"] withIntermediateDirectories:YES attributes:nil error:nil]; 
       [fileManager copyItemAtPath:tessdataPath toPath:dataPath error:NULL]; 
      } 

謝謝大家...... @沃爾克@Vaisakh