Skip to content

Using BarDecoder recognizer in Swift

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

BarDecoder recognizer is responsible for scanning Code 39 and Code 128 1D barcodes. Alternatively, scanning the same barcodes can be performed with ZXingRecognizer, but in general, for these barcode types, BarDecoder is faster and more robust.

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 BarDecoder Recognizer, you need to use PPBarDecoderRecognizerSettings object for initialization, and PPBarDecoderRecognizerResult for collecting results.

Back to "Getting started" guide.

Initializing the scanning with BarDecoder recognizer

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

// To specify we want to perform BarDecoder recognition, initialize the BarDecoder recognizer settings
let barDecoderRecognizerSettings : PPBarDecoderRecognizerSettings = PPBarDecoderRecognizerSettings()

// Set this to YES to scan Code 39 barcodes
barDecoderRecognizerSettings.scanCode39 = true;

// Set this to YES to scan Code 128 barcodes
barDecoderRecognizerSettings.scanCode128 = 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
barDecoderRecognizerSettings.scanInverse = false;

// Use automatic scale detection feature. This normally should not be used.
// The only situation where this helps in getting better scanning results is
// when using kPPUseVideoPresetPhoto on iPad devices.
// Video preview resoution of 2045x1536 in that case is very large and autoscale helps.
barDecoderRecognizerSettings.autoDetectScale = false;

// Set this to YES to enable scanning of lower resolution barcodes
// at cost of additional processing time.
barDecoderRecognizerSettings.tryHarder = true;

// Add BarDecoderRecognizer setting to a list of used recognizer settings
settings.scanSettings.addRecognizerSettings(barDecoderRecognizerSettings);

Retrieving results

Below is the sample source code which collects results of barcode scanning with BarDecoder recognizer.

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(PPBarDecoderRecognizerResult)) {
            let barDecoderResult : PPBarDecoderRecognizerResult=result as! PPBarDecoderRecognizerResult
            
            switch barDecoderResult.barcodeType() {
                case PPBarDecoderBarcodeType.Code128:
                    print("Barcode type is Code 128")
                case PPBarDecoderBarcodeType.Code39:
                    print("barcode type is Code 39")
            }
            
            // If you don't know the exact encoding of the text use stringUsingGuessedEncoding
            print(barDecoderResult.stringUsingGuessedEncoding());

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

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

            // Code 39 and Code 128 can be encoded in "extended" encoding.
            // Use getters which start with "extended" to get the result for these barcodes
           print(barDecoderResult.extendedStringUsingGuessedEncoding())
        }
    }

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