Skip to content

Using MyKad recognizer in Swift

dino.gustin edited this page Jun 14, 2016 · 3 revisions

MyKad recognizer is responsible for scanning, decoding and parsing front side of Malaysian ID documents MyKads

If you completed Obtaining scanning results guide, you learned that in order to use a specific recognizer, you need to specify Recognizer Settings object in the initialization stage, and collect Recognizer Result object in the success callback.

Here we explain how to use MyKad recognizer, it's settings class PPMyKadRecognizerSettings, and result class PPMyKadRecognizerResult to obtain Driver's license information from the scanning results.

Back to "Getting started" guide.

Initializing the scanning of MyKad (Malysian ID) documents

Below is the sample source code which initializes the scanning for Malaysian ID documents.

// 3. ************* Setup Scan Settings **************/

// To specify we want to perform MyKad (Malysian ID document) recognition, initialize the MyKad recognizer settings
let mykadRecognizerSettings : PPMyKadRecognizerSettings = PPMyKadRecognizerSettings()

// Add MyKad Recognizer setting to a list of used recognizer settings
settings.scanSettings.addRecognizerSettings(mykadRecognizerSettings)

Retrieving results

By default, scanningViewController:didOutputResults: callback returns results as a PPRecognizerResults object. When the instance of this result is of type PPMyKadRecognizerResult, this means we got the result of MyKad scanning and parsing.

Below is the sample source code which demonstrates how to collect results of MyKad scanning.

func scanningViewController(scanningViewController: UIViewController?, didOutputResults results: [PPRecognizerResult]) {

    let scanController : PPScanningViewController = scanningViewController as! PPScanningViewController

    // Here you process scanning results. Scanning results are given in the array of PPRecognizerResult objects.

    // first, pause scanning until we process all the results
    scanController.pauseScanning()

    // Collect data from the result
    for result in results {

        // Check if result is MyKad result
        if (result.isKindOfClass(PPMyKadRecognizerResult)) {

            // Cast result to PPMyKadRecognizerResult
            let myKadResult : PPMyKadRecognizerResult = result as! PPMyKadRecognizerResult

            // For accessing owner full name, use property ownerFullName
            print("Full name: %@",myKadResult.ownerFullName)

            // For accessing NRIC number, use property nricNumber
            print("NRIC no.: %@", myKadResult.nricNumber)

            // For accessing owner address, use property ownerAddress
            print("Address: %@", myKadResult.ownerAddress)

            // For accessing Birth date, use property ownerBirthDate
            print("Birth date: %@", myKadResult.ownerBirthDate)

            // For accessing Religion, use property ownerReligion
            print("Religion: %@", myKadResult.ownerReligion)

            // For accessing owner sex, use property ownerSex
            print("Sex: %@", myKadResult.ownerSex)
        }
    }

    // either resume scanning, or dismiss Scanning View controller
    // scanningViewController.resumeScanningAndResetState(true)
    scanningViewController.dismissViewControllerAnimated(true, completion:nil)
}
Clone this wiki locally