Skip to content

Using PDF417 recognizer in Swift

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

PDF417 recognizer is responsible for scanning and reading 2D barcodes in PDF417 standard

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.

To use PDF417 Recognizer, you need to use PPPdf417RecognizerSettings object for initialization, and PPPdf417RecognizerResult for collecting results.

Back to "Getting started" guide.

Initializing the scanning of PDF417 barcodes

Below is the sample source code which initializes the scanning for PDF417 barcodes, and specifies default values for all available parameteres.

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

// To specify we want to perform PDF417 recognition, initialize the PDF417 recognizer settings
let pdf417RecognizerSettings : PPPdf417RecognizerSettings = PPPdf417RecognizerSettings()

// Set this to YES to scan even barcode not compliant with standards
// For example, malformed PDF417 barcodes which were incorrectly encoded
// Use only if necessary because it slows down the recognition process
pdf417RecognizerSettings.scanUncertain = false

// Set this to YES to scan barcodes which don't have quiet zone (white area) around it
// Use only if necessary because it slows down the recognition process
pdf417RecognizerSettings.allowNullQuietZone = false

// Set this to YES to allow scanning barcodes with inverted intensities
// (i.e. white barcodes on black background)
// NOTE: this options doubles the frame processing time
pdf417RecognizerSettings.scanInverse = false;

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

Retrieving results

Below is the sample source code which collects results of PDF417 barcode scanning.

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

    let scanConroller : 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
    scanConroller.pauseScanning()

    // Collect data from the result
    for result in results {
        if(result.isKindOfClass(PPPdf417RecognizerResult)) {
            let pdf417Result : PPPdf417RecognizerResult = result as! PPPdf417RecognizerResult
            
            // if you know barcode contains text, obtain string results

            // If you don't know the exact encoding of the text use stringUsingGuessedEncoding
            print(pdf417Result.stringUsingGuessedEncoding());

            // If you know exactly which encoding is used in the barcode, specify it manually
            print(pdf417Result.stringUsingEncoding(NSUTF8StringEncoding));

            // If the barcode contains raw bytes instead of just text, obtain detailed barcode data
            print(pdf417Result.rawData());
        }
    }

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