Skip to content

Using ZXing recognizer in Swift

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

ZXing recognizer is responsible for scanning all existing barcode formats except PDF417. PDF417 is scanned with PDF417 recognizer. Code 39 and Code 128 barcodes are scanned with BarDecoder Recognizer. For all other types, use ZXing.

Supported barcode types in ZXingRecognizer:

  • Aztec (alpha quality)
  • Code 39
  • Code 128
  • Data Matrix (alpha quality)
  • 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 ZXing Recognizer, you need to use PZXingRecognizerSettings object for initialization, and PPZXingRecognizerResult 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
let zxingRecognizerSettings : PPZXingRecognizerSettings = PPZXingRecognizerSettings()

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

// Set this to YES to scan Code 128 1D barcodes
zxingRecognizerSettings.scanCode128 = false;

// Set this to YES to scan Code 39 1D barcodes
zxingRecognizerSettings.scanCode39 = false;

// Set this to YES to scan DataMatrix 2D barcodes
zxingRecognizerSettings.scanDataMatrix = false;

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

// Set this to YES to scan EAN8 barcodes
zxingRecognizerSettings.scanEAN8 = false;

//Set this to YES to scan ITF barcodes
zxingRecognizerSettings.scanITF = false;

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

// Set this to YES to scan UPCA barcodes
zxingRecognizerSettings.scanUPCA = false;

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

// Add ZXingDecoderRecognizer setting to a list of used recognizer settings
settings.scanSettings.addRecognizerSettings(zxingRecognizerSettings)

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(PPZXingRecognizerResult)) {
            let zxingResult : PPZXingRecognizerResult=result as! PPZXingRecognizerResult
            
            switch zxingResult.barcodeType() {
    
                // Aztec
                case PPZXingBarcodeType.Aztec:
                    print("Barcode type is Aztec:")

                // Code 128
                case PPZXingBarcodeType.Code128:
                    print("Barcode type is Code 128:")
    
                // Code 39
                case PPZXingBarcodeType.Code39:
                    print("Barcode type is Code 39:")
    
                // Data Matrix
                case PPZXingBarcodeType.DataMatrix:
                    print("Barcode type is Data Matrix:")
    
                // EAN 13
                case PPZXingBarcodeType.EAN13:
                    print("Barcode type is EAN 13:")
    
                // EAN 8
                case PPZXingBarcodeType.EAN8:
                    print("Barcode type is EAN 8:")
    
                // ITF
                case PPZXingBarcodeType.ITF:
                    print("Barcode type is ITF:")
    
                // QR code
                case PPZXingBarcodeType.QR:
                    print("Barcode type is QR:")
    
                // UPCA
                case PPZXingBarcodeType.UPCA:
                    print("Barcode type is UPCA:")
    
                // UPCE
                case PPZXingBarcodeType.UPCE:
                    print("Barcode type is UPCE:")
            }
            // If you don't know the exact encoding of the text use stringUsingGuessedEncoding
            print(zxingResult.stringUsingGuessedEncoding());

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

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

    // resume scanning while preserving internal recognizer state
    scanConroller.resumeScanningAndResetState(false);
}
Clone this wiki locally