-
Notifications
You must be signed in to change notification settings - Fork 0
/
DetectionOnTheImageActivity.kt
113 lines (96 loc) · 4.37 KB
/
DetectionOnTheImageActivity.kt
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
package com.example.scanbot.usecases
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.provider.MediaStore
import android.widget.Button
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContract
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.scanbot.ExampleUtils
import com.example.scanbot.R
import com.example.scanbot.usecases.adapter.BarcodeItemAdapter
import io.scanbot.sdk.barcode.ScanbotBarcodeDetector
import io.scanbot.sdk.barcode_scanner.ScanbotBarcodeScannerSDK
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
// This activity demonstrates how to detect barcodes on an image
class DetectionOnTheImageActivity : AppCompatActivity() {
private lateinit var scanbotBarcodeScannerSDK: ScanbotBarcodeScannerSDK
private val resultAdapter by lazy { BarcodeItemAdapter() }
private lateinit var resultView: RecyclerView
private lateinit var galleryImageLauncher: ActivityResultLauncher<Unit>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_detection_on_the_image)
scanbotBarcodeScannerSDK = ScanbotBarcodeScannerSDK(this)
// Create a barcode detector instance
val barcodeDetector = scanbotBarcodeScannerSDK.createBarcodeDetector()
barcodeDetector.modifyConfig {
// Specify the barcode format you want to scan
// setBarcodeFormats(listOf(BarcodeFormat.QR_CODE))
}
findViewById<Button>(R.id.import_image_button).setOnClickListener {
galleryImageLauncher.launch(Unit)
}
// Register a callback to receive the result of the image selection
galleryImageLauncher = registerForActivityResult(ImportImageContract(this)) { resultEntity ->
// Clear the previous results
resultAdapter.setBarcodeItems(listOf())
// Process the selected image on a background thread
lifecycleScope.launch(Dispatchers.Default) {
resultEntity?.let { bitmap ->
processImage(barcodeDetector, bitmap)
}
}
}
resultView = findViewById(R.id.barcode_recycler_view)
resultView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
resultView.adapter = resultAdapter
}
private fun processImage(
barcodeDetector: ScanbotBarcodeDetector,
bitmap: Bitmap
) {
if (!scanbotBarcodeScannerSDK.licenseInfo.isValid) {
ExampleUtils.showLicenseExpiredToastAndExit(this@DetectionOnTheImageActivity)
return
}
barcodeDetector.detectFromBitmap(bitmap, 0)?.let {
runOnUiThread {
resultAdapter.setBarcodeItems(it.barcodeItems)
}
}
}
// This class is used to select an image from the photo library
class ImportImageContract(private val context: Context) : ActivityResultContract<Unit, Bitmap?>() {
override fun createIntent(context: Context, input: Unit): Intent {
// An image is selected from the photo library and document detection is run on it:
val imageIntent = Intent()
imageIntent.type = "image/*"
imageIntent.action = Intent.ACTION_GET_CONTENT
imageIntent.putExtra(Intent.EXTRA_LOCAL_ONLY, false)
imageIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, false)
return Intent.createChooser(imageIntent, "Select a picture")
}
override fun parseResult(resultCode: Int, intent: Intent?): Bitmap? {
return if (resultCode == Activity.RESULT_OK && intent != null) {
return processGalleryResult(intent)
} else {
null
}
}
// This method is called when the image selection is complete
private fun processGalleryResult(data: Intent): Bitmap? {
val imageUri = data.data
return MediaStore.Images.Media.getBitmap(context.contentResolver, imageUri)
}
}
}