Skip to content

Using Barcode recognizer

juraskrlec edited this page Sep 12, 2017 · 1 revision

Barcode recognizer is responsible for scanning all existing barcode formats except PDF417. PDF417 is scanned with PDF417 recognizer.

Supported barcode types in BarcodeRecognizer:

  • Aztec
  • Code 39
  • Code 128
  • Data Matrix
  • EAN 13
  • EAN 8
  • ITF
  • QR code
  • UPCA
  • UPCE

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 Barcode Recognizer, you need to use PPBarcodeRecognizerSettings object for initialization, and PPBarcodeRecognizerResult 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.

// To specify we want to perform ZXing recognition, initialize the ZXing recognizer settings
PPBarcodeRecognizerSettings *barcodeRecognizerSettings = [[PPBarcodeRecognizerSettings alloc] init];

// Set this to YES to scan Aztec 2D barcodes
barcodeRecognizerSettings.scanAztec = YES;

// Set this to YES to scan Code 128 1D barcodes
barcodeRecognizerSettings.scanCode128 = NO;

// Set this to YES to scan Code 39 1D barcodes
barcodeRecognizerSettings.scanCode39 = NO;

// Set this to YES to scan DataMatrix 2D barcodes
barcodeRecognizerSettings.scanDataMatrix = NO;

// Set this to YES to scan EAN 13 barcodes
barcodeRecognizerSettings.scanEAN13 = YES;

// Set this to YES to scan EAN8 barcodes
barcodeRecognizerSettings.scanEAN8 = NO;

//Set this to YES to scan ITF barcodes
barcodeRecognizerSettings.scanITF = NO;

// Set this to YES to scan QR barcodes
barcodeRecognizerSettings.scanQR = YES;

// Set this to YES to scan UPCA barcodes
barcodeRecognizerSettings.scanUPCA = NO;

// Set this to YES to scan UPCE barcodes
barcodeRecognizerSettings.scanUPCE = 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
barcodeRecognizerSettings.scanInverse = NO;

// Set manatee lib key to unlock all Aztec scanning features
barcodeRecognizerSettings.manateeKey = "<your manatee lib key for Aztec>";

// Add ZXingDecoderRecognizer setting to a list of used recognizer settings
[settings.scanSettings addRecognizerSettings:barcodeRecognizerSettings];

Retrieving results

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

- (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 Barcode result
        if ([result isKindOfClass:[PPBarcodeRecognizerResult class]]) {

            // Cast result to PPBarcodeRecognizerResult
            PPBarcodeRecognizerResult *barcodeResult = (PPBarcodeRecognizerResult *)result;

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

                // Aztec
                case PPBarcodeTypeAztec:
                    NSLog(@"Barcode type is Aztec:");
                    break;

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

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

                // Data Matrix
                case PPBarcodeTypeDataMatrix:
                    NSLog(@"Barcode type is Data Matrix:");
                    break;

                // EAN 13
                case PPBarcodeTypeEAN13:
                    NSLog(@"Barcode type is EAN 13:");
                    break;

                // EAN 8
                case PPBarcodeTypeEAN8:
                    NSLog(@"Barcode type is EAN 8:");
                    break;

                // ITF
                case PPBarcodeTypeITF:
                    NSLog(@"Barcode type is ITF:");
                    break;

                // QR code
                case PPBarcodeTypeQR:
                    NSLog(@"Barcode type is QR:");
                    break;

                // UPCA
                case PPBarcodeTypeUPCA:
                    NSLog(@"Barcode type is UPCA:");
                    break;

                // UPCE
                case PPBarcodeTypeUPCE:
                    NSLog(@"Barcode type is UPCE:");
                    break;
            }

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

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

            // If the barcode contains raw bytes instead of just text, obtain detailed barcode data
            NSLog(@"%@", [barcodeResult rawData]);
        }
    };

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