-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathMainActivity.kt
More file actions
311 lines (270 loc) · 13.7 KB
/
MainActivity.kt
File metadata and controls
311 lines (270 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package io.scanbot.example
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import io.scanbot.example.databinding.*
import io.scanbot.example.fragments.*
import io.scanbot.genericdocument.entity.*
import io.scanbot.sap.*
import io.scanbot.sdk.*
import io.scanbot.sdk.check.*
import io.scanbot.sdk.check.entity.*
import io.scanbot.sdk.creditcard.entity.CreditCard
import io.scanbot.sdk.documentdata.*
import io.scanbot.sdk.documentdata.entity.*
import io.scanbot.sdk.ehicscanner.*
import io.scanbot.sdk.genericdocument.entity.*
import io.scanbot.sdk.mc.*
import io.scanbot.sdk.ui.registerForActivityResultOk
import io.scanbot.sdk.ui.view.check.*
import io.scanbot.sdk.ui.view.check.configuration.CheckScannerConfiguration
import io.scanbot.sdk.ui.view.documentdata.*
import io.scanbot.sdk.ui.view.documentdata.configuration.DocumentDataExtractorConfiguration
import io.scanbot.sdk.ui.view.hic.*
import io.scanbot.sdk.ui.view.hic.configuration.*
import io.scanbot.sdk.ui.view.mc.*
import io.scanbot.sdk.ui.view.mc.configuration.*
import io.scanbot.sdk.ui.view.vin.*
import io.scanbot.sdk.ui.view.vin.configuration.*
import io.scanbot.sdk.ui_v2.common.ScanbotColor
import io.scanbot.sdk.ui_v2.common.activity.*
import io.scanbot.sdk.ui_v2.creditcard.CreditCardScannerActivity
import io.scanbot.sdk.ui_v2.creditcard.configuration.CreditCardScannerScreenConfiguration
import io.scanbot.sdk.ui_v2.mrz.*
import io.scanbot.sdk.ui_v2.mrz.configuration.*
import io.scanbot.sdk.ui_v2.textpattern.TextPatternScannerActivity
import io.scanbot.sdk.ui_v2.textpattern.configuration.TextPatternScannerScreenConfiguration
class MainActivity : AppCompatActivity() {
private val scanbotSdk: ScanbotSDK by lazy { ScanbotSDK(this) }
private val mrzDefaultUiResultLauncher: ActivityResultLauncher<MrzScannerScreenConfiguration>
private val creditCardUiResultLauncher: ActivityResultLauncher<CreditCardScannerScreenConfiguration>
private val textDataScannerResultLauncher: ActivityResultLauncher<TextPatternScannerScreenConfiguration>
private val vinScannerResultLauncher: ActivityResultLauncher<VinScannerConfiguration>
private val medicalCertificateScannerActivityResultLauncher: ActivityResultLauncher<MedicalCertificateScannerConfiguration>
private val ehicScannerResultLauncher: ActivityResultLauncher<HealthInsuranceCardScannerConfiguration>
private val dataExtractorResultLauncher: ActivityResultLauncher<DocumentDataExtractorConfiguration>
private val checkScannerResultLauncher: ActivityResultLauncher<CheckScannerConfiguration>
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
findViewById<View>(R.id.mrz_camera_default_ui).setOnClickListener {
val mrzCameraConfiguration = MrzScannerScreenConfiguration()
mrzCameraConfiguration.cameraConfiguration.apply {
flashEnabled = false
}
mrzCameraConfiguration.mrzExampleOverlay =
MrzFinderLayoutPreset.threeLineMrzFinderLayoutPreset()
mrzCameraConfiguration.topBar.backgroundColor = ScanbotColor(Color.BLACK)
mrzDefaultUiResultLauncher.launch(mrzCameraConfiguration)
}
findViewById<View>(R.id.text_pattern_scanner_default_ui).setOnClickListener {
// Create the default configuration object.
val textPatternScannerConfiguration = TextPatternScannerScreenConfiguration()
// Configure what string should be passed as successfully scanned text.
/* textPatternScannerConfiguration.scannerConfiguration.validator = CustomContentValidator().apply {
val pattern = Pattern.compile("^[0-9]{4}$") // e.g. 4 digits
this.callback = object : ContentValidationCallback {
override fun clean(rawText: String): String {
return rawText.replace(" ", "")
}
override fun validate(text: String): Boolean {
val matcher = pattern.matcher(text)
return matcher.find()
}
}
}*/
textPatternScannerConfiguration.topBar.backgroundColor = ScanbotColor(Color.BLACK)
textDataScannerResultLauncher.launch(textPatternScannerConfiguration)
}
findViewById<View>(R.id.mrz_camera_compose_ui).setOnClickListener {
val intent = Intent(this, MrzScannerComposeActivity::class.java)
startActivity(intent)
}
findViewById<View>(R.id.vin_scanner_default_ui).setOnClickListener {
val vinScannerConfiguration = VinScannerConfiguration()
vinScannerConfiguration.setTopBarBackgroundColor(
ContextCompat.getColor(this, R.color.colorPrimaryDark)
)
vinScannerConfiguration.setTopBarButtonsColor(
ContextCompat.getColor(this, R.color.greyColor)
)
vinScannerResultLauncher.launch(vinScannerConfiguration)
}
findViewById<View>(R.id.credit_card_scanner_default_ui).setOnClickListener {
val creditCardScannerConfiguration = CreditCardScannerScreenConfiguration()
creditCardScannerConfiguration.topBar.backgroundColor = ScanbotColor(Color.BLACK)
creditCardUiResultLauncher.launch(creditCardScannerConfiguration)
}
findViewById<View>(R.id.generic_document_default_ui).setOnClickListener {
val genericDocumentConfiguration = DocumentDataExtractorConfiguration()
genericDocumentConfiguration.setTopBarButtonsInactiveColor(
ContextCompat.getColor(this, android.R.color.white)
)
genericDocumentConfiguration.setTopBarBackgroundColor(
ContextCompat.getColor(this, R.color.colorPrimaryDark)
)
genericDocumentConfiguration.setFieldsDisplayConfiguration(
hashMapOf(
DePassport.NormalizedFieldNames.PHOTO to FieldProperties(
"My passport photo",
FieldProperties.DisplayState.AlwaysVisible
),
MRZ.NormalizedFieldNames.CHECK_DIGIT_GENERAL to FieldProperties(
"Check digit general",
FieldProperties.DisplayState.AlwaysVisible
)
)
)
dataExtractorResultLauncher.launch(genericDocumentConfiguration)
}
binding.ehicDefaultUi.setOnClickListener {
val ehicScannerConfig = HealthInsuranceCardScannerConfiguration()
ehicScannerConfig.setTopBarButtonsColor(Color.WHITE)
ehicScannerConfig.setRecognizerParameters(
EuropeanHealthInsuranceCardRecognizerConfiguration(
// Add your parameters here if needed
)
)
// ehicScannerConfig.setTopBarBackgroundColor(ContextCompat.getColor(this, android.R.color.holo_red_dark))
// ehicScannerConfig.setFinderTextHint("custom text")
// ...
ehicScannerResultLauncher.launch(ehicScannerConfig)
}
binding.checkRecognizerUi.setOnClickListener {
val config = CheckScannerConfiguration().apply {
setTopBarBackgroundColor(
ContextCompat.getColor(
this@MainActivity,
R.color.colorPrimaryDark
)
)
setTopBarButtonsColor(ContextCompat.getColor(this@MainActivity, R.color.greyColor))
}
checkScannerResultLauncher.launch(config)
}
binding.mcScannerUi.setOnClickListener {
val config = MedicalCertificateScannerConfiguration().apply {
setTopBarBackgroundColor(
ContextCompat.getColor(
this@MainActivity,
R.color.colorPrimaryDark
)
)
setTopBarButtonsColor(ContextCompat.getColor(this@MainActivity, R.color.greyColor))
}
medicalCertificateScannerActivityResultLauncher.launch(config)
}
}
override fun onResume() {
super.onResume()
if (!scanbotSdk.licenseInfo.isValid) {
showLicenseDialog()
}
binding.warningView.visibility =
if (scanbotSdk.licenseInfo.status != Status.StatusOkay) View.VISIBLE else View.GONE
}
private fun handleGenericDocScannerResult(result: List<DocumentDataExtractionResult>) {
result
Toast.makeText(
this,
result.joinToString {
it?.document?.fields?.joinToString { "${it.type.name} = ${it.value?.text}" } ?: ""
},
Toast.LENGTH_LONG
).show()
}
private fun showLicenseDialog() {
if (supportFragmentManager.findFragmentByTag(ErrorFragment.NAME) == null) {
val dialogFragment = ErrorFragment.newInstance()
dialogFragment.show(supportFragmentManager, ErrorFragment.NAME)
}
}
private fun showMrzDialog(genericDocument: GenericDocument) {
val dialogFragment = MRZDialogFragment.newInstance(genericDocument)
dialogFragment.show(supportFragmentManager, MRZDialogFragment.NAME)
}
private fun showEHICResultDialog(recognitionResult: EuropeanHealthInsuranceCardRecognitionResult) {
val dialogFragment = EHICResultDialogFragment.newInstance(recognitionResult)
dialogFragment.show(supportFragmentManager, EHICResultDialogFragment.NAME)
}
private fun handleMedicalCertificateResult(resultWrapper: MedicalCertificateScanningResult) {
showMedicalCertificateScannerResult(resultWrapper!!)
}
private fun showMedicalCertificateScannerResult(recognitionResult: MedicalCertificateScanningResult) {
val dialogFragment = MedicalCertificateResultDialogFragment.newInstance(recognitionResult)
dialogFragment.show(supportFragmentManager, MedicalCertificateResultDialogFragment.NAME)
}
private fun handleCheckScannerResult(result: CheckScanningResult) {
showCheckScannerResult(result)
}
private fun showCheckScannerResult(recognitionResult: CheckScanningResult) {
val document = recognitionResult.check?.let { Check(it) } // Convert to the document model
Toast.makeText(this, recognitionResult.toString(), Toast.LENGTH_SHORT).show()
}
init {
creditCardUiResultLauncher =
registerForActivityResult(CreditCardScannerActivity.ResultContract()) { resultEntity: CreditCardScannerActivity.Result ->
if (resultEntity.resultOk) {
resultEntity.result?.creditCard?.let {
val creditCard = CreditCard(it)
val cardNumber: String = creditCard.cardNumber.value.text
val cardholderName: String = creditCard.cardholderName?.value?.text ?: ""
val expiryDate: String? = creditCard.expiryDate?.value?.text
Toast.makeText(
this,
"Card Number: $cardNumber, Cardholder Name: $cardholderName, Expiry Date: $expiryDate",
Toast.LENGTH_LONG
).show()
}
}
}
mrzDefaultUiResultLauncher =
registerForActivityResultOk(MrzScannerActivity.ResultContract()) { resultEntity ->
if (resultEntity.resultOk) {
resultEntity.result?.mrzDocument?.let {
showMrzDialog(it)
}
}
}
textDataScannerResultLauncher =
registerForActivityResult(TextPatternScannerActivity.ResultContract()) { resultEntity: TextPatternScannerActivity.Result ->
if (resultEntity.resultOk) {
resultEntity.result?.rawText?.let {
Toast.makeText(this, it, Toast.LENGTH_LONG).show()
}
}
}
vinScannerResultLauncher =
registerForActivityResultOk(VinScannerActivity.ResultContract()) { resultEntity ->
val vinScanResult = resultEntity.result!!
Toast.makeText(
this@MainActivity,
"VIN Scanned: ${vinScanResult.textResult.rawText}",
Toast.LENGTH_LONG
).show()
}
ehicScannerResultLauncher =
registerForActivityResultOk(HealthInsuranceCardScannerActivity.ResultContract()) { resultEntity ->
showEHICResultDialog(resultEntity.result!!)
}
dataExtractorResultLauncher =
registerForActivityResultOk(DocumentDataExtractorActivity.ResultContract()) { resultEntity ->
handleGenericDocScannerResult(resultEntity.result!!)
}
medicalCertificateScannerActivityResultLauncher =
registerForActivityResultOk(MedicalCertificateScannerActivity.ResultContract()) { resultEntity ->
handleMedicalCertificateResult(resultEntity.result!!)
}
checkScannerResultLauncher =
registerForActivityResultOk(CheckScannerActivity.ResultContract()) { resultEntity ->
handleCheckScannerResult(resultEntity.result!!)
}
}
}