tesseractOCR 识别数字问题
程序员文章站
2022-04-15 13:34:11
...
拍下图片再进行识别,所以如果图片的分辨率不高的话是无法准确识别的,那么在拍出来进行截取时,就需要保留原有的分辨率,确保图片不会严重失真(归根结底保证图片不失真)
处理图片方法:
/**
*将图片缩放到指定的CGSize大小
* UIImage image 原始的图片
* CGSize size 要缩放到的大小
*/
-(UIImage*)image:(UIImage *)image scaleToSize:(CGSize)size{
CGSize size1 = CGSizeMake(size.width * [UIScreen mainScreen].scale, size.height * [UIScreen mainScreen].scale);
UIGraphicsBeginImageContext(size1);
// 得到图片上下文,指定绘制范围
UIGraphicsBeginImageContext(size1);
// 将图片按照指定大小绘制
[image drawInRect:CGRectMake(0, 0, size1.width, size1.height)];
// 从当前图片上下文中导出图片
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
// 当前图片上下文出栈
UIGraphicsEndImageContext();
// 返回新的改变大小后的图片
return scaledImage;
}
/**
*从图片中按指定的位置大小截取图片的一部分
* UIImage image 原始的图片
* CGRect rect 要截取的区域
*/
-(UIImage )imageFromImage:(UIImage )image inRect:(CGRect)rect{
float a = [UIScreen mainScreen].scale;
//将UIImage转换成CGImageRef
CGImageRef sourceImageRef = [image CGImage];
//按照给定的矩形区域进行剪裁
CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, CGRectMake(rect.origin.x*a, rect.origin.y*a,rect.size.width*a, rect.size.height*a));
//将CGImageRef转换成UIImage
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
//返回剪裁后的图片
return newImage;
}
完整代码:
// 这是拍照按钮的方法
- (void)shutterCamera
{
NSLog(@"jnlai");
AVCaptureConnection *videoConnection = [self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo];
if (!videoConnection) {
return;
}
[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
if (imageDataSampleBuffer == NULL) {
return;
}
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
UIImage *image = [UIImage imageWithData:imageData];
NSLog(@"image size = %@ %lf %lf", NSStringFromCGSize(image.size),image.size.height,image.size.width);
NSInteger h = screenHeigth-370-64*2-45-30;
NSInteger w = screenWidth-90;
UIImage* image1 = [Tools image:image scaleToSize:CGSizeMake(screenWidth, screenHeigth)];
UIImage *image2= [Tools imageFromImage:image1 inRect:CGRectMake(45, 45, w, h)];
[self recognizeImageWithTesseract:image2];
}];
}
-(void)recognizeImageWithTesseract:(UIImage *)image{
NSMutableArray *Marr = [NSMutableArray arrayWithCapacity:9];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// 处理耗时操作的代码块...
G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:@"eng"];
tesseract.image = [image g8_blackAndWhite];
tesseract.engineMode = G8OCREngineModeCubeOnly;
tesseract.charWhitelist = @"0123456789";
[tesseract recognize];
NSString *recognizedText = tesseract.recognizedText;
NSArray *recognizeArr = [recognizedText componentsSeparatedByString:@"\n"];
NSLog(@"arrarr %@",recognizeArr);
NSString *temp =nil;
for (NSString *str in recognizeArr) {
if ([self checkIsHaveNumAndLetter:str]>2) {
NSMutableString *mStr = [[NSMutableString alloc]init];
for(int i =0; i < [str length]; i++)
{
temp = [str substringWithRange:NSMakeRange(i,1)];
if ([Tools deptNumInputShouldNumber:temp]) {
[mStr appendString:temp];
}
if ([temp isEqualToString:@"B"]) {
[mStr appendString:@"8"];
}
if ([temp isEqualToString:@"l"]) {
[mStr appendString:@"1"];
}
if ([temp isEqualToString:@"O"]||[temp isEqualToString:@"o"]) {
[mStr appendString:@"0"];
}
}
if ([Tools checkLandline:mStr]||[Tools checkTel:mStr]) {
[_Marr addObject:mStr];
[Marr addObject:mStr];
}
}
}
NSLog(@"识别结果 \n %@",recognizedText);
//通知主线程刷新
dispatch_async(dispatch_get_main_queue(), ^{
//回调或者说是通知主线程刷新,
if (Marr.count !=0) {
[_Marr addObjectsFromArray:Marr];
_dataArr = [self removeArray:_Marr].copy;
[self To_CreateScanResultView];
}
});
});
}
在处理识别结果时最好先用”\n”分割成数组,会比较简单处理数据