1. Vision SDK
  2. iOS

Vision SDK

iOS

Barcode and QR Code scanner framework for iOS. VisionSDK provides a way to detect barcodes and qr codes. It also provides the functionality for information extraction from different kind of logistic labels like shipping labels, inventory labels, bill of ladings, receipts & invoices

Development Requirements

  • iOS 13.0+
  • Swift: 5.4.2+
  • Xcode Version: 13.0+

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate VisionSDK into your Xcode project using CocoaPods, specify it in your Podfile:

        pod 'VisionSDK'

      

Swift Package Manager

The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler.

Once you have your Swift package set up, add the VisionSDK as a dependency by adding it to the dependencies value of your Package.swift.

        dependencies: [
    .package(url: "https://github.com/packagexlabs/vision-sdk-sample-code.git", .upToNextMajor(from: "1.0.0"))
]

      

Manual Framework Integration

  • In Xcode, move to "General > Build Phase > Linked Frameworks and Libraries"
  • Add the VisionSDK.xcframework from to your project
  • Mark it "Embed and Sign"
  • Write Import statement on your source file
        import VisionSDK

      

Usage

Update Info.plist

Add Privacy - Camera Usage Description to Info.plist file

PermissionSettings

Initialization

In order to use the OCR API for information extraction, you have to set following parameters

  • Constants.apiKey to your API key.
  • Constants.apiEnvironment you also need to specify the API environment that you have the API key for (sandbox or production).
NOTE

Please note that these have to be set before using the API call. You can generate your own API key at cloud.packagex.io. You can find the instruction guide here.

        Constants.apiKey = "your_api_key"
Constants.apiEnvironment = .production

      

Code Samples

You can find the sample code for using the vision-sdk on our github.

Functional Description

Configuration Method

        
scannerView.configure(delegate: VisionSDK.CodeScannerViewDelegate, input: VisionSDK.CodeScannerView.Input = .default, scanMode: VisionSDK.CodeScannerMode = .qrCode)


      

Parameters

delegate
Should be the class that confirms to the CodeScannerViewDelegate protocol

Input struct
Input struct defines the properties of scanner view. These properties are:

  • Input.focusImage UIImage
    Image to be displayed in the center of the view. If not provided, VisionSDK will use the default image. Note that focus rectangle frame is subject to change with respect to different scan modes.

  • Input.focusImageRect CGRECT
    Custom rect for the focus image. You can provide your preferred rect or use .zero for default. Note that default focus rectangle frame is subject to change with respect to different scan modes.

  • Input.shouldDisplayFocusImage Bool
    set true if you need focused region to be drawn.

  • Input.shouldScanInFocusImageRect Bool
    set true if you want to detect codes visible in focused region only. This will discard the codes detected to be outside of the focus image.

  • Input.isTextIndicationOn Bool
    Set false if you do not want to detect text in live camera feed. If set false codeScannerViewDidDetect(_ text: Bool, barCode: Bool, qrCode: Bool) method will send text parameter as false.

  • Input.isBarCodeOrQRCodeIndicationOn Bool
    Set false if you do not want to detect bar codes or qrcodes in live camera feed. Using this property my be helpful in cases if you want to perform manual capture based on code detection

  • Input.sessionPreset Session.Preset
    You can set session preset as per your requirement. Default is .high.

  • Input.nthFrameToProcess Int
    This is the nth number of the frame that is processed for detection of text, barcodes, and qrcodes in live camera feed if enabled by isTextIndicationOn or isBarCodeOrQRCodeIndicationOn. Processing every single frame may be costly in terms of CPU usage and battery consumption. Default value is 10 which means that from camera stream of usual 30 fps, every 10 frame is processed. Its value should be set between 1 - 30.

  • Input.captureMode
    Set it to .multiple if you want to allow multiple results from scan. In .manual case, you will have to manually trigger scanning using capturePhoto() method.

  • Input.captureType
    Image to be displayed in the center of the view. If not provided, VisionSDK will use the default image. Note that focus rectangle frame is subject to change with respect to different scan modes.

  • Input.scanMode
    Defines the scan mode. It has following options

    • .barCode - Detects barcodes only in this mode
    • .qrCode - Detects qr codes only in this mode
    • .ocr - Use this mode to capture photos for later user in OCR API call.
    • .autoBarCodeOrQRCode - Detects both bar codes and qr codes

Set Scan Mode

  • Sets the scan mode to desired mode. i.e
    • .barCode
    • .qrcode
    • .ocr
    • .autoBarCodeOrQRCode
        scannerView.setScanModeTo(_ mode: VisionSDK.CodeScannerMode)

      

Set Capture Mode

  • Sets the capture mode to desired mode. i.e
    • .manual
    • .auto
        scannerView.setCaptureModeTo(_ mode: VisionSDK.CaptureMode)

      

Set Capture Type

  • Sets the capture type to desired type.
        scannerView.setCaptureTypeTo(_ type: VisionSDK.CaptureType)

      

Start the scanning

  • Needs configure() method to be called before it. It starts the camera session and scanning.
        scannerView.startRunning()

      

Stop the scanning

  • Stops camera session and scanning.
        scannerView.stopRunning()

      

Rerstart the camera & scanning

  • Use this function to resume scanning
        scannerView.rescan()

      

Deconfigure the configurations

  • Removes all the configurations of scannerView and stops scanning.
        scannerView.deConfigure()

      

Capture the photo/image

  • Use this method to trigger code scan or photo capture when you are scanning for multiple codes, in manual capture or OCR mode.
        scannerView.capturePhoto()

      

Delegate Methods / Callbacks

  • This method returns with the codes scanned after successful scan
        func codeScannerView(_ scannerView: VisionSDK.CodeScannerView, didSuccess codes: [String])

      
  • This method is called when text, barcode or qr code is detected in the camera stream. Values depend on whether text or code indication is enabled while configuring the scanner view.
        func codeScannerViewDidDetect(_ text: Bool, barCode: Bool, qrCode: Bool)

      
  • This method is called when capturePhoto() method is called in OCR Mode. It return with the captured image and all the detected codes in it.
        func codeScannerView(_ scannerView: VisionSDK.CodeScannerView, didCaptureOCRImage image: UIImage, withbarCodes barcodes: [String])

      
  • This method is called when an error occurs in any stage of initializing or capturing the codes when there is none detected.
        func codeScannerView(_ scannerView: VisionSDK.CodeScannerView, didFailure error: VisionSDK.CodeScannerError)

      

OCR Method

  • This method is called on the shared instance of VisionAPIManager. It can be accessed using VisionAPIManager.shared syntax. This method recieves the captured image and the API Key or AuthToken as parameters. It returns with the OCR Response from PackageX API Response.
        func callScanAPIWith(_ image: UIImage, andBarcodes barcodes: [String], andApiKey apiKey: String? = nil, andToken token: String? = nil, andLocationId locationId: String? = nil, andOptions options: [String: String], _ completion: @escaping ((_ data: Data?, _ response: URLResponse?, _ error: NSError?)-> Void))