-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathDocumentCameraActivity.kt
More file actions
executable file
·189 lines (158 loc) · 7.11 KB
/
DocumentCameraActivity.kt
File metadata and controls
executable file
·189 lines (158 loc) · 7.11 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
package io.scanbot.example
import android.Manifest
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Color
import android.graphics.Matrix
import android.os.Bundle
import android.view.View
import android.widget.Button
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.onSuccess
import io.scanbot.example.common.applyEdgeToEdge
import io.scanbot.sdk.ScanbotSDK
import io.scanbot.sdk.camera.CaptureInfo
import io.scanbot.sdk.document.DocumentScannerFrameHandler
import io.scanbot.sdk.document.ui.DocumentScannerView
import io.scanbot.sdk.document.ui.IDocumentScannerViewCallback
import io.scanbot.sdk.documentscanner.DocumentDetectionStatus
import io.scanbot.sdk.documentscanner.DocumentEnhancer
import io.scanbot.sdk.documentscanner.DocumentScanner
import io.scanbot.sdk.documentscanner.DocumentStraighteningMode
import io.scanbot.sdk.documentscanner.DocumentStraighteningParameters
import io.scanbot.sdk.geometry.AspectRatio
import io.scanbot.sdk.image.ImageRef
import io.scanbot.sdk.process.ImageProcessor
import io.scanbot.sdk.ui.camera.ShutterButton
import io.scanbot.sdk.ui.view.base.configuration.CameraOrientationMode
class DocumentCameraActivity : AppCompatActivity() {
private var lastUserGuidanceHintTs = 0L
private var flashEnabled = false
private var autoSnappingEnabled = true
private val ignoreOrientationMistmatch = true
private lateinit var documentScannerView: DocumentScannerView
private lateinit var resultView: ImageView
private lateinit var userGuidanceHint: TextView
private lateinit var autoSnappingToggleButton: Button
private lateinit var shutterButton: ShutterButton
override fun onCreate(savedInstanceState: Bundle?) {
supportRequestWindowFeature(WindowCompat.FEATURE_ACTION_BAR_OVERLAY)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_camera)
askPermission()
supportActionBar!!.hide()
applyEdgeToEdge(findViewById(R.id.root_view))
val scanbotSdk = ScanbotSDK(this)
documentScannerView = findViewById(R.id.document_scanner_view)
resultView = findViewById<View>(R.id.result) as ImageView
val documentEnhancer = scanbotSdk.createDocumentEnhancer()
scanbotSdk.createDocumentScanner().onSuccess { documentScanner ->
documentScannerView.apply {
initCamera()
initScanningBehavior(
documentScanner,
{ result, frame ->
// 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 { data ->
}
false // typically you need to return false
},
object : IDocumentScannerViewCallback {
override fun onCameraOpen() {
documentScannerView.viewController.useFlash(flashEnabled)
}
override fun onPictureTaken(image: ImageRef, captureInfo: CaptureInfo) {
documentEnhancer.onSuccess { documentEnhancer ->
processPictureTaken(image, documentEnhancer)
}
// continue scanning
documentScannerView.postDelayed({
documentScannerView.viewController.startPreview()
}, 1000)
}
}
)
}
}
documentScannerView.polygonConfiguration.apply {
setPolygonFillColor(POLYGON_FILL_COLOR)
setPolygonFillColorOK(POLYGON_FILL_COLOR_OK)
}
documentScannerView.viewController.apply {
setAcceptedAngleScore(60.0)
setAcceptedSizeScore(75.0)
setIgnoreOrientationMismatch(ignoreOrientationMistmatch)
// Please note: https://docs.scanbot.io/document-scanner-sdk/android/features/document-scanner/autosnapping/#sensitivity
setAutoSnappingSensitivity(0.85f)
}
shutterButton = findViewById(R.id.shutterButton)
shutterButton.setOnClickListener { documentScannerView.viewController.takePicture(false) }
shutterButton.visibility = View.VISIBLE
findViewById<View>(R.id.flashToggle).setOnClickListener {
flashEnabled = !flashEnabled
documentScannerView.viewController.useFlash(flashEnabled)
}
autoSnappingToggleButton = findViewById(R.id.autoSnappingToggle)
autoSnappingToggleButton.setOnClickListener {
autoSnappingEnabled = !autoSnappingEnabled
setAutoSnapEnabled(autoSnappingEnabled)
}
autoSnappingToggleButton.post { setAutoSnapEnabled(autoSnappingEnabled) }
}
private fun askPermission() {
if (ContextCompat.checkSelfPermission(
this,
Manifest.permission.CAMERA
) != PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CAMERA), 999)
}
}
override fun onResume() {
super.onResume()
documentScannerView.viewController.onResume()
}
override fun onPause() {
super.onPause()
documentScannerView.viewController.onPause()
}
private fun processPictureTaken(image: ImageRef, documentEnhancer: DocumentEnhancer) {
// STRAIGHTEN SCANNED IMAGE ASSUMING DOCUMENT IS BENT
// Run document enhancer unwarping on original image:
val result = documentEnhancer.straighten(image, DocumentStraighteningParameters().apply {
straighteningMode = DocumentStraighteningMode.STRAIGHTEN
// uncomment if you want wo set specific aspect ratios for documents
// aspectRatios = listOf(AspectRatio(29.0, 21.0))
}).getOrNull()
resultView.post {
resultView.setImageBitmap(
result?.straightenedImage?.toBitmap()?.getOrNull()
)
}
}
private fun setAutoSnapEnabled(enabled: Boolean) {
documentScannerView.viewController.apply {
autoSnappingEnabled = enabled
isFrameProcessingEnabled = enabled
}
documentScannerView.polygonConfiguration.setPolygonViewVisible(enabled)
autoSnappingToggleButton.text = "Automatic ${if (enabled) "ON" else "OFF"}"
if (enabled) {
shutterButton.showAutoButton()
} else {
shutterButton.showManualButton()
userGuidanceHint.visibility = View.GONE
}
}
companion object {
private val POLYGON_FILL_COLOR = Color.parseColor("#55ff0000")
private val POLYGON_FILL_COLOR_OK = Color.parseColor("#4400ff00")
}
}