-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathBarcodeScannerViewActivity.kt
More file actions
135 lines (120 loc) · 5.03 KB
/
BarcodeScannerViewActivity.kt
File metadata and controls
135 lines (120 loc) · 5.03 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
package io.scanbot.example.ui
import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.ImageView
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.Result
import io.scanbot.common.onFailure
import io.scanbot.common.onSuccess
import io.scanbot.example.R
import io.scanbot.example.common.applyEdgeToEdge
import io.scanbot.example.model.BarcodeResultBundle
import io.scanbot.example.repository.BarcodeResultRepository
import io.scanbot.example.repository.BarcodeTypeRepository
import io.scanbot.sdk.ScanbotSDK
import io.scanbot.sdk.barcode.BarcodeItem
import io.scanbot.sdk.barcode.BarcodeScannerResult
import io.scanbot.sdk.barcode.setBarcodeFormats
import io.scanbot.sdk.barcode.ui.BarcodeScannerView
import io.scanbot.sdk.barcode.ui.IBarcodeScannerViewCallback
import io.scanbot.sdk.camera.CaptureInfo
import io.scanbot.sdk.image.ImageRef
class BarcodeScannerViewActivity : AppCompatActivity() {
private lateinit var barcodeScannerView: BarcodeScannerView
private lateinit var resultView: ImageView
private var flashEnabled = false
override fun onCreate(savedInstanceState: Bundle?) {
supportRequestWindowFeature(WindowCompat.FEATURE_ACTION_BAR_OVERLAY)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_barcode_scanner_view)
applyEdgeToEdge(findViewById(R.id.root_view))
barcodeScannerView = findViewById(R.id.barcode_scanner_view)
resultView = findViewById(R.id.result)
ScanbotSDK(this).createBarcodeScanner().onSuccess { scanner ->
scanner.setConfiguration(scanner.copyCurrentConfiguration().copy().apply {
setBarcodeFormats(barcodeFormats = BarcodeTypeRepository.selectedTypes.toList())
})
barcodeScannerView.apply {
initCamera()
initScanningBehavior(
scanner,
{ result, frame ->
result.onSuccess { data ->
handleSuccess(data)
}.onFailure {
if (it is Result.InvalidLicenseError) {
barcodeScannerView.post {
Toast.makeText(
this@BarcodeScannerViewActivity,
"1-minute trial license has expired!",
Toast.LENGTH_LONG
).show()
}
} else {
Toast.makeText(
this@BarcodeScannerViewActivity,
"Error occurred during scanner init",
Toast.LENGTH_LONG
).show()
}
}
false
},
object : IBarcodeScannerViewCallback {
override fun onCameraOpen() {
barcodeScannerView.viewController.useFlash(flashEnabled)
}
override fun onPictureTaken(
image: ImageRef,
captureInfo: CaptureInfo
) {
image.toBitmap().onSuccess { bitmap ->
resultView.post {
resultView.setImageBitmap(bitmap)
}
}
}
override fun onSelectionOverlayBarcodeClicked(barcodeItem: BarcodeItem) {
}
}
)
}
}
barcodeScannerView.viewController.apply {
barcodeScanningInterval = 1000
autoSnappingEnabled = false
}
}
override fun onResume() {
super.onResume()
if (ContextCompat.checkSelfPermission(
this,
Manifest.permission.CAMERA
) != PackageManager.PERMISSION_GRANTED
) {
// Use onActivityResult to handle permission rejection
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.CAMERA),
REQUEST_PERMISSION_CODE
)
}
}
private fun handleSuccess(result: BarcodeScannerResult) {
result.takeIf { it.barcodes.isNotEmpty() }?.let {
BarcodeResultRepository.barcodeResultBundle = BarcodeResultBundle(result)
val intent = Intent(this, BarcodeResultActivity::class.java)
startActivity(intent)
finish()
}
}
companion object {
private const val REQUEST_PERMISSION_CODE = 200
}
}