Skip to content

Using BarDecoder recognizer

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
PPBarDecoderRecognizerSettings *barDecoderRecognizerSettings = [[PPBarDecoderRecognizerSettings alloc] init];

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

// Set this to YES to scan Code 128 barcodes
barDecoderRecognizerSettings.scanCode128 = NO;

// 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 = NO;

// 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 = NO;

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

// 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.

- (void)scanningViewController:(UIViewController<PPScanningViewController> *)scanningViewController
              didOutputResults:(NSArray<PPRecognizerResult *> *)results {

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

    // first, pause scanning until we process all the results
    [scanningViewController pauseScanning];

    // Collect data from the result
    for (PPRecognizerResult* result in results) {

        // Check if result is BarDecoder result
        if ([result isKindOfClass:[PPBarDecoderRecognizerResult class]]) {

            // Cast result to PPBarDecoderRecognizerResult
            PPBarDecoderRecognizerResult *barDecoderResult = (PPBarDecoderRecognizerResult *)result;

            // check barcode type
            switch (barDecoderResult.barcodeType) {

                // Code 128
                case PPBarDecoderBarcodeTypeCode128:
                    NSLog(@"Barcode type is Code 128");
                    break;

                    // Code 39
                case PPBarDecoderBarcodeTypeCode39:
                    NSLog(@"Barcode type is Code 39");
                    break;
            }

            // If you don't know the exact encoding of the text use stringUsingGuessedEncoding
            NSLog(@"%@", [barDecoderResult stringUsingGuessedEncoding]);

            // If you know exactly which encoding is used in the barcode, specify it manually
            NSLog(@"%@", [barDecoderResult stringUsingEncoding:NSUTF8StringEncoding]);

            // If the barcode contains raw bytes instead of just text, obtain detailed barcode data
            NSLog(@"%@", [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
            NSLog(@"%@", [barDecoderResult extendedStringUsingGuessedEncoding]);
        }
    };

    // either resume scanning, or dismiss Scanning View controller
    // [scanningViewController resumeScanningAndResetState:YES];
    [scanningViewController dismissViewControllerAnimated:YES completion:nil];
}
Clone this wiki locally