我要求Microlink读卡器查看卡片的照片,而不是使用相机。我试过万事达卡的人像版和风景版。这就是我如何声明我的主要变量:
let blinkCardRecognizer = MBCBlinkCardRecognizer()
var recognizerList = [MBCRecognizer]()
lazy var recognizerCollection: MBCRecognizerCollection = {
blinkCardRecognizer.extractCvv = false
blinkCardRecognizer.extractIban = false
blinkCardRecognizer.extractExpiryDate = false
recognizerList.append(blinkCardRecognizer)
return MBCRecognizerCollection(recognizers: recognizerList)
}()
lazy var recognizerRunner: MBCRecognizerRunner = {
MBCRecognizerRunner(recognizerCollection: recognizerCollection)
}()
我已经宣布了这两位代表:
MBCBlinkCardOverlayViewControllerDelegate, MBCScanningRecognizerRunnerDelegate
在我得到UIImage之后,我调用这个函数:
func prepareToReadImage(_ theImage: UIImage?) {
recognizerRunner.scanningRecognizerRunnerDelegate = self
var image: MBCImage? = nil
if let anImage = theImage {
image = MBCImage(uiImage: anImage)
}
image?.cameraFrame = true
image?.orientation = MBCProcessingOrientation.down
let _serialQueue = DispatchQueue(label: "com.microblink.DirectAPI-sample-swift")
_serialQueue.async(execute: {() -> Void in
self.recognizerRunner.processImage(image!)
})
}
这是我的代理回拨。每次,我都会得到.empty
:的状态
func recognizerRunner(_ recognizerRunner: MBCRecognizerRunner, didFinishScanningWith state: MBCRecognizerResultState) {
if state == .valid { // values: .empty, .uncertain, .valid, .stageValid
let result = blinkCardRecognizer.result
DispatchQueue.main.async(execute: {() -> Void in
print (result.owner)
print (result.cardNumber)
print (result.cardNumberPrefix)
})
}
}
为什么我没有得到.valid
响应?此外,一旦(如果和何时(我这样做了,这看起来像是提取卡片详细信息的正确方式吗?谢谢
这个问题与这里的问题有关吗?
如果是,我还可以提出以下建议:你可以添加一个函数来调用图像选择器(我使用设备相机作为源类型(:
private func openImagePicker() {
let imagePicker = UIImagePickerController()
imagePicker.sourceType = .camera
imagePicker.cameraDevice = .rear
addOverlayLabel(toImagePicker: imagePicker)
// Displays a control that allows the user to choose only photos
imagePicker.mediaTypes = [kUTTypeImage as String]
// Hides the controls for moving & scaling pictures, or for trimming movies.
imagePicker.allowsEditing = false
// Shows default camera control overlay over camera preview.
imagePicker.showsCameraControls = true
// set delegate
imagePicker.delegate = self
present(imagePicker, animated: true) {() -> Void in }
imagePickerController = imagePicker
}
在func recognizerRunner
中,当状态无效时,可以放置要调用的函数,如下所示:
extension ViewController: MBCScanningRecognizerRunnerDelegate {
func recognizerRunner(_ recognizerRunner: MBCRecognizerRunner, didFinishScanningWith state: MBCRecognizerResultState) {
DispatchQueue.main.async(execute: {() -> Void in
if state != .valid {
self.openImagePicker()
return
}
print(state.rawValue)
let result = self.blinkCardRecognizer.result
print("Owner: (result.owner)")
print("Card number: (result.cardNumber)")
print("Card number prefix: (result.cardNumberPrefix)")
})
}
}
如果状态无效,这将使您有机会重新拍摄图像。
还有一个问题,你把prepareToReadImage
放在哪里?
您需要将其放置在func imagePickerController
中,如下所示:
extension ViewController: UIImagePickerControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let mediaType = info[UIImagePickerController.InfoKey.mediaType] as? String
if CFStringCompare(mediaType as CFString?, kUTTypeImage, CFStringCompareFlags(rawValue: 9)) == .compareEqualTo {
let originalImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
prepareToReadImage(originalImage)
}
picker.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
serialQueue.async {
self.recognizerRunner?.resetState()
}
dismiss(animated: true, completion: nil)
}
}
最后,在开始扫描过程之前,请确保放置了识别器的设置。
我添加了func setupRecognizerRunner
,在这里我定义了你在代码块中所做的一切:
private func setupRecognizerRunner() {
blinkCardRecognizer = MBCBlinkCardRecognizer()
var recognizers = [MBCRecognizer]()
blinkCardRecognizer.extractCvv = false
blinkCardRecognizer.extractIban = false
blinkCardRecognizer.extractExpiryDate = false
recognizers.append(blinkCardRecognizer)
let recognizerCollection = MBCRecognizerCollection(recognizers: recognizers)
recognizerRunner = MBCRecognizerRunner(recognizerCollection: recognizerCollection)
recognizerRunner?.scanningRecognizerRunnerDelegate = self
}
在加载了许可证密钥方法之后,我将其放置在viewDidLoad()
中。
删除以下行使我能够获得.valid
响应:
image?.cameraFrame = true