Skip to content

A UI component for what3words OCR. Uses the camera to scan for three word addresses.

Notifications You must be signed in to change notification settings

what3words/w3w-swift-components-ocr

Repository files navigation

CircleCI

what3words w3w-swift-components-ocr

Overview

A Swift component library for what3words OCR. These components can work with either what3words' OCR SDK, or with iOS' Vision Framework.

what3words OCR XCFramework

If you want to use what3words' OCR XCFramework W3WOcrSdk.xcframework, please contact what3words to get it. Otherwise you can use this component as is and rely on iOS's Vision Framework.

Swift Package Manager

Use Swift Package Manager and add the URL below:

https://github.com/what3words/w3w-swift-components-ocr.git

Import the libraries wherever you use the components:

import W3WSwiftComponentsOcr
import W3WSwiftApi

Info.plist

You must set the camera permission in your app's Info.plist:

Using The Component

The component constructor needs an OCR object.

Using the API with Vision Framework:

Our W3WOcrNative class that uses iOS' Vision Framework requires our API (or SDK) to be passed into the constructor.

  let api = What3WordsV3(apiKey: "YourApiKey")
  let ocr = W3WOcrNative(api)
  let ocrViewController = W3WOcrViewController(ocr: ocr)

Using the SDK with what3words' OCR SDK:

The what3words OCR SDK requires only a path to the OCR data files, and optionally a language parameter (ISO 2 letter language code).

  let ocr = W3WOcr(dataPath: "/path/to/ocr/datafiles", language: "en")
  let ocrViewController = W3WOcrViewController(ocr: ocr)

Typical Usage

Here's a typical use example set in a UIViewController's IBOutlet function that is connected to a UIButton (presuming the initialisation code above was used somewhere in the class):

@IBAction func scanButtonPressed(_ sender: Any) {

  // show the OCR ViewController
  self.show(ocrViewController, sender: self)
  
  // start the OCR processing images
  ocrViewController.start()
  
  // when it finds an address, show it in the viewfinder
  ocrViewController.onSuggestions = { [weak self] suggestions in
    if let suggestion = suggestions.first {
      self?.ocrViewController.show(suggestion: suggestion)
      self?.ocrViewController.stop()
  }

  // if there is an error show the user
  ocrViewController.onError = { [weak self] error in
    self?.ocrViewController.stop()
    self?.showError(error: error)
  }
}

Example Code

An example called OcrComponent can be found here in the Examples/OcrComponent directory of this repository.

OCR Reference

Both W3WOcr and W3WOcrNative conform to W3WOcrProtocol. This guarantees the following functions:

set(language: String) - sets a language to use. Langauge codes can be retrieved using the availableLangauges() function.

availableLanguages() -> [String] - returns an array of language codes that the OCR accepts.

About

A UI component for what3words OCR. Uses the camera to scan for three word addresses.

Resources

Stars

Watchers

Forks

Packages

No packages published