-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathMainActivity.kt
More file actions
executable file
·221 lines (191 loc) · 8.53 KB
/
MainActivity.kt
File metadata and controls
executable file
·221 lines (191 loc) · 8.53 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
package io.scanbot.example
import android.Manifest
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Matrix
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.core.view.WindowCompat
import io.scanbot.common.Result
import io.scanbot.common.mapSuccess
import io.scanbot.common.onSuccess
import io.scanbot.example.common.applyEdgeToEdge
import io.scanbot.sdk.ScanbotSDK
import io.scanbot.sdk.camera.*
import io.scanbot.sdk.document.DocumentAutoSnappingController
import io.scanbot.sdk.document.DocumentScannerFrameHandler
import io.scanbot.sdk.documentscanner.DocumentDetectionResult
import io.scanbot.sdk.documentscanner.DocumentDetectionStatus
import io.scanbot.sdk.documentscanner.DocumentScanner
import io.scanbot.sdk.documentscanner.DocumentScannerParameters
import io.scanbot.sdk.documentscanner.DocumentScanningResult
import io.scanbot.sdk.geometry.AspectRatio
import io.scanbot.sdk.image.ImageRef
import io.scanbot.sdk.imageprocessing.ScanbotSdkImageProcessor
import io.scanbot.sdk.process.ImageProcessor
import io.scanbot.sdk.ui.camera.AdaptiveFinderOverlayView
import io.scanbot.sdk.ui.camera.ScanbotCameraXView
import io.scanbot.sdk.ui.camera.ShutterButton
import io.scanbot.sdk.util.PolygonHelper
/**
This example uses the SDK APIs introduced in Scanbot SDK v8.x.x.
Please check the official documentation for more details:
Result API https://docs.scanbot.io/android/document-scanner-sdk/detailed-setup-guide/result-api/
ImageRef API https://docs.scanbot.io/android/document-scanner-sdk/detailed-setup-guide/image-ref-api/
*/
class MainActivity : AppCompatActivity(), DocumentScannerFrameHandler.ResultHandler {
private lateinit var cameraView: ScanbotCameraXView
private lateinit var resultView: ImageView
private lateinit var userGuidanceHint: TextView
private lateinit var shutterButton: ShutterButton
private lateinit var scanbotSDK: ScanbotSDK
private lateinit var scanner: DocumentScanner
private var flashEnabled = false
private var lastUserGuidanceHintTs = 0L
private val requiredPageAspectRatios = listOf(AspectRatio(4.0, 3.0))
override fun onCreate(savedInstanceState: Bundle?) {
supportRequestWindowFeature(WindowCompat.FEATURE_ACTION_BAR_OVERLAY)
super.onCreate(savedInstanceState)
scanbotSDK = ScanbotSDK(this)
scanner = scanbotSDK.createDocumentScanner().getOrThrow()
askPermission()
setContentView(R.layout.activity_main)
supportActionBar?.hide()
applyEdgeToEdge(this.findViewById(R.id.root_view))
cameraView = findViewById<View>(R.id.camera) as ScanbotCameraXView
cameraView.setPreviewMode(CameraPreviewMode.FILL_IN)
// Lock the orientation of the UI (Activity) as well as the orientation of the taken picture to portrait.
cameraView.lockToPortrait(true)
cameraView.setCameraOpenCallback {
cameraView.postDelayed({
// Shutter sound is ON by default. You can disable it:
// cameraView.setShutterSound(false)
cameraView.continuousFocus()
cameraView.useFlash(flashEnabled)
}, 700)
}
resultView = findViewById<View>(R.id.result) as ImageView
val frameHandler = DocumentScannerFrameHandler.attach(cameraView, scanner)
// frameHandler.setAcceptedSizeScore(70)
val finderOverlayView = findViewById<View>(R.id.finder_overlay) as AdaptiveFinderOverlayView
finderOverlayView.setRequiredAspectRatios(requiredPageAspectRatios)
scanner.setConfiguration(scanner.copyCurrentConfiguration().apply {
parameters.apply {
this.aspectRatios = requiredPageAspectRatios
this.ignoreOrientationMismatch = true
}
})
frameHandler.addResultHandler(finderOverlayView.documentScannerFrameHandler)
frameHandler.addResultHandler(this)
DocumentAutoSnappingController.attach(cameraView, frameHandler).apply {
// setSensitivity(0.4f)
}
cameraView.addPictureCallback(object : PictureCallback() {
override fun onPictureTaken(image: ImageRef, captureInfo: CaptureInfo) {
processPictureTaken(image, captureInfo.imageOrientation)
}
})
userGuidanceHint = findViewById(R.id.userGuidanceHint)
shutterButton = findViewById(R.id.shutterButton)
shutterButton.setOnClickListener { cameraView.takePicture(false) }
shutterButton.visibility = View.VISIBLE
shutterButton.post { shutterButton.showAutoButton() }
findViewById<View>(R.id.flashToggle).setOnClickListener {
flashEnabled = !flashEnabled
cameraView.useFlash(flashEnabled)
}
}
private fun askPermission() {
if (ContextCompat.checkSelfPermission(
this,
Manifest.permission.CAMERA
) != PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CAMERA), 999)
}
}
override fun handle(
result: Result<DocumentDetectionResult>,
frame: FrameHandler.Frame
): Boolean {
// Here you are continuously notified about document scanning results.
// For example, you can show a user guidance text depending on the current scanning status.
result.onSuccess { value ->
userGuidanceHint.post {
showUserGuidance(value.status)
}
}
return false // typically you need to return false
}
private fun showUserGuidance(result: DocumentDetectionStatus) {
val autoSnappingEnabled = true
if (!autoSnappingEnabled) {
return
}
if (System.currentTimeMillis() - lastUserGuidanceHintTs < 400) {
return
}
when (result) {
DocumentDetectionStatus.OK -> {
userGuidanceHint.text = "Don't move"
userGuidanceHint.visibility = View.VISIBLE
}
DocumentDetectionStatus.OK_BUT_TOO_SMALL -> {
userGuidanceHint.text = "Move closer"
userGuidanceHint.visibility = View.VISIBLE
}
DocumentDetectionStatus.OK_BUT_BAD_ANGLES -> {
userGuidanceHint.text = "Perspective"
userGuidanceHint.visibility = View.VISIBLE
}
DocumentDetectionStatus.OK_BUT_OFF_CENTER -> {
userGuidanceHint.text = "Move to the center"
userGuidanceHint.visibility = View.VISIBLE
}
DocumentDetectionStatus.ERROR_NOTHING_DETECTED -> {
userGuidanceHint.text = "No Document"
userGuidanceHint.visibility = View.VISIBLE
}
DocumentDetectionStatus.ERROR_TOO_NOISY -> {
userGuidanceHint.text = "Background too noisy"
userGuidanceHint.visibility = View.VISIBLE
}
DocumentDetectionStatus.ERROR_TOO_DARK -> {
userGuidanceHint.text = "Poor light"
userGuidanceHint.visibility = View.VISIBLE
}
else -> userGuidanceHint.visibility = View.GONE
}
lastUserGuidanceHintTs = System.currentTimeMillis()
}
private fun processPictureTaken(image: ImageRef, imageOrientation: Int) {
scanner.setConfiguration(scanner.copyCurrentConfiguration().apply {
parameters.apply {
this.aspectRatios = requiredPageAspectRatios
this.ignoreOrientationMismatch = true
}
})
val image = scanner.run(image).mapSuccess { result ->
val polygon = result.pointsNormalized
polygon.takeIf { it.isNotEmpty() && it.size == 4 } ?: PolygonHelper.getFullPolygon()
}.mapSuccess { polygonCrop ->
var documentImage = ScanbotSdkImageProcessor.create()
.crop(image, polygonCrop)
.getOrReturn()
ScanbotSdkImageProcessor.create().resize(documentImage, 200).getOrReturn()
}.onSuccess { documentImage ->
resultView.post {
resultView.setImageBitmap(documentImage.toBitmap().getOrNull())
documentImage.close()
cameraView.continuousFocus()
cameraView.startPreview()
}
}
}
}