-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathAutoSnappingCheckRecognizerActivity.kt
More file actions
146 lines (123 loc) · 5.18 KB
/
AutoSnappingCheckRecognizerActivity.kt
File metadata and controls
146 lines (123 loc) · 5.18 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
package io.scanbot.example
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Bundle
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
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.camera.CameraPreviewMode
import io.scanbot.sdk.camera.CaptureInfo
import io.scanbot.sdk.camera.PictureCallback
import io.scanbot.sdk.document.DocumentAutoSnappingController
import io.scanbot.sdk.document.DocumentScannerFrameHandler
import io.scanbot.sdk.image.ImageRef
import io.scanbot.sdk.ui.PolygonView
import io.scanbot.sdk.ui.camera.ScanbotCameraXView
class AutoSnappingCheckScannerActivity : AppCompatActivity() {
private lateinit var scanbotSDK: ScanbotSDK
private lateinit var cameraView: ScanbotCameraXView
private lateinit var polygonView: PolygonView
private lateinit var resultView: TextView
private lateinit var frameHandler: DocumentScannerFrameHandler
private lateinit var autoSnappingController: DocumentAutoSnappingController
private var flashEnabled = false
override fun onCreate(savedInstanceState: Bundle?) {
supportRequestWindowFeature(WindowCompat.FEATURE_ACTION_BAR_OVERLAY)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_autosnapping_check_scanner)
supportActionBar?.hide()
applyEdgeToEdge(this.findViewById(R.id.root_view))
cameraView = findViewById<ScanbotCameraXView>(R.id.camera).also { cameraView ->
cameraView.setPreviewMode(CameraPreviewMode.FIT_IN)
cameraView.setCameraOpenCallback {
cameraView.postDelayed({
cameraView.useFlash(flashEnabled)
cameraView.continuousFocus()
}, 700)
}
}
resultView = findViewById<View>(R.id.result) as TextView
scanbotSDK = ScanbotSDK(this)
val documentScanner = scanbotSDK.createDocumentScanner().getOrThrow()
polygonView = findViewById<View>(R.id.polygonView) as PolygonView
frameHandler =
DocumentScannerFrameHandler.attach(cameraView, documentScanner)
documentScanner.setConfiguration(documentScanner.copyCurrentConfiguration().apply {
parameters.apply {
this.ignoreOrientationMismatch = true
this.acceptedSizeScore = 75
this.acceptedAngleScore = 60
}
})
frameHandler.addResultHandler(polygonView.documentScannerResultHandler)
autoSnappingController =
DocumentAutoSnappingController.attach(cameraView, frameHandler)
// Please note: https://docs.scanbot.io/document-scanner-sdk/android/features/document-scanner/ui-components/#sensitivity
autoSnappingController.setSensitivity(0.85f)
cameraView.addPictureCallback(object : PictureCallback() {
override fun onPictureTaken(image: ImageRef, captureInfo: CaptureInfo) {
frameHandler.isEnabled = false
processPictureTaken(image)
runOnUiThread {
polygonView.visibility = View.GONE
}
}
})
findViewById<View>(R.id.flash).setOnClickListener {
flashEnabled = !flashEnabled
cameraView.useFlash(flashEnabled)
}
frameHandler.addResultHandler { result, frame ->
result.onSuccess {
if (!scanbotSDK.licenseInfo.isValid) {
frameHandler.isEnabled = false
runOnUiThread {
Toast.makeText(
this@AutoSnappingCheckScannerActivity,
"License is expired",
Toast.LENGTH_LONG
).show()
finish()
}
}
}
false
}
}
private fun processPictureTaken(image: ImageRef) {
val checkScanner = scanbotSDK.createCheckScanner().getOrThrow()
val checkResult = checkScanner.run(image).getOrNull()
if (checkResult?.check != null) {
CheckScannerResultActivity.tempDocumentImage = checkResult.croppedImage?.toBitmap()?.getOrNull()
startActivity(CheckScannerResultActivity.newIntent(this, checkResult))
} else {
runOnUiThread {
Toast.makeText(
this,
"Check is not found - please, try agian",
Toast.LENGTH_LONG
).show()
}
}
// continue scanning
cameraView.postDelayed({
cameraView.continuousFocus()
cameraView.startPreview()
frameHandler.isEnabled = true
polygonView.visibility = View.VISIBLE
}, 1000)
}
companion object {
@JvmStatic
fun newIntent(context: Context?): Intent {
return Intent(context, AutoSnappingCheckScannerActivity::class.java)
}
}
}