1. Swift
  2. AI Scanning APIs

Swift

AI Scanning APIs

The Vision SDK supports OCR via cloud-based APIs to extract structured data from various documents like shipping labels, BOLs (Bill of Lading), and item labels. This mode leverages RESTful APIs and requires a valid API key and environment to be configured before making any calls.


πŸ” SDK Configuration for Cloud OCR

Before initiating an OCR scan, you must configure the SDK with your cloud API credentials:

        Constants.apiKey = "your_api_key"
Constants.apiEnvironment = .staging // or .production

      

πŸ“Œ You can obtain your API key and environment details from cloud.packagex.io.


πŸ“Έ Capturing an Image for OCR

The SDK provides a CodeScannerView that enables you to capture an image either manually or via auto capture mode. Here's how to set it up:

        let scannerView = CodeScannerView(frame: view.bounds)
view.addSubview(scannerView)

scannerView.configure(
    delegate: self,
    sessionPreset: .high,
    captureMode: .auto, // or .manual
    captureType: .single,
    scanMode: .photo // or .ocr Required for OCR
)

scannerView.startRunning()

scannerView.capturePhoto()

      

🧭 Delegate Method for OCR Photo Capture

When a photo is successfully captured, the following delegate method is triggered:

        func codeScannerView(_ scannerView: CodeScannerView, didCaptureOCRImage image: UIImage, withCroppedImge croppedImage: UIImage?, withbarCodes barcodes: [String])


      

Use this method to send the captured image to the cloud OCR endpoint using the provided SDK methods.


🧾 Document Types Supported

The Vision SDK cloud OCR supports the following document types:

πŸ“¦ Shipping Labels

Extracts structured data such as tracking numbers, courier names, addresses, etc. More details Here

Here’s an explanation of each parameter in the callScanAPIWith method used to scan Shipping Labels via the Vision SDK's Cloud OCR API:


πŸ“¦ callScanAPIWith(...) – Shipping Label API Method

        
func callScanAPIWith(
  _ image: UIImage, 
  andBarcodes barcodes: [String], 
  andApiKey apiKey: String? = nil, 
  andToken token: String? = nil, 
  andLocationId locationId: String? = nil, 
  andOptions options: [String: Any], 
  andMetaData metaData: [String: Any] = [:], 
  andRecipient recipient: [String: Any]? = nil, 
  andSender sender: [String: Any]? = nil, 
  withImageResizing shouldResizeImage: Bool = true, 
  _ completion: @escaping ((_ data: Data?, _ error: NSError?)-> Void))


      

πŸ” Parameter Breakdown

Parameter Description
image The captured UIImage of the shipping label to be scanned.
barcodes A list of string barcode values (if detected beforehand) to include in the OCR request.
apiKey (Optional) API key for authenticating the request.
token (Optional) Bearer token for authenticated sessions (used as an alternative to apiKey).
locationId (Optional) Location identifier where the scan is being performed. Leave nil if not applicable.
options Additional settings to control scanning behavior. You can learn more here: Shipping Label Inference Options.
metaData (Optional) A dictionary of key-value pairs to associate custom metadata with the scan.
shouldResizeImage Boolean flag to control if the image should be resized before upload. Default is true.
completion Completion handler that returns the response data or an error object. Shipping Label Output Model

πŸ“„ Bill of Lading (BOL)

Detects and extracts BOL-specific fields such as carrier information, consignee details, and reference numbers. More details Here

πŸ“„ getPredictionBillOfLadingCloud(...) – BOL API Method

        func getPredictionBillOfLadingCloud(
  _ image: UIImage, 
  andBarcodes barcodes: [String], 
  andApiKey apiKey: String? = nil, 
  andToken token: String? = nil, 
  andLocationId locationId: String? = nil, 
  andOptions options: [String: Any] = [:], 
  withImageResizing shouldResizeImage: Bool = true, 
  _ completion: @escaping ((_ data: Data?, _ error: NSError?)-> Void)
  )

      

πŸ” Parameter Breakdown

Parameter Description
image The UIImage of the Bill of Lading document to be scanned.
barcodes A list of barcode string values (if already detected or provided manually).
apiKey (Optional) API key for direct authentication.
token (Optional) Bearer token for authenticated sessions (alternative to apiKey).
locationId (Optional) Identifier for the scan’s location. Can be left nil if not applicable.
options Additional settings to control scanning behavior. You can learn more here: Bill of Lading Inference Options.
shouldResizeImage Boolean to decide if the image should be resized before upload. Default is true.
completion Completion handler returning the response data or an error if the scan fails. BOL Output Model

🏷️ Item Labels

Recognizes SKU, GTIN, price, brand, and other item-level information printed on labels. More details Here

🏷️ callItemLabelsAPIWith(...) – Item Label API Method

        func callItemLabelsAPIWith(
  _ image: UIImage,
  andApiKey apiKey: String? = nil,
  andToken token: String? = nil,
  withImageResizing shouldResizeImage: Bool = true,
  _ completion: @escaping ((_ data: Data?, _ error: NSError?) -> Void)
)

      

πŸ” Parameter Breakdown

Parameter Description
image The UIImage of the item label to be scanned.
apiKey (Optional) API key for authentication.
token (Optional) Bearer token as an alternative authentication method.
shouldResizeImage Boolean to specify if the image should be resized before being sent (default is true).
completion Closure that returns either the API response data or an error object upon completion. Item label Response Model

🏷️ Document Classification

Recognizes the document type and returns the type of a logistics document. More details Here

🏷️ callDocumentClassificationAPIWith(...) – Item Label API Method

        func callDocumentClassificationAPIWith(
  _ image: UIImage, 
  andApiKey apiKey: String? = nil, 
  andToken token: String? = nil, 
  withImageResizing shouldResizeImage: Bool = true, 
  _ completion: @escaping ((_ data: Data?, _ error: NSError?)-> Void))

      

πŸ” Parameter Breakdown

Parameter Description
image The UIImage of the Document to be scanned.
apiKey (Optional) API key for authentication.
token (Optional) Bearer token as an alternative authentication method.
shouldResizeImage Boolean to specify if the image should be resized before being sent (default is true).
completion Closure that returns either the API response data or an error object upon completion