Barcode scanning

Real-time barcode scanning

The SDK can detect barcodes located on the packaging of packaged foods. To implement real-time barcode scanning, the camera setup for continuous image recognition is needed.

Camera preview

Add the UI element that is responsible for rendering the camera frames:

var videoLayer: AVCaptureVideoPreviewLayer?

func setupPreviewLayer() {
    guard videoLayer == nil else { return }
    if let videoLayer = passioSDK.getPreviewLayer() {
        self.videoLayer = videoLayer
        videoLayer.frame = view.bounds
        view.layer.insertSublayer(videoLayer, at: 0)
    }
}

Start barcode scanning session

To start a barcode scanning session, a callback for the results needs to be registered. Once the camera preview is active, if there is a registered barcode callback, the SDK will start analysing the frames and providing the results:

/// Use this function to detect barcodes by pointing the camera at a barcode
/// - Parameters:
///   - recognitionDelegate: ``BarcodeRecognitionDelegate``, Add self to implement the BarcodeRecognitionDelegate
///   - completion: If success, it will return array of `BarcodeCandidate` objects
public func startBarcodeScanning(recognitionDelegate: BarcodeRecognitionDelegate,
                                 completion: @escaping (Bool) -> Void)


/// Implement the BarcodeRecognitionDelegate protocol to receive delegate method from the startBarcodeScanning
public protocol BarcodeRecognitionDelegate: AnyObject {
    /// Delegate function for food recognition
    /// - Parameters:
    ///   - candidates: Barcode candidates if available
    func recognitionResults(barcodeCandidates: [BarcodeCandidate]?)
}

On smaller packages, barcodes can be very small. When trying to scan these barcodes, the user usually brings to package to close to the camera creating focus issues. To avoid this, but the camera zoom level to 2.

Barcode scanning from an image

To recognise a barcode in an image, refer to the recognizeImageRemote function. The result of the barcode scanning process will come in the packagedFoodItem attribute, and the resultType will be Barcode.

UI Example

  1. Set up the camera preview and detection session to recognise barcodes

  2. Create a result view with two states: search and result

  3. If the candidate.barcodeCandidates is empty, show the search state. Else, fetch the PassioFoodItem, and use it's name and icon to show the result

Example of an image that produces multiple BarcodeCandidates:

Barcode example image

Last updated