0
点赞
收藏
分享

微信扫一扫

iOS OCR 之银行卡/身份证信息识别小知识

Alex富贵 2022-08-01 阅读 48

iOS OCR 之银行卡/身份证信息识别小知识_ide

引言

功能:扫描银行卡识别信息( 银行名称、 银行卡号)并截取银行卡图像

应用场景:快速填充银行卡号的场景,比如商户进件、实名认证

原理:

  1. 自定义相机并利用第三方库SDK​​libexbankcardios.a​​​ 、​​libbexbankcard.a​​​进行识别(识别​​次数无限,免费 ​​)

2.添加自定义的扫描界面(中间有一个镂空窗口和来回移动的扫描线)

I、扫描银行卡

1.1 引入第三方SDK和头文件

exbankcard.h BankCard.h exbankcardcore.h libexbankcardios.a libbexbankcard.a

1.2 获取信息(解码)

#pragma
- (void)parseBankImageBuffer:(CVImageBufferRef)imageBuffer {
#if

#else
size_t width_t= CVPixelBufferGetWidth(imageBuffer);
size_t height_t = CVPixelBufferGetHeight(imageBuffer);
CVPlanarPixelBufferInfo_YCbCrBiPlanar *planar = CVPixelBufferGetBaseAddress(imageBuffer);
size_t offset = NSSwapBigIntToHost(planar->componentInfoY.offset);

unsigned char* baseAddress = (unsigned char *)CVPixelBufferGetBaseAddress(imageBuffer);
unsigned char* pixelAddress = baseAddress + offset;

size_t cbCrOffset = NSSwapBigIntToHost(planar->componentInfoCbCr.offset);
uint8_t *cbCrBuffer = baseAddress + cbCrOffset;

CGSize size = CGSizeMake(width_t, height_t);
CGRect effectRect = [IDCardRectManager getEffectImageRect:size];
CGRect rect = [IDCardRectManager getGuideFrame:effectRect];

int width = ceilf(width_t);
int height = ceilf(height_t);

unsigned char result [512];
int resultLen = BankCardNV12(result, 512, pixelAddress, cbCrBuffer, width, height, rect.origin.x, rect.origin.y, rect.origin.x+rect.size.width, rect.origin.y+rect.size.height);

if(resultLen > 0) {

int charCount = [IDCardRectManager docode:result len:resultLen];
if(charCount > 0) {
// CGRect subRect = [IDCardRectManager getCorpCardRect:width height:height guideRect:rect charCount:charCount];

self.isHasResult = YES;
if ([self.captureSession isRunning]) {
[self.captureSession stopRunning];
}
UIImage *image = [UIImage getImageStream:imageBuffer];
// __block UIImage *subImg = [UIImage getSubImage:subRect inImage:image];

char *numbers = [IDCardRectManager getNumbers];

NSString *numberStr = [NSString stringWithCString:numbers encoding:NSASCIIStringEncoding];
NSString *bank = [BankCardSearch getBankNameByBin:numbers count:charCount];

NSLog(@"\n卡号%@\n银行类型%@",numberStr,bank);

BankCardInfo *model = [JYBDBankCardInfo new];

model.bankNumber = numberStr;
model.bankName = bank;
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
dispatch_async(dispatch_get_main_queue(), ^{

if (self.finish)
{
self.finish(model, image);
}
});
}
}
CVPixelBufferUnlockBaseAddress(imageBuffer, 0);
self.isInProcessing = NO;
#endif

1.3 图片裁剪

void)parseBankImageBuffer:(CVImageBufferRef)imageBuffer {
size_t width_t= CVPixelBufferGetWidth(imageBuffer);
size_t height_t = CVPixelBufferGetHeight(imageBuffer);
CGSize size = CGSizeMake(width_t, height_t);

CGRect effectRect = [IDCardRectManager getEffectImageRect:size];
CGRect rect = [IDCardRectManager getGuideFrame:effectRect];

UIImage *image = [UIImage getImageStream:imageBuffer];// image 原图
__block UIImage *subImg = [UIImage getSubImage:rect inImage:image];// 裁剪 rect

  • 工具方法

UIImage *)getImageStream:(CVImageBufferRef)imageBuffer {
CIImage *ciImage = [CIImage imageWithCVPixelBuffer:imageBuffer];
CIContext *temporaryContext = [CIContext contextWithOptions:nil];
CGImageRef videoImage = [temporaryContext createCGImage:ciImage fromRect:CGRectMake(0, 0, CVPixelBufferGetWidth(imageBuffer), CVPixelBufferGetHeight(imageBuffer))];

UIImage *image = [[UIImage alloc] initWithCGImage:videoImage];

CGImageRelease(videoImage);
return image;
}

+ (UIImage *)getSubImage:(CGRect)rect inImage:(UIImage*)image {
CGImageRef subImageRef = CGImageCreateWithImageInRect(image.CGImage, rect);

CGRect smallBounds = CGRectMake(0, 0, CGImageGetWidth(subImageRef), CGImageGetHeight(subImageRef));

UIGraphicsBeginImageContext(smallBounds.size);

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextDrawImage(context, smallBounds, subImageRef);

UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
CFRelease(subImageRef);

UIGraphicsEndImageContext();

return

1.4 根据numbers和charCount查询银行名称

II 常见问题

2.1 iOS13适配:present 半屏问题

全屏显示采集界面

- (NSMutableArray *)FullScreenClasss{

if(_FullScreenClasss == nil){

[_FullScreenClasss addObject:@"KNAVCapture4IDfrontViewController"];
}
return

2.2 Undefined symbols for architecture arm64

Undefined symbols for architecture arm64:
"_ZIM_SaveImage", referenced from:
ImgSave(tagIMG, char const*) in libbexbankcard.a(gjimage.o)
"_ZIM_LoadImage", referenced from:
ImgLoad(tagIMG&, char const*) in libbexbankcard.a(gjimage.o)
"_ZIM_DoneImage", referenced from:
ImgLoad(tagIMG&, char const*) in libbexbankcard.a(gjimage.o)
ld: symbol(s) not found for

解决办法:在TARGETS和PROJECT 两处中build settings 搜索 ENABLE_TESTABILITY 改为NO

RN项目,Dead Code Stripping 设置为yes

see also

如果无法下载Demo,请关注公众号:【iOS逆向】,进行获取

iOS OCR 之银行卡/身份证信息识别小知识_.net_04

欢迎关注​​#小程序:iOS逆向​

举报

相关推荐

0 条评论