A Swift component library for what3words OCR. These components can work with either what3words' OCR SDK, or with iOS' Vision Framework.
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.
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
You must set the camera permission in your app's Info.plist
:
The component constructor needs an OCR object.
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)
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)
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)
}
}
An example called OcrComponent
can be found here in the Examples/OcrComponent
directory of this repository.
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.
link |
Stars: 0 |
Last commit: 3 weeks ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics