iosmicroblink

Microblink: getting values after scanning


I've set up the Microblink card reader to read only one side, and not give the user the ability to edit the scan results:

func didTapScan() {
        /** Create BlinkCard recognizer */
        blinkCardRecognizer = MBCBlinkCardRecognizer()
        blinkCardRecognizer?.extractCvv = false
        blinkCardRecognizer?.extractIban = false
        blinkCardRecognizer?.extractExpiryDate = false
        
        /** Create BlinkCard settings */
        let settings : MBCBlinkCardOverlaySettings = MBCBlinkCardOverlaySettings()
        settings.enableEditScreen = false
        
        /** Crate recognizer collection */
        let recognizerList = [blinkCardRecognizer!]
        let recognizerCollection : MBCRecognizerCollection = MBCRecognizerCollection(recognizers: recognizerList)
        
        /** Create your overlay view controller */
        let blinkCardOverlayViewController = MBCBlinkCardOverlayViewController(settings: settings, recognizerCollection: recognizerCollection, delegate: self)
        
        /** Create recognizer view controller with wanted overlay view controller */
        // NOTE that I put a bang on the end of this - not good
        let recognizerRunneViewController : UIViewController = MBCViewControllerFactory.recognizerRunnerViewController(withOverlayViewController: blinkCardOverlayViewController)!
        
        /** Present the recognizer runner view controller. You can use other presentation methods as well (instead of presentViewController) */
        self.present(recognizerRunneViewController, animated: true, completion: nil)
    }

After scanning, I check in the delegate callback for a valid state, and then I try to retrieve the values from the cardRecognizer, but I'm getting nothing but crashes:

func blinkCardOverlayViewControllerDidFinishScanning(_ blinkCardOverlayViewController: MBCBlinkCardOverlayViewController, state: MBCRecognizerResultState) {
        // this is done on background thread
        // check for valid state
        if state == .valid {
            
            CRASHES HERE
            guard let result = blinkCardRecognizer?.combinedResult else {
                return
            }
            // CRASHES HERE
            print (result)
            
            // first, pause scanning until we process all the results
            blinkCardOverlayViewController.recognizerRunnerViewController?.pauseScanning()
            
            DispatchQueue.main.async(execute: {() -> Void in
                print(self.blinkCardRecognizer)
                // self.dismiss(animated: true, completion: nil)
            })
        }
    }

What am I missing here?


Solution

  • You've done everything correctly regarding the first block of code.

    Regarding the second part (in the blinkCardOverlayViewControllerDidFinishScanning method), the .combinedResult is the parent of the result object, so you can use the blinkCardRecognizer.result instead.

    Also, there seems to be an issue with the recognizer's description method (blinkCardRecognizer.result), so you would need to specify which information you want to extract.

    An example code would be:

    extension ViewController: MBCBlinkCardOverlayViewControllerDelegate {
        func blinkCardOverlayViewControllerDidFinishScanning(_ blinkCardOverlayViewController: MBCBlinkCardOverlayViewController, state: MBCRecognizerResultState) {
            /** This is done on background thread */
            
            if state == .valid {
    
                guard let result = blinkCardRecognizer?.result else {
                      return
                  }
                blinkCardOverlayViewController.recognizerRunnerViewController?.pauseScanning()
    
                  DispatchQueue.main.async(execute: {() -> Void in
                    print(result.cardNumber)
                  })
              }
          }