-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathMainActivity.kt
More file actions
133 lines (111 loc) · 5.01 KB
/
MainActivity.kt
File metadata and controls
133 lines (111 loc) · 5.01 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
package io.scanbot.example.ui
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import androidx.activity.result.PickVisualMediaRequest
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import io.scanbot.example.R
import io.scanbot.example.common.Const
import io.scanbot.example.common.applyEdgeToEdge
import io.scanbot.example.common.showToast
import io.scanbot.example.databinding.ActivityMainBinding
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.setBarcodeFormats
import io.scanbot.sdk.image.ImageRef
import io.scanbot.sdk.licensing.LicenseStatus
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
/**
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() {
private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }
private val scanbotSdk: ScanbotSDK by lazy { ScanbotSDK(this) }
private val selectGalleryImageResultLauncher = registerForActivityResult(ActivityResultContracts.PickVisualMedia()) { uri ->
if (!scanbotSdk.licenseInfo.isValid) {
this@MainActivity.showToast("1-minute trial license has expired!")
Log.e(Const.LOG_TAG, "1-minute trial license has expired!")
return@registerForActivityResult
}
if (uri == null) {
showToast("Error obtaining selected image!")
Log.e(Const.LOG_TAG, "Error obtaining selected image!")
return@registerForActivityResult
}
if (scanbotSdk.licenseInfo.isValid) {
lifecycleScope.launch { scanBarcodeAndShowResult(uri) }
} else {
this@MainActivity.showToast("1-minute trial license has expired!")
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
applyEdgeToEdge(findViewById(R.id.root_view))
binding.qrDemo.setOnClickListener {
val intent = Intent(applicationContext, BarcodeScannerActivity::class.java)
startActivity(intent)
}
binding.barcodeScannerViewDemo.setOnClickListener {
val intent = Intent(applicationContext, BarcodeScannerViewActivity::class.java)
startActivity(intent)
}
binding.barcodeCounterViewDemo.setOnClickListener {
val intent = Intent(applicationContext, BarcodeScanAndCountViewActivity::class.java)
startActivity(intent)
}
binding.settings.setOnClickListener {
val intent = Intent(this@MainActivity, BarcodeTypesActivity::class.java)
startActivity(intent)
}
binding.importImage.setOnClickListener {
// select an image from photo library and run document scanning on it:
selectGalleryImageResultLauncher.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly))
}
}
override fun onResume() {
super.onResume()
binding.warningView.isVisible = ScanbotSDK(this).licenseInfo.status == LicenseStatus.TRIAL
}
private suspend fun scanBarcodeAndShowResult(uri: Uri) {
withContext(Dispatchers.Main) {
binding.progressBar.isVisible = true
}
val imageRef = withContext(Dispatchers.IO) {
contentResolver.openInputStream(uri)?.use { inputStream ->
ImageRef.fromInputStream(inputStream)
}
}
if (imageRef == null) {
withContext(Dispatchers.Main) {
binding.progressBar.isVisible = false
showToast("Error opening selected image!")
Log.e(Const.LOG_TAG, "Cannot open input stream from URI: $uri")
}
return
}
withContext(Dispatchers.Default) {
val scanner = scanbotSdk.createBarcodeScanner().getOrThrow()
scanner.setConfiguration(scanner.copyCurrentConfiguration().copy().apply {
setBarcodeFormats(barcodeFormats = BarcodeTypeRepository.selectedTypes.toList())
})
val result = scanner.run(imageRef).getOrNull()
BarcodeResultRepository.barcodeResultBundle = result?.let { BarcodeResultBundle(it, imageRef) }
}
withContext(Dispatchers.Main) {
startActivity(Intent(this@MainActivity, BarcodeResultActivity::class.java))
binding.progressBar.isVisible = false
}
}
}