-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathMRZLiveScanningActivity.kt
More file actions
executable file
·108 lines (96 loc) · 3.91 KB
/
MRZLiveScanningActivity.kt
File metadata and controls
executable file
·108 lines (96 loc) · 3.91 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
package io.scanbot.example
import android.Manifest
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.core.view.WindowCompat
import io.scanbot.common.onSuccess
import io.scanbot.example.common.applyEdgeToEdge
import io.scanbot.sdk.ScanbotSDK
import io.scanbot.sdk.documentdata.entity.MRZ
import io.scanbot.sdk.geometry.AspectRatio
import io.scanbot.sdk.mrz.MrzScannerFrameHandler
import io.scanbot.sdk.ui.camera.FinderOverlayView
import io.scanbot.sdk.ui.camera.ScanbotCameraXView
import io.scanbot.sdk.util.log.LoggerProvider
class MRZLiveScanningActivity : AppCompatActivity() {
private val logger = LoggerProvider.logger
// @Tag("MRZ Custom UI")
private lateinit var cameraView: ScanbotCameraXView
private lateinit var finderOverlay: FinderOverlayView
private lateinit var mrzScannerFrameHandler: MrzScannerFrameHandler
private var flashEnabled = false
override fun onCreate(savedInstanceState: Bundle?) {
supportRequestWindowFeature(WindowCompat.FEATURE_ACTION_BAR_OVERLAY)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_mrz_live_scanner)
askPermission()
supportActionBar!!.hide()
applyEdgeToEdge(findViewById(R.id.root_view))
// Configure Initial camera state
cameraView = findViewById(R.id.camera)
cameraView.setCameraOpenCallback {
cameraView.postDelayed({
cameraView.useFlash(flashEnabled)
cameraView.continuousFocus()
}, 700)
}
// Configure finder overlay with required aspect ratios
finderOverlay = findViewById(R.id.finder_overlay)
finderOverlay.setRequiredAspectRatios(listOf(AspectRatio(5.0, 1.0)))
// Get the scanbot sdk instance
val scanbotSDK = ScanbotSDK(this)
// Configure mrz scanner
val mrzScanner = scanbotSDK.createMrzScanner().getOrThrow()
// Attach mrz scanner to the camera
mrzScannerFrameHandler = MrzScannerFrameHandler.attach(cameraView, mrzScanner)
// Handle live mrz scanning results
mrzScannerFrameHandler.addResultHandler { result, frame ->
result.onSuccess { scannerResult ->
// It is recommended to use a frame accumulation as well and expect at least 2 of 4 frames to be equal
val mrzDocument = scannerResult.document?.let { MRZ(it) }
if (scannerResult.success
&& mrzDocument?.checkDigitGeneral?.isValid == true
) {
mrzScannerFrameHandler.isEnabled = false
startActivity(MRZResultActivity.newIntent(this@MRZLiveScanningActivity, scannerResult))
}
}
false
}
findViewById<View>(R.id.flash).setOnClickListener {
flashEnabled = !flashEnabled
cameraView.useFlash(flashEnabled)
}
Toast.makeText(
this,
if (scanbotSDK.licenseInfo.isValid) "License is active" else "License is expired",
Toast.LENGTH_LONG
).show()
}
override fun onResume() {
super.onResume()
mrzScannerFrameHandler.isEnabled = true
}
// @EndTag("MRZ Custom UI")
private fun askPermission() {
if (ContextCompat.checkSelfPermission(
this,
Manifest.permission.CAMERA
) != PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CAMERA), 999)
}
}
companion object {
fun newIntent(context: Context?): Intent {
return Intent(context, MRZLiveScanningActivity::class.java)
}
}
}