Skip to content

Commit 4caf283

Browse files
committed
fixed mc-scanner module
1 parent 3ea6607 commit 4caf283

File tree

5 files changed

+64
-83
lines changed

5 files changed

+64
-83
lines changed

classic-components-example/mc-scanner/src/main/java/io/scanbot/example/ExampleApplication.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.scanbot.example
22

33
import android.app.Application
4-
import io.scanbot.sap.SdkFeature
54
import io.scanbot.sdk.ScanbotSDK
65
import io.scanbot.sdk.ScanbotSDKInitializer
76
import io.scanbot.sdk.util.log.LoggerProvider
@@ -24,11 +23,9 @@ class ExampleApplication : Application() {
2423
.withLogging(true)
2524
// TODO 2/2: Enable the Scanbot SDK license key
2625
//.license(this, licenseKey)
27-
.licenceErrorHandler { status, feature, statusMessage ->
26+
.licenseErrorHandler { status, feature, statusMessage ->
2827
LoggerProvider.logger.d("ExampleApplication", "+++> License status: ${status.name}. Status message: $statusMessage")
29-
if (feature != SdkFeature.NoSdkFeature) {
30-
LoggerProvider.logger.d("ExampleApplication", "+++> Feature not available: ${feature.name}")
31-
}
28+
LoggerProvider.logger.d("ExampleApplication", "+++> Feature not available: ${feature.name}")
3229
}
3330
//.sdkFilesDirectory(this, getExternalFilesDir(null)!!)
3431
.prepareOCRLanguagesBlobs(true)
@@ -39,6 +36,6 @@ class ExampleApplication : Application() {
3936
val licenseInfo = ScanbotSDK(this).licenseInfo
4037
LoggerProvider.logger.d("ExampleApplication", "License status: ${licenseInfo.status}")
4138
LoggerProvider.logger.d("ExampleApplication", "License isValid: ${licenseInfo.isValid}")
42-
LoggerProvider.logger.d("ExampleApplication", "License expirationDate: ${licenseInfo.expirationDate}")
39+
LoggerProvider.logger.d("ExampleApplication", "License expirationDate: ${licenseInfo.expirationDateString}")
4340
}
4441
}

classic-components-example/mc-scanner/src/main/java/io/scanbot/example/MainActivity.kt

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.scanbot.example
22

3-
import android.graphics.BitmapFactory
43
import android.net.Uri
54
import android.os.Bundle
65
import android.util.Log
@@ -9,12 +8,15 @@ import androidx.activity.result.contract.ActivityResultContracts
98
import androidx.appcompat.app.AppCompatActivity
109
import androidx.core.view.isVisible
1110
import androidx.lifecycle.lifecycleScope
11+
import io.scanbot.common.getOrNull
12+
import io.scanbot.common.getOrThrow
1213
import io.scanbot.example.common.Const
1314
import io.scanbot.example.common.applyEdgeToEdge
1415
import io.scanbot.example.common.showToast
1516
import io.scanbot.example.databinding.ActivityMainBinding
1617
import io.scanbot.sdk.ScanbotSDK
17-
import io.scanbot.sdk.mc.MedicalCertificateScanningParameters
18+
import io.scanbot.sdk.image.ImageRef
19+
import io.scanbot.sdk.medicalcertificate.MedicalCertificateScanningParameters
1820
import kotlinx.coroutines.Dispatchers
1921
import kotlinx.coroutines.launch
2022
import kotlinx.coroutines.withContext
@@ -25,34 +27,39 @@ class MainActivity : AppCompatActivity() {
2527

2628
private val scanbotSdk: ScanbotSDK by lazy { ScanbotSDK(this) }
2729

28-
private val selectGalleryImageResultLauncher = registerForActivityResult(ActivityResultContracts.PickVisualMedia()) { uri ->
29-
if (!scanbotSdk.licenseInfo.isValid) {
30-
this@MainActivity.showToast("1-minute trial license has expired!")
31-
Log.e(Const.LOG_TAG, "1-minute trial license has expired!")
32-
return@registerForActivityResult
30+
private val selectGalleryImageResultLauncher =
31+
registerForActivityResult(ActivityResultContracts.PickVisualMedia()) { uri ->
32+
if (!scanbotSdk.licenseInfo.isValid) {
33+
this@MainActivity.showToast("1-minute trial license has expired!")
34+
Log.e(Const.LOG_TAG, "1-minute trial license has expired!")
35+
return@registerForActivityResult
36+
}
37+
38+
if (uri == null) {
39+
showToast("Error obtaining selected image!")
40+
Log.e(Const.LOG_TAG, "Error obtaining selected image!")
41+
return@registerForActivityResult
42+
}
43+
44+
lifecycleScope.launch { processImportedImage(uri) }
3345
}
3446

35-
if (uri == null) {
36-
showToast("Error obtaining selected image!")
37-
Log.e(Const.LOG_TAG, "Error obtaining selected image!")
38-
return@registerForActivityResult
39-
}
40-
41-
lifecycleScope.launch { processImportedImage(uri) }
42-
}
43-
4447
private suspend fun processImportedImage(uri: Uri) {
4548
withContext(Dispatchers.Main) {
4649
binding.progressBar.isVisible = true
4750
}
4851

4952
val result = withContext(Dispatchers.Default) {
5053
val inputStream = contentResolver.openInputStream(uri)
51-
val bitmap = BitmapFactory.decodeStream(inputStream)
54+
?: throw IllegalStateException("Cannot open input stream from URI: $uri")
55+
val imageRef = ImageRef.fromInputStream(inputStream)
5256

53-
val scanner = scanbotSdk.createMedicalCertificateScanner()
57+
val scanner = scanbotSdk.createMedicalCertificateScanner().getOrThrow()
5458

55-
scanner.scanFromBitmap(bitmap, 0, MedicalCertificateScanningParameters(true, true, true))
59+
scanner.run(
60+
imageRef,
61+
MedicalCertificateScanningParameters(true, true, true)
62+
).getOrNull()
5663
}
5764

5865
withContext(Dispatchers.Main) {
@@ -70,7 +77,13 @@ class MainActivity : AppCompatActivity() {
7077
supportActionBar?.hide()
7178
applyEdgeToEdge(findViewById(R.id.root_view))
7279

73-
binding.scannerBtn.setOnClickListener { startActivity(MedicalCertificateScannerActivity.newIntent(this)) }
80+
binding.scannerBtn.setOnClickListener {
81+
startActivity(
82+
MedicalCertificateScannerActivity.newIntent(
83+
this
84+
)
85+
)
86+
}
7487

7588
binding.manualScannerBtn.setOnClickListener {
7689
startActivity(ManualMedicalCertificateScannerActivity.newIntent(this))

classic-components-example/mc-scanner/src/main/java/io/scanbot/example/ManualMedicalCertificateScannerActivity.kt

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@ import androidx.appcompat.app.AppCompatActivity
1616
import androidx.core.app.ActivityCompat
1717
import androidx.core.content.ContextCompat
1818
import androidx.core.view.WindowCompat
19+
import io.scanbot.common.getOrNull
20+
import io.scanbot.common.getOrThrow
1921
import io.scanbot.example.MedicalCertificateResultActivity.Companion.newIntent
2022
import io.scanbot.example.common.applyEdgeToEdge
2123
import io.scanbot.sdk.ScanbotSDK
2224
import io.scanbot.sdk.camera.CameraPreviewMode
2325
import io.scanbot.sdk.camera.CaptureInfo
2426
import io.scanbot.sdk.camera.PictureCallback
25-
import io.scanbot.sdk.mc.MedicalCertificateScanner
26-
import io.scanbot.sdk.mc.MedicalCertificateScanningParameters
27+
import io.scanbot.sdk.image.ImageRef
28+
import io.scanbot.sdk.medicalcertificate.MedicalCertificateScanner
29+
import io.scanbot.sdk.medicalcertificate.MedicalCertificateScanningParameters
2730
import io.scanbot.sdk.ui.camera.ScanbotCameraXView
2831
import kotlin.math.roundToInt
2932

@@ -44,7 +47,7 @@ class ManualMedicalCertificateScannerActivity : AppCompatActivity() {
4447

4548
askPermission()
4649
val scanbotSDK = ScanbotSDK(this)
47-
scanner = scanbotSDK.createMedicalCertificateScanner()
50+
scanner = scanbotSDK.createMedicalCertificateScanner().getOrThrow()
4851

4952
cameraView = findViewById<View>(R.id.camera) as ScanbotCameraXView
5053
cameraView.setCameraOpenCallback {
@@ -54,7 +57,7 @@ class ManualMedicalCertificateScannerActivity : AppCompatActivity() {
5457
}, 700)
5558
}
5659
cameraView.addPictureCallback(object : PictureCallback() {
57-
override fun onPictureTaken(image: ByteArray, captureInfo: CaptureInfo) {
60+
override fun onPictureTaken(image: ImageRef, captureInfo: CaptureInfo) {
5861
processPictureTaken(image, captureInfo.imageOrientation)
5962
}
6063
})
@@ -81,48 +84,21 @@ class ManualMedicalCertificateScannerActivity : AppCompatActivity() {
8184
Toast.makeText(this, "Scanning Medical Certificate...", Toast.LENGTH_LONG)
8285
}
8386

84-
private fun processPictureTaken(image: ByteArray, imageOrientation: Int) {
85-
// Here we get the full image from the camera.
86-
// Implement a suitable async(!) scanning and image handling here.
87-
88-
// Decode Bitmap from bytes of original image:
89-
val options = BitmapFactory.Options()
90-
options.inSampleSize = 2 // use 1 for full, no downscaled image.
91-
var originalBitmap = BitmapFactory.decodeByteArray(image, 0, image.size, options)
92-
93-
// rotate original image if required:
94-
if (imageOrientation > 0) {
95-
val matrix = Matrix()
96-
matrix.setRotate(
97-
imageOrientation.toFloat(),
98-
originalBitmap.width / 2f,
99-
originalBitmap.height / 2f
100-
)
101-
originalBitmap = Bitmap.createBitmap(
102-
originalBitmap,
103-
0,
104-
0,
105-
originalBitmap.width,
106-
originalBitmap.height,
107-
matrix,
108-
false
109-
)
110-
}
87+
private fun processPictureTaken(image: ImageRef, imageOrientation: Int) {
11188

11289
// And finally run Medical Certificate scanning on prepared document image:
113-
val resultInfo = scanner.scanFromBitmap(
114-
originalBitmap,
115-
0,
90+
val resultInfo = scanner.run(
91+
image,
11692
MedicalCertificateScanningParameters(
11793
shouldCropDocument = true,
11894
extractCroppedImage = true,
11995
recognizePatientInfoBox = true,
12096
recognizeBarcode = true
12197
)
122-
)
98+
).getOrNull()
12399
if (resultInfo != null && resultInfo.scanningSuccessful) {
124100
// Show the cropped image as thumbnail preview
125-
resultInfo.croppedImage?.toBitmap()?.let { image ->
101+
resultInfo.croppedImage?.toBitmap()?.getOrNull()?.let { image ->
126102
val thumbnailImage = resizeImage(image, 600f, 600f)
127103
runOnUiThread {
128104
resultImageView.setImageBitmap(thumbnailImage)

classic-components-example/mc-scanner/src/main/java/io/scanbot/example/MedicalCertificateRecognizerActivity.kt

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,19 @@ import androidx.appcompat.app.AppCompatActivity
1515
import androidx.core.app.ActivityCompat
1616
import androidx.core.content.ContextCompat
1717
import androidx.core.view.WindowCompat
18+
import io.scanbot.common.getOrNull
19+
import io.scanbot.common.getOrThrow
1820
import io.scanbot.example.MedicalCertificateResultActivity.Companion.newIntent
1921
import io.scanbot.example.common.applyEdgeToEdge
2022
import io.scanbot.sdk.ScanbotSDK
2123
import io.scanbot.sdk.camera.CameraPreviewMode
2224
import io.scanbot.sdk.camera.CaptureInfo
2325
import io.scanbot.sdk.camera.PictureCallback
26+
import io.scanbot.sdk.image.ImageRef
2427
import io.scanbot.sdk.mc.MedicalCertificateAutoSnappingController
2528
import io.scanbot.sdk.mc.MedicalCertificateFrameHandler
26-
import io.scanbot.sdk.mc.MedicalCertificateScanner
27-
import io.scanbot.sdk.mc.MedicalCertificateScanningParameters
29+
import io.scanbot.sdk.medicalcertificate.MedicalCertificateScanner
30+
import io.scanbot.sdk.medicalcertificate.MedicalCertificateScanningParameters
2831
import io.scanbot.sdk.ui.camera.ScanbotCameraXView
2932
import kotlin.math.roundToInt
3033

@@ -54,14 +57,14 @@ class MedicalCertificateScannerActivity : AppCompatActivity() {
5457
}, 700)
5558
}
5659
cameraView.addPictureCallback(object : PictureCallback() {
57-
override fun onPictureTaken(image: ByteArray, captureInfo: CaptureInfo) {
60+
override fun onPictureTaken(image: ImageRef, captureInfo: CaptureInfo) {
5861
processPictureTaken(image)
5962
}
6063
})
6164
cameraView.setPreviewMode(CameraPreviewMode.FIT_IN)
6265

6366
val scanbotSDK = ScanbotSDK(this)
64-
scanner = scanbotSDK.createMedicalCertificateScanner()
67+
scanner = scanbotSDK.createMedicalCertificateScanner().getOrThrow()
6568

6669
// Attach `FrameHandler`, that will be scanning for Medical Certificate document on the camera frames
6770
val frameHandler =
@@ -89,30 +92,22 @@ class MedicalCertificateScannerActivity : AppCompatActivity() {
8992
Toast.makeText(this, "Scanning Medical Certificate...", Toast.LENGTH_LONG)
9093
}
9194

92-
private fun processPictureTaken(image: ByteArray) {
93-
// Here we get the full image from the camera.
94-
// Implement a suitable async(!) scanning and image handling here.
95-
96-
// Decode Bitmap from bytes of original image:
97-
val options = BitmapFactory.Options()
98-
options.inSampleSize = 2 // use 1 for full, no downscaled image.
99-
var originalBitmap = BitmapFactory.decodeByteArray(image, 0, image.size, options)
95+
private fun processPictureTaken(image: ImageRef) {
10096

10197
// And finally run Medical Certificate scanning on prepared document image:
102-
val resultInfo = scanner.scanFromBitmap(
103-
originalBitmap,
104-
0,
98+
val resultInfo = scanner.run(
99+
image,
105100
MedicalCertificateScanningParameters(
106101
shouldCropDocument = true,
107102
extractCroppedImage = true,
108103
recognizePatientInfoBox = true,
109104
recognizeBarcode = true
110105
)
111-
)
106+
).getOrNull()
112107

113108
if (resultInfo != null && resultInfo.scanningSuccessful) {
114109
// Show the cropped image as thumbnail preview
115-
resultInfo.croppedImage?.toBitmap()?.let { image ->
110+
resultInfo.croppedImage?.toBitmap()?.getOrNull()?.let { image ->
116111
val thumbnailImage = resizeImage(image, 600f, 600f)
117112
runOnUiThread {
118113
resultImageView.setImageBitmap(thumbnailImage)

classic-components-example/mc-scanner/src/main/java/io/scanbot/example/MedicalCertificateResultActivity.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import android.widget.LinearLayout
99
import android.widget.TextView
1010
import androidx.appcompat.app.AppCompatActivity
1111
import io.scanbot.example.common.applyEdgeToEdge
12-
import io.scanbot.sdk.mc.MedicalCertificateCheckBoxType
13-
import io.scanbot.sdk.mc.MedicalCertificateDateRecordType
14-
import io.scanbot.sdk.mc.MedicalCertificatePatientInfoField
15-
import io.scanbot.sdk.mc.MedicalCertificateScanningResult
12+
import io.scanbot.sdk.medicalcertificate.MedicalCertificateCheckBoxType
13+
import io.scanbot.sdk.medicalcertificate.MedicalCertificateDateRecordType
14+
import io.scanbot.sdk.medicalcertificate.MedicalCertificatePatientInfoField
15+
import io.scanbot.sdk.medicalcertificate.MedicalCertificateScanningResult
1616

1717
class MedicalCertificateResultActivity : AppCompatActivity() {
1818
override fun onCreate(savedInstanceState: Bundle?) {

0 commit comments

Comments
 (0)