導(dǎo)語
受到搜狗輸入法中文字掃描功能的啟發(fā),小編自不量力的進(jìn)行了一番嘗試,雖然達(dá)到了一些初步的
效果,但是優(yōu)化提升的空間依然很大,所以小編在這里記錄一下前的工作與大家分享,也希望能獲
得大家更好的建議,對該功能進(jìn)行完善。
工作準(zhǔn)備
1、創(chuàng)建項(xiàng)目
2、打開終端cd進(jìn)入文件根目錄
3、執(zhí)行pod init
4、進(jìn)入Podfile文件寫上pod 'TesseractOCRiOS'
5、終端在該項(xiàng)目根目錄下執(zhí)行pod install
6、打開 xcworkspace文件 真機(jī)運(yùn)行后報(bào)錯(cuò)
(ld: -weak_library and -bitcode_bundle (Xcode setting ENABLE_BITCODE=YES)
cannotbe used together)

解決報(bào)錯(cuò)問題.png
7、在項(xiàng)目中創(chuàng)建文件夾(注意文件夾顏色)

tessdata文件夾.png
8、下載漢化包和英化包,如上圖文件夾中所示
編寫代碼
1、引入頭文件
#import <TesseractOCR/TesseractOCR.h>
2、創(chuàng)建屬性
@property (nonatomic, strong) NSOperationQueue *operationQueue;
/**菊花*/
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
/**識別后文字顯示*/
@property (weak, nonatomic) IBOutlet UITextView *contens;
/**進(jìn)度條*/
@property (weak, nonatomic) IBOutlet UILabel *progressLabel;
3、viewDidLoad中實(shí)現(xiàn)
self.operationQueue = [[NSOperationQueue alloc] init];
4、定義核心識別方法-(void)recognizeImage:(UIImage *)image;
-(void)recognizeImageWithTesseract:(UIImage *)image{
self.contens.text=@"";
self.progressLabel.text=@"";
[self.activityIndicator startAnimating];
//初始化G8Tesseract類,為文字識別做準(zhǔn)備
G8RecognitionOperation *operation = [[G8RecognitionOperation alloc] initWithLa
nguage:@"eng+chi_sim"];
operation.tesseract.engineMode = G8OCREngineModeTesseractOnly;
operation.tesseract.pageSegmentationMode = G8PageSegmentationModeAutoOnly;
// operation.tesseract.maximumRecognitionTime = 3.0;//最大識別時(shí)間
operation.delegate = self;
operation.tesseract.image = [image g8_blackAndWhite];
operation.tesseract.image = image;
operation.recognitionCompleteBlock = ^(G8Tesseract *tesseract) {
NSString *recognizedText = tesseract.recognizedText;
NSLog(@"%@", recognizedText);
[self.activityIndicator stopAnimating];
self.contens.text=[NSString stringWithFormat:@"識別結(jié)果:\n%@",recognizedText];
dispatch_async(dispatch_get_main_queue(), ^{
self.progressLabel.text=[NSString stringWithFormat:@"進(jìn)度:100%%"];
});
};
[self.operationQueue addOperation:operation];
}
5、實(shí)現(xiàn)代理---G8TesseractDelegate
- (void)progressImageRecognitionForTesseract:(G8Tesseract *)tesseract {
dispatch_async(dispatch_get_main_queue(), ^{
self.progressLabel.text=[NSString stringWithFormat:@"進(jìn)度:%lu%%",
tesseract.progress];
});
NSLog(@"progress: %lu", (unsigned long)tesseract.progress);
}
- (BOOL)shouldCancelImageRecognitionForTesseract:(G8Tesseract *)tesseract {
return NO;
}
6、調(diào)用識別方法傳入一個(gè)需要被識別的圖片(從Xcode本地獲取、調(diào)用相機(jī)、調(diào)用本地相冊)
個(gè)人心得
一、iOS終端識別功能優(yōu)缺點(diǎn):
優(yōu)點(diǎn):不需要聯(lián)網(wǎng);
缺點(diǎn):1、 識別速度有待提高、2識別文字如果間隔太小會產(chǎn)生亂碼;
二、其他實(shí)現(xiàn)方式:
通過測試發(fā)現(xiàn)搜狗輸入法是通過前端傳入識別圖片,后端進(jìn)行識別處理,這種方案優(yōu)點(diǎn)是識別速度相對快些、缺點(diǎn)是必須得連接網(wǎng)絡(luò)(這種方案騰訊和百度都有相關(guān)的SDK,但是都是盈利性質(zhì)的,所以如果公司該功能使用情況比較頻繁的話,還是建議自己造一個(gè)是最優(yōu)的選擇??)
拓展
由于小編對Python有點(diǎn)皮毛的了解,所以也研究了一下Python圖文識別,代碼相對比較簡單,以下附上最終代碼,前期準(zhǔn)備工作就不深入說明了,如果有不清楚的歡迎私信!
from PIL import Image
import pytesseract
import datetime
#打印當(dāng)前時(shí)間
time_stamp =datetime.datetime.now()
print(time_stamp.strftime('%Y.%m.%d-%H:%M:%S'))
# text = pytesseract.image_to_string(Image.open('image_one.png'),
lang='eng+chi_sim')
text = pytesseract.image_to_string(Image.open('Wechat_Image.jpeg'),
lang='eng+chi_sim')
#打印識別后文字
print(text)
#打印當(dāng)前時(shí)間
time_stamp_two =datetime.datetime.now()
print(time_stamp_two.strftime('%Y.%m.%d-%H:%M:%S'))