-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathScannerActivity.kt
More file actions
84 lines (69 loc) · 3.64 KB
/
ScannerActivity.kt
File metadata and controls
84 lines (69 loc) · 3.64 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
package io.scanbot.example
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import io.scanbot.example.common.applyEdgeToEdge
import io.scanbot.sdk.ScanbotSDK
import io.scanbot.sdk.camera.CameraPreviewMode
import io.scanbot.sdk.geometry.AspectRatio
import io.scanbot.sdk.textpattern.ContentValidationCallback
import io.scanbot.sdk.textpattern.CustomContentValidator
import io.scanbot.sdk.textpattern.TextPatternScanner
import io.scanbot.sdk.textpattern.TextPatternScannerFrameHandler
import io.scanbot.sdk.ui.camera.IScanbotCameraView
import io.scanbot.sdk.ui.camera.ScanbotCameraXView
import io.scanbot.sdk.ui.camera.ZoomFinderOverlayView
class ScannerActivity : AppCompatActivity() {
// @Tag("Text Pattern Custom UI")
private lateinit var cameraView: IScanbotCameraView
private lateinit var resultTextView: TextView
private var useFlash = false
private lateinit var patternScanner: TextPatternScanner
private lateinit var patternScannerFrameHandler: TextPatternScannerFrameHandler
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_scanner)
supportActionBar!!.hide()
applyEdgeToEdge(findViewById(R.id.root_view))
cameraView = findViewById<ScanbotCameraXView>(R.id.cameraView)
resultTextView = findViewById(R.id.resultTextView)
val zoomFinderOverlay = findViewById<ZoomFinderOverlayView>(R.id.finder_overlay)
// The smaller finder view brings better performance and allows user to scan text more precise
zoomFinderOverlay.setRequiredAspectRatios(listOf(AspectRatio(4.0, 1.0)))
zoomFinderOverlay.zoomLevel = 1.8f
cameraView.setPreviewMode(CameraPreviewMode.FIT_IN)
patternScanner = ScanbotSDK(this).createTextPatternScanner().getOrThrow()
// TODO: set validation string and validation callback which matches the need of the task
// For the pattern: # - digits, ? - for any character. Other characters represent themselves
// In this example we are waiting for a string which starts with 1 or 2, and then 5 more digits
patternScanner.setConfiguration( patternScanner.copyCurrentConfiguration().copy(
// validator = PresetContentValidator(preset = ValidatorPreset.VEHICLE_IDENTIFICATION_NUMBER),
// validator = PatternContentValidator(pattern = "######"),
validator = CustomContentValidator(callback = object : ContentValidationCallback {
override fun clean(rawText: String): String {
return rawText.replace(" ", "_")
}
override fun validate(text: String): Boolean {
return text.firstOrNull() in listOf('1', '2') // TODO: add additional validation for the recognized text
}
})
))
patternScannerFrameHandler = TextPatternScannerFrameHandler.attach(cameraView, patternScanner)
patternScannerFrameHandler.addResultHandler { result, frame ->
val resultText: String = result.getOrNull()?.rawText ?: result.errorOrNull()?.localizedMessage ?: "No result"
runOnUiThread { resultTextView.text = resultText }
false
}
cameraView.setCameraOpenCallback {
cameraView.useFlash(useFlash)
cameraView.continuousFocus()
}
findViewById<Button>(R.id.flashButton).setOnClickListener { toggleFlash() }
}
private fun toggleFlash() {
useFlash = !useFlash
cameraView.useFlash(useFlash)
}
// @EndTag("Text Pattern Custom UI")
}