Skip to content

Commit 7080169

Browse files
Duy PhạmDuy Phạm
authored andcommitted
Update README
1 parent 1cd313b commit 7080169

File tree

1 file changed

+92
-111
lines changed

1 file changed

+92
-111
lines changed

README.md

Lines changed: 92 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
[![Maven Central](https://img.shields.io/maven-central/v/com.what3words/w3w-android-ocr-components.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22com.what3words%22%20AND%20a:%22w3w-android-ocr-components%22)
44

5-
### Android minumum SDK support
5+
### Android minimum SDK support
66
[![Generic badge](https://img.shields.io/badge/minSdk-24-green.svg)](https://developer.android.com/about/versions/marshmallow/android-7.0/)
77

8-
98
An Android library to scan what3words address using [MLKit V2](https://developers.google.com/ml-kit/vision/text-recognition/v2/android).
109

1110
<img src="https://github.com/what3words/w3w-android-ocr-components/blob/main/assets/ocr-component-demo.gif" width=40% height=40%>
@@ -18,148 +17,130 @@ To obtain an API key, please visit [https://what3words.com/select-plan](https://
1817
implementation 'com.what3words:w3w-android-ocr-components:$latest'
1918
```
2019

21-
## Sample using w3w-android-wrapper library
20+
## Sample app
2221

23-
[ocr-sample](https://github.com/what3words/w3w-android-samples/tree/main/ocr-sample)
22+
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.
2423

25-
## Usage
24+
## Usages
2625

27-
There are two ways to use our MLKit OCR Component:
26+
Adding a `W3WOcrScanner` to your app looks like the following:
2827

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>
3039

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
3241

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.
3443

35-
```Kotlin
36-
class MainActivity : AppCompatActivity() {
44+
```kotlin
45+
val w3WImageDataSource = W3WMLKitImageDataSource.create(
46+
context = context,
47+
recognizerOptions = TextRecognizerOptionsInterface.LATIN
48+
)
49+
50+
val w3WTextDataSource = W3WApiTextDataSource.create(context, W3W_API_KEY)
51+
52+
val ocrScanManager = rememberOcrScanManager(
53+
w3wImageDataSource = w3WImageDataSource,
54+
w3wTextDataSource = w3WTextDataSource,
55+
)
3756

38-
private val resultLauncher =
39-
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
40-
when {
41-
//registerForActivityResult success with result
42-
result.resultCode == Activity.RESULT_OK && result.data?.hasExtra(BaseOcrScanActivity.SUCCESS_RESULT_ID) == true -> {
43-
val suggestion =
44-
result.data!!.serializable<SuggestionWithCoordinates>(BaseOcrScanActivity.SUCCESS_RESULT_ID)
45-
//TODO: Handle suggestion scanned
46-
}
47-
//registerForActivityResult canceled with error
48-
result.resultCode == Activity.RESULT_CANCELED && result.data?.hasExtra(
49-
BaseOcrScanActivity.ERROR_RESULT_ID
50-
) == true -> {
51-
val error =
52-
result.data!!.getStringExtra(BaseOcrScanActivity.ERROR_RESULT_ID)
53-
//TODO: Handle error
54-
}
55-
//registerForActivityResult canceled by user.
56-
else -> {
57-
}
58-
}
59-
}
60-
61-
override fun onCreate(savedInstanceState: Bundle?) {
62-
...
63-
//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(
73-
context = this,
74-
mlKitLibrary = com.google.mlkit.vision.text.TextRecognizerOptionsInterface.LATIN,
75-
apiKey = "YOUR_API_KEY_HERE",
76-
options = options,
77-
returnCoordinates = returnCoordinates,
78-
scanStateFoundTitle = "Custom found title"
79-
)
80-
try {
81-
resultLauncher.launch(intent)
82-
} catch (e: ExceptionInInitializerError) {
83-
//TODO: Handle Error
84-
}
85-
}
86-
}
8757
```
8858

89-
### Using W3WOcrScanner Composable (#2)
59+
</details>
9060

91-
```Kotlin
92-
class MainActivity : ComponentActivity() {
93-
private lateinit var ocrWrapper: W3WOcrWrapper
61+
<details>
62+
<summary>Using OcrScannerState</summary>
9463

95-
override fun onDestroy() {
96-
super.onDestroy()
97-
if (::ocrWrapper.isInitialized) ocrWrapper.stop()
98-
}
99-
100-
override fun onCreate(savedInstanceState: Bundle?) {
101-
//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
10365

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.
10867

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)
113-
ocrWrapper = W3WOcrMLKitWrapper(this, textRecognizer)
114-
115-
setContent {
116-
YourTheme {
117-
W3WOcrScanner(
118-
ocrWrapper,
119-
dataProvider = dataProvider,
120-
modifier = Modifier.fillMaxSize(),
121-
options = options,
122-
returnCoordinates = returnCoordinates,
123-
onError = { error ->
124-
//TODO: Handle error
125-
},
126-
onDismiss = {
127-
//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)
13486
}
135-
}
136-
}
87+
},
88+
onSuggestionSelected = { suggestion ->
89+
// Handle address selection
90+
},
91+
)
13792
```
13893

139-
### Styling W3WOcrScanner (#2)
94+
#### Comparison Between using OcrScanManager and OcrScannerState directly
95+
| Feature/Aspect | **OcrScanManager** | **OcrScannerState** |
96+
|-------------------------------|-----------------------------------------------------------------|----------------------------------------------------------|
97+
| **Ease of Use** | Encapsulates scanner state and logic, easier to use out of the box. | Provides more granular control but requires extra setup. |
98+
| **State Management** | Internal state management, ideal for simple integrations. | External state management, better for complex apps. |
99+
| **Customizability** | Limited to predefined functionalities. | High, allows deep customization and integration. |
100+
| **Use Case** | Quick implementation without managing internal details. | Advanced use cases needing dynamic or external control. |
101+
| **Flexibility** | Limited flexibility.
140102

141-
```Kotlin
103+
</details>
104+
105+
<details>
106+
<summary>Styling W3WOcrScanner</summary>
107+
108+
### Styling W3WOcrScanner
109+
110+
You can customize the appearance of the `W3WOcrScanner` by providing parameters for strings, colors, and text styles.
111+
112+
```kotlin
142113
W3WOcrScanner(
143114
...
144-
//optional if you want to override any string of the scanner composable, to allow localisation and accessibility.
145115
scannerStrings = W3WOcrScannerDefaults.defaultStrings(
146116
scanStateFoundTitle = stringResource(id = R.string.scan_state_found),
147117
),
148-
//optional if you want to override any colors of the scanner composable.
149118
scannerColors = W3WOcrScannerDefaults.defaultColors(
150119
bottomDrawerBackground = W3WTheme.colors.background
151120
),
152-
//optional if you want to override any text styles.
153121
scannerTextStyles = W3WOcrScannerDefaults.defaultTextStyles(
154122
stateTextStyle = W3WTheme.typography.headline
155123
),
156-
//optional if you want to override any colors of the scanned list item composable.
157124
suggestionColors = SuggestionWhat3wordsDefaults.defaultColors(
158125
background = W3WTheme.colors.background
159126
),
160-
//optional if you want to override any text styles of the scanned list item composable.
161127
suggestionTextStyles = SuggestionWhat3wordsDefaults.defaultTextStyles(
162128
wordsTextStyle = W3WTheme.typography.headline
163129
)
164-
)
130+
)
165131
```
132+
133+
</details>
134+
135+
<details>
136+
<summary>Camera Permission</summary>
137+
138+
### Camera Permission
139+
140+
The OCR component requires camera permission to function. Add the following permission to your app’s `AndroidManifest.xml` file:
141+
142+
```xml
143+
<uses-permission android:name="android.permission.CAMERA" />
144+
```
145+
146+
</details>

0 commit comments

Comments
 (0)