Skip to content

Commit

Permalink
Wrapper for fully parameterized Tesseract Init
Browse files Browse the repository at this point in the history
  • Loading branch information
timvandekamp committed Oct 14, 2013
1 parent 1a580e6 commit 908f6a2
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 4 deletions.
13 changes: 12 additions & 1 deletion Classes/Tesseract.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,25 @@

#import <Foundation/Foundation.h>

@interface Tesseract : NSObject {
extern NSString * const OcrEngineModeTesseractOnly;
extern NSString * const OcrEngineModeCubeOnly;
extern NSString * const OcrEngineModeTesseractCubeCombined;
extern NSString * const OcrEngineModeDefault;

@interface Tesseract : NSObject {
NSString* _dataPath;
NSString* _language;
NSMutableDictionary* _variables;
}

+ (NSString *)version;

- (id)initWithDataPath:(NSString *)dataPath
language:(NSString *)language
ocrEngineMode:(NSString *)mode
configFilenames:(NSArray*)configFilenames
variables:(NSDictionary*)variables
setOnlyNonDebugParams:(BOOL)setOnlyNonDebugParams;
- (id)initWithDataPath:(NSString *)dataPath language:(NSString *)language;
- (void)setVariableValue:(NSString *)value forKey:(NSString *)key;
- (void)setImage:(UIImage *)image;
Expand Down
90 changes: 87 additions & 3 deletions Classes/Tesseract.mm
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,22 @@
#import "environ.h"
#import "pix.h"

NSString * const OcrEngineModeTesseractOnly = @"OcrEngineModeTesseractOnly";
NSString * const OcrEngineModeCubeOnly = @"OcrEngineModeCubeOnly";
NSString * const OcrEngineModeTesseractCubeCombined = @"OcrEngineModeTesseractCubeCombined";
NSString * const OcrEngineModeDefault = @"OcrEngineModeDefault";

namespace tesseract {
class TessBaseAPI;
};

@interface Tesseract () {
tesseract::TessBaseAPI* _tesseract;
uint32_t* _pixels;
bool _setOnlyNonDebugParams;
tesseract::OcrEngineMode _ocrEngineMode;

NSArray* _configFilenames;
}

@end
Expand All @@ -30,12 +39,44 @@ + (NSString *)version {
return [NSString stringWithFormat:@"%s", tesseract::TessBaseAPI::Version()];
}

- (id)initWithDataPath:(NSString *)dataPath language:(NSString *)language {
- (id)initWithDataPath:(NSString *)dataPath
language:(NSString *)language
ocrEngineMode:(NSString *)mode
configFilenames:(NSArray*)configFilenames
variables:(NSDictionary*)variables
setOnlyNonDebugParams:(BOOL)setOnlyNonDebugParams {
self = [super init];
if (self) {
_dataPath = dataPath;
_language = language;
_variables = [[NSMutableDictionary alloc] init];

if (mode) {
if ([mode isEqualToString:OcrEngineModeTesseractOnly]) {
_ocrEngineMode = tesseract::OEM_TESSERACT_ONLY;
} else if ([mode isEqualToString:OcrEngineModeCubeOnly]) {
_ocrEngineMode = tesseract::OEM_CUBE_ONLY;
} else if ([mode isEqualToString:OcrEngineModeTesseractCubeCombined]) {
_ocrEngineMode = tesseract::OEM_TESSERACT_CUBE_COMBINED;
} else {
_ocrEngineMode = tesseract::OEM_DEFAULT;
}
} else {
_ocrEngineMode = tesseract::OEM_DEFAULT;
}

_configFilenames = configFilenames;

if (variables) {
_variables = [variables mutableCopyWithZone:NULL];
} else {
_variables = [[NSMutableDictionary alloc] init];
}

if (setOnlyNonDebugParams) {
_setOnlyNonDebugParams = true;
} else {
_setOnlyNonDebugParams = false;
}

[self copyDataToDocumentsDirectory];
_tesseract = new tesseract::TessBaseAPI();
Expand All @@ -48,8 +89,51 @@ - (id)initWithDataPath:(NSString *)dataPath language:(NSString *)language {
return self;
}

- (id)initWithDataPath:(NSString *)dataPath language:(NSString *)language {
return [self initWithDataPath:dataPath
language:language
ocrEngineMode:OcrEngineModeDefault
configFilenames:nil
variables:nil
setOnlyNonDebugParams:NO];
}

- (BOOL)initEngine {
int returnCode = _tesseract->Init([_dataPath UTF8String], [_language UTF8String]);
char **configs = NULL;
int configs_size = 0;
GenericVector<STRING> keys;
GenericVector<STRING> values;

if (_configFilenames && _configFilenames.count > 0) {
// completely untested
configs = (char**) malloc(_configFilenames.count * sizeof(char*));
for (; configs_size < _configFilenames.count; configs_size++) {
configs[configs_size] = (char*) malloc((((NSString*)(_configFilenames[configs_size])).length + 1) * sizeof(char));
strncpy(configs[configs_size], [_configFilenames[configs_size] UTF8String], ((NSString*)(_configFilenames[configs_size])).length + 1);
}
}

for (NSString* key in _variables) {
keys.push_back([key UTF8String]);
values.push_back([_variables[key] UTF8String]);
}

int returnCode = _tesseract->Init(_dataPath ? [_dataPath UTF8String] : NULL,
_language ? [_language UTF8String] : NULL,
_ocrEngineMode,
configs,
configs_size,
&keys,
&values,
_setOnlyNonDebugParams);
if (configs) {
// completely untested
for (int filenameIndex = 0; filenameIndex < configs_size; filenameIndex++) {
free(configs[filenameIndex]);
}
free(configs);
}

return (returnCode == 0) ? YES : NO;
}

Expand Down

0 comments on commit 908f6a2

Please sign in to comment.