forked from gali8/Tesseract-OCR-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathG8RecognitionOperation.m
99 lines (82 loc) · 3.11 KB
/
G8RecognitionOperation.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// G8RecognitionOperation.m
// Tesseract OCR iOS
//
// Created by Nikolay Volosatov on 12.12.14.
// Copyright (c) 2014 Daniele Galiotto - www.g8production.com.
// All rights reserved.
//
#import "G8RecognitionOperation.h"
#import "TesseractOCR.h"
@interface G8RecognitionOperation() <G8TesseractDelegate> {
G8Tesseract *_tesseract;
}
@property (nonatomic, assign, readwrite) CGFloat progress;
@end
@implementation G8RecognitionOperation
- (id) initWithLanguage:(NSString *)language
{
return [self initWithLanguage:language configDictionary:nil configFileNames:nil absoluteDataPath:nil engineMode:G8OCREngineModeTesseractOnly];
}
- (id)initWithLanguage:(NSString *)language
configDictionary:(NSDictionary *)configDictionary
configFileNames:(NSArray *)configFileNames
absoluteDataPath:(NSString *)absoluteDataPath
engineMode:(G8OCREngineMode)engineMode
{
self = [super init];
if (self != nil) {
_tesseract = [[G8Tesseract alloc] initWithLanguage:language
configDictionary:configDictionary
configFileNames:configFileNames
absoluteDataPath:absoluteDataPath
engineMode:engineMode];
_tesseract.delegate = self;
__weak __typeof(self) weakSelf = self;
self.completionBlock = ^{
__strong __typeof(weakSelf) strongSelf = weakSelf;
G8RecognitionOperationCallback callback = [strongSelf.recognitionCompleteBlock copy];
G8Tesseract *tesseract = strongSelf.tesseract;
if (callback != nil) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
callback(tesseract);
}];
}
};
}
return self;
}
- (void)main
{
@autoreleasepool {
// Analyzing the layout must be performed before recognition
[self.tesseract analyseLayout];
[self.tesseract recognize];
}
}
- (void)progressImageRecognitionForTesseract:(G8Tesseract *)tesseract
{
self.progress = self.tesseract.progress / 100.0f;
if (self.progressCallbackBlock != nil) {
self.progressCallbackBlock(self.tesseract);
}
if ([self.delegate respondsToSelector:@selector(progressImageRecognitionForTesseract:)]) {
[self.delegate progressImageRecognitionForTesseract:tesseract];
}
}
- (BOOL)shouldCancelImageRecognitionForTesseract:(G8Tesseract *)tesseract
{
BOOL canceled = self.isCancelled;
if (canceled == NO && [self.delegate respondsToSelector:@selector(shouldCancelImageRecognitionForTesseract:)]) {
canceled = [self.delegate shouldCancelImageRecognitionForTesseract:tesseract];
}
return canceled;
}
- (UIImage *)preprocessedImageForTesseract:(G8Tesseract *)tesseract sourceImage:(UIImage *)sourceImage
{
if ([self.delegate respondsToSelector:@selector(preprocessedImageForTesseract:sourceImage:)]) {
return [self.delegate preprocessedImageForTesseract:tesseract sourceImage:sourceImage];
}
return nil;
}
@end