[iOS]OCR光学识别信用卡

来源:互联网 发布:源码与公众号 编辑:程序博客网 时间:2024/04/30 04:28

看到一个光学识别信用卡的文章,根据文章测试了下,识别信用卡还算灵敏,但是可惜的是识别不了储蓄卡,这里记录一下:

原文cocoachina微信公共号链接

所用框架github地址:https://github.com/AllLuckly/card.io-iOS-SDK

具体的使用,demo中有介绍,这里只记录一下集成:

将demo里的CardIO文件夹拖进工程,然后在TARGETS---Build Phases---Link Binary With Libraries里边加入系统框架框架:

Accelerate.frameworkMobileCoreServices.frameworkCoreMedia.frameworkAudioToolbox.frameworkAVFoundation.framework

最后,在TARGETS---Build Settings---Other Linker Flags中添加-ObjC和-lc++即可;

接下来就可以使用相关的API了:

首先导入头文件:

#import "CardIO.h"#import "CardIOPaymentViewControllerDelegate.h"

并实现代理CardIOPaymentViewControllerDelegate

实现代理方法:

Objective-C:

 (void)viewWillAppear {[super viewWillAppear];[CardIOUtilities preload];}//开始调用扫描- (IBAction)begin:(id)sender {CardIOPaymentViewController *scanViewController = [[CardIOPaymentViewController alloc] initWithPaymentDelegate:self];[self presentViewController:scanViewController animated:YES completion:nil];}//取消扫描- (void)userDidCancelPaymentViewController:(CardIOPaymentViewController *)scanViewController{[scanViewController dismissViewControllerAnimated:YES completion:nil];}//扫描完成-(void)userDidProvideCreditCardInfo:(CardIOCreditCardInfo *)info inPaymentViewController:(CardIOPaymentViewController *)scanViewController{//扫描结果NSLog(@"Received card info. Number: %@, expiry: %02i/%i, cvv: %@.", info.redactedCardNumber, info.expiryMonth, info.expiryYear, info.cvv);[scanViewController dismissViewControllerAnimated:YES completion:nil];}

Swift:

import UIKitclass ViewController: UIViewController, CardIOPaymentViewControllerDelegate {@IBOutlet weak var resultLabel: UILabel!override func viewDidLoad() {super.viewDidLoad()CardIOUtilities.preload()}//开始调用扫描@IBAction func scanCard(sender: AnyObject) {let cardIOVC = CardIOPaymentViewController(paymentDelegate: self)cardIOVC.modalPresentationStyle = .FormSheetpresentViewController(cardIOVC, animated: true, completion: nil)}//取消扫描func userDidCancelPaymentViewController(paymentViewController: CardIOPaymentViewController!) {resultLabel.text = "user canceled"paymentViewController?.dismissViewControllerAnimated(true, completion: nil)}//扫描完成func userDidProvideCreditCardInfo(cardInfo: CardIOCreditCardInfo!, inPaymentViewController paymentViewController: CardIOPaymentViewController!) {if let info = cardInfo {let str = NSString(format: "Received card info.\\\\n Number: %@\\\\n expiry: %02lu/%lu\\\\n cvv: %@.", info.redactedCardNumber, info.expiryMonth, info.expiryYear, info.cvv)resultLabel.text = str as String}paymentViewController?.dismissViewControllerAnimated(true, completion: nil)}}

到此,基本就完成了,尝试一下吧!!!

0 0
原创粉丝点击