看到一个光学识别信用卡的文章,根据文章测试了下,识别信用卡还算灵敏,但是可惜的是识别不了储蓄卡,这里记录一下:
所用框架github地址:https://github.com/AllLuckly/card.io-iOS-SDK
具体的使用,demo中有介绍,这里只记录一下集成:
将demo里的CardIO文件夹拖进工程,然后在TARGETS---Build Phases---Link Binary With Libraries里边加入系统框架框架:
Accelerate.framework
MobileCoreServices.framework
CoreMedia.framework
AudioToolbox.framework
AVFoundation.framework
接下来就可以使用相关的API了:
首先导入头文件:
#import "CardIO.h"
#import "CardIOPaymentViewControllerDelegate.h"
实现代理方法:
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];
}
import UIKit
class 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 = .FormSheet
presentViewController(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)
}
}