You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This repository includes a [sample app](https://github.com/what3words/w3w-android-samples/tree/main/ocr-sample) demonstrating the usage of the what3words OCR component.
24
23
25
-
## Usage
24
+
## Usages
26
25
27
-
There are two ways to use our MLKit OCR Component:
26
+
Adding a `W3WOcrScanner`to your app looks like the following:
28
27
29
-
1. As an Activity, **MLKitOcrScanActivity**, that should be used as an activity for result, which have minimum setup but doesn't allow style customisation. Our library handles all lifecycle and scan flow and will return the selected scanned three word address. Custom localisation and accessibility are available.
28
+
```kotlin
29
+
W3WOcrScanner(
30
+
ocrScanManager = ocrScanManager,
31
+
onDismiss = {},
32
+
onSuggestionSelected = {},
33
+
onError = {},
34
+
onSuggestionFound = {}
35
+
)
36
+
```
37
+
<details>
38
+
<summary>Creating a OcrScanManager</summary>
30
39
31
-
2. Using our Jetpack Compose Composable **W3WOcrScanner**, will allow all the above, but the results are returned as a callback (selection and errors) and will enable styling customisation, allowing to override all styles used on our composable with just a couple of extra steps to setup.
40
+
### Creating a OcrScanManager
32
41
33
-
### Using MLKitOcrScanActivity (#1)
42
+
OcrScanManager encapsulates the scanner’s state and logic within it. It uses the `W3WImageDataSource` to scan images for possible what3words addresses, and the `W3WTextDataSource` to validate detected addresses.
34
43
35
-
```Kotlin
36
-
classMainActivity : AppCompatActivity() {
44
+
```kotlin
45
+
val w3WImageDataSource =W3WMLKitImageDataSource.create(
//Options to filter the OCR scanning or like this example providing current location for more accurate results/distances to three word addresses.
64
-
val options =AutosuggestOptions().apply {
65
-
this.focus =Coordinates(51.23, 0.1)
66
-
}
67
-
68
-
//Per default the scanned three word address will not return coordinate information, if you set returnCoordinates to true when instanciating a new MLKitOcrScanActivity, it will return coordinates and this might results in charge against your API Key.
69
-
val returnCoordinates =true
70
-
71
-
//MLKitOcrScanActivity.newInstanceWithApi allows to provide all strings to be used internally for localisation and accessibility propuses. This should be used on a click actions, i.e: button click.
72
-
val intent =MLKitOcrScanActivity.newInstanceWithApi(
//This example uses Latin MLKit library, check MLKit documentation of how to instanciate other libraries like Korean, Japanese, Devanagari or Chinese.
102
-
val textRecognizer = com.google.mlkit.vision.text.TextRecognizerOptionsInterface.LATIN
64
+
### Using OcrScannerState
103
65
104
-
//Options to filter the OCR scanning or like this example providing current location for more accurate results/distances to three word addresses.
105
-
val options =AutosuggestOptions().apply {
106
-
this.focus =Coordinates(51.23, 0.1)
107
-
}
66
+
If you prefer more control over the scanner's state, you can use `OcrScannerState` directly instead of using `OcrScanManager`. This allows you to manage the state externally and integrate the component into your existing architecture.
108
67
109
-
//Per default the scanned three word address will not return coordinate information, if you set returnCoordinates to true when instanciating a new MLKitOcrScanActivity, it will return coordinates and this might results in charge against your API Key.
110
-
val returnCoordinates =true
111
-
112
-
val dataProvider =What3WordsV3("YOUR_API_KEY_HERE", this)
//TODO: Dismissed by user, hide W3WOcrScanner using AnimatedVisibility or finish activity.
128
-
},
129
-
onSuggestionSelected = { scannedSuggestion ->
130
-
//TODO: Use scanned three word address info, hide W3WOcrScanner using AnimatedVisibility or finish activity.
131
-
}
132
-
)
133
-
}
68
+
#### Example Usage
69
+
```kotlin
70
+
val ocrScannerState = remember { OcrScannerState() }
71
+
72
+
W3WOcrScanner(
73
+
ocrScannerState = ocrScannerState,
74
+
onError = { error ->
75
+
// Handle the error
76
+
},
77
+
onDismiss = {
78
+
// Handle scanner dismissal
79
+
},
80
+
onFrameCaptured = { image ->
81
+
// Scanner has captured a frame, you will need to implement your own functions to detect the what3words addresses in the frame and then update the ocrScannerState
82
+
83
+
CompletableDeferred<Unit>().apply {
84
+
// Signal completion when processing is done
85
+
complete(Unit)
134
86
}
135
-
}
136
-
}
87
+
},
88
+
onSuggestionSelected = { suggestion ->
89
+
// Handle address selection
90
+
},
91
+
)
137
92
```
138
93
139
-
### Styling W3WOcrScanner (#2)
94
+
#### Comparison Between using OcrScanManager and OcrScannerState directly
0 commit comments