Skip to content

Commit 5072f29

Browse files
committed
fix image-ref usage logic
fix imageRef usage logic update dependencies fix compiling
1 parent f4aef69 commit 5072f29

File tree

14 files changed

+161
-26
lines changed

14 files changed

+161
-26
lines changed

classic-components-example/camera-view-aspect-ratio-finder/src/main/java/io/scanbot/example/MainActivity.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ import io.scanbot.sdk.documentscanner.DocumentScanner
2626
import io.scanbot.sdk.documentscanner.DocumentScannerParameters
2727
import io.scanbot.sdk.geometry.AspectRatio
2828
import io.scanbot.sdk.image.ImageRef
29+
import io.scanbot.sdk.imageprocessing.ScanbotSdkImageProcessor
2930
import io.scanbot.sdk.process.ImageProcessor
3031
import io.scanbot.sdk.ui.camera.AdaptiveFinderOverlayView
3132
import io.scanbot.sdk.ui.camera.ScanbotCameraXView
3233
import io.scanbot.sdk.ui.camera.ShutterButton
34+
import io.scanbot.sdk.util.PolygonHelper
3335

3436
class MainActivity : AppCompatActivity(), DocumentScannerFrameHandler.ResultHandler {
3537
private lateinit var cameraView: ScanbotCameraXView
@@ -186,11 +188,18 @@ class MainActivity : AppCompatActivity(), DocumentScannerFrameHandler.ResultHand
186188
this.ignoreOrientationMismatch = true
187189
}
188190
})
189-
val polygon = scanner.run(image).getOrNull()?.pointsNormalized ?: throw Exception("Document detection failed")
191+
val polygon = scanner.run(image).getOrNull()?.pointsNormalized
192+
?: throw Exception("Document detection failed")
193+
194+
val polygonCrop =
195+
polygon.takeIf { it.isNotEmpty() && it.size == 4 } ?: PolygonHelper.getFullPolygon()
196+
var documentImage = ScanbotSdkImageProcessor.create()
197+
.crop(image, polygonCrop)
198+
.getOrThrow()
199+
documentImage = ScanbotSdkImageProcessor.create().resize(documentImage, 200).getOrThrow()
190200

191-
val documentImage = ImageProcessor(image).crop(polygon).processedBitmap().getOrNull()
192201
resultView.post {
193-
resultView.setImageBitmap(documentImage)
202+
resultView.setImageBitmap(documentImage.toBitmap().getOrNull())
194203
cameraView.continuousFocus()
195204
cameraView.startPreview()
196205
}

classic-components-example/camera-view/src/main/java/io/scanbot/example/MainActivity.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ import io.scanbot.sdk.document.DocumentScannerFrameHandler
3333
import io.scanbot.sdk.documentscanner.DocumentDetectionStatus
3434
import io.scanbot.sdk.documentscanner.DocumentScanner
3535
import io.scanbot.sdk.image.ImageRef
36+
import io.scanbot.sdk.imageprocessing.ScanbotSdkImageProcessor
3637
import io.scanbot.sdk.process.ImageProcessor
3738
import io.scanbot.sdk.ui.PolygonView
3839
import io.scanbot.sdk.ui.camera.ScanbotCameraXView
3940
import io.scanbot.sdk.ui.camera.ShutterButton
41+
import io.scanbot.sdk.util.PolygonHelper
4042

4143
class MainActivity : AppCompatActivity(), DocumentScannerFrameHandler.ResultHandler {
4244
private lateinit var cameraView: ScanbotCameraXView
@@ -223,16 +225,14 @@ class MainActivity : AppCompatActivity(), DocumentScannerFrameHandler.ResultHand
223225
documentScanner.run(image).getOrNull()?.pointsNormalized ?: throw IllegalStateException(
224226
"No document detected"
225227
)
228+
val polygonCrop = polygon.takeIf { it.isNotEmpty() && it.size == 4 } ?: PolygonHelper.getFullPolygon()
229+
var documentImage = ScanbotSdkImageProcessor.create()
230+
.crop(image, polygonCrop)
231+
.getOrThrow()
232+
documentImage = ScanbotSdkImageProcessor.create().resize(documentImage, 200).getOrThrow()
226233

227-
val documentImage =
228-
ImageProcessor(image).resize(200).crop(polygon).processedBitmap().getOrNull()
229234
resultView.post {
230-
val bm = image.toBitmap()
231-
when(bm){
232-
is Result.Success<*> -> {}
233-
is Result.Unexpected ->{ throw IllegalStateException(bm.message) }
234-
}
235-
resultView.setImageBitmap(bm.getOrNull())
235+
resultView.setImageBitmap(documentImage.toBitmap().getOrNull())
236236
}
237237

238238
// continue scanning

data-capture-ready-to-use-ui-example/app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ dependencies {
7474
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version")
7575
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version")
7676

77-
def scanbotSdkVersion = "8.0.0.38-STAGING-SNAPSHOT"
77+
def scanbotSdkVersion = "8.0.0_RESULT.54-STAGING-SNAPSHOT"
7878

7979
implementation("io.scanbot:sdk-package-4:$scanbotSdkVersion")
8080
implementation("io.scanbot:sdk-package-ui:$scanbotSdkVersion")

data-capture-ready-to-use-ui-example/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
plugins {
3-
id "com.android.application" version "8.3.2" apply false
4-
id "org.jetbrains.kotlin.android" version "1.9.23" apply false
3+
id "com.android.application" version '8.13.0' apply false
4+
id "org.jetbrains.kotlin.android" version "2.2.20" apply false
55
}
66

77
allprojects {

data-capture-ready-to-use-ui-example/gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.13-bin.zip

document-scanner-ready-to-use-ui-example/app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ android {
3939
}
4040
}
4141

42-
def scanbotSdkVersion = "8.0.0.39-STAGING-SNAPSHOT"
42+
def scanbotSdkVersion = "8.0.0_RESULT.54-STAGING-SNAPSHOT"
4343

4444
dependencies {
4545

document-scanner-ready-to-use-ui-example/app/src/main/java/com/example/scanbot/di/ExampleSingleton.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import io.scanbot.sdk.docprocessing.PdfPagesExtractor
77
import io.scanbot.sdk.documentqualityanalyzer.DocumentQualityAnalyzer
88
import io.scanbot.sdk.ocr.OcrEngine
99
import io.scanbot.sdk.ocr.OcrEngineManager
10+
import io.scanbot.sdk.pdf.PdfGenerator
1011
import io.scanbot.sdk.persistence.fileio.FileIOProcessor
1112
import io.scanbot.sdk.persistence.page.PageFileStorage
12-
import io.scanbot.sdk.process.PdfGenerator
1313
import io.scanbot.sdk.tiff.TiffGeneratorManager
1414
import io.scanbot.sdk.tiffgeneration.TiffGenerator
1515

document-scanner-ready-to-use-ui-example/app/src/main/java/com/example/scanbot/doc_code_snippet/PdfFromDocumentSnippet.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class PdfFromDocumentSnippet : AppCompatActivity() {
8383
pageFit = PageFit.NONE,
8484
resamplingMethod = ResamplingMethod.NONE,
8585
)
86-
val result = pdfGenerator.generateFromDocument(
86+
val result = pdfGenerator.generate(
8787
document,
8888
config
8989
)

document-scanner-ready-to-use-ui-example/app/src/main/java/com/example/scanbot/doc_code_snippet/PdfFromImageSnippet.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ class PdfFromImageSnippet : AppCompatActivity() {
5656
// notify the generator that the images are encrypted with global sdk-encryption settings
5757
val encryptionEnabled = false
5858
// Join the images into a PDF file.
59-
val pdfFile = pdfGenerator.generateFromUris(
60-
imageFileUris = list.toTypedArray(),
59+
val pdfFile = pdfGenerator.generate(
60+
imageFileUris = list,
6161
sourceFilesEncrypted = encryptionEnabled,
6262
config
6363
).getOrNull()
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.example.scanbot.doc_code_snippet
2+
3+
import io.scanbot.common.Result
4+
import io.scanbot.common.getOrDefault
5+
import io.scanbot.common.getOrNull
6+
import io.scanbot.common.getOrThrow
7+
import io.scanbot.common.mapFailure
8+
import io.scanbot.common.mapSuccess
9+
import io.scanbot.common.onFailure
10+
import io.scanbot.common.onSuccess
11+
import io.scanbot.sdk.barcode.BarcodeFormat
12+
import io.scanbot.sdk.barcode.BarcodeScannerResult
13+
import io.scanbot.sdk.barcode.ScanbotSdkBarcodeScanner
14+
import io.scanbot.sdk.documentscanner.ScanbotSdkDocumentScanner
15+
import io.scanbot.sdk.image.ImageRef
16+
17+
class ResultApi {
18+
// @Tag("Handle Result with when/switch")
19+
fun processResultWithSwitch(result: Result<BarcodeScannerResult>) = when (result) {
20+
is Result.Success<*> -> {
21+
val barcodeResult = result.data
22+
// Handle successful result
23+
}
24+
25+
is Result.ComponentUnavailableError,
26+
is Result.IllegalStateError,
27+
is Result.InvalidArgumentError,
28+
is Result.InvalidDataError,
29+
is Result.InvalidImageRefError,
30+
is Result.InvalidLicenseError,
31+
is Result.IoError,
32+
is Result.NullPointerError,
33+
is Result.OperationCanceledError,
34+
is Result.OutOfMemoryError,
35+
is Result.TimeoutError,
36+
is Result.UnknownError -> {
37+
// all Result.[ERROR_NAME] are also of type `Result.Unexpected` and can be handled together as throwable errors or logged etc.
38+
val exception = result.message
39+
// Handle error
40+
println("Error scanning barcodes: ${exception}")
41+
}
42+
}
43+
// @EndTag("Handle Result with when/switch")
44+
45+
// @Tag("Handle Result with getter functions")
46+
fun proceedWithGetters(result: Result<BarcodeScannerResult>) {
47+
// get value or null if unsuccessful
48+
val nullableResult = result.getOrNull()
49+
// get value or throw exception if unsuccessful then handle exception with try-catch
50+
val nonNullableResult = result.getOrThrow()
51+
// get value or put default value if unsuccessful
52+
val resultOrDefaultValue = result.getOrDefault(
53+
BarcodeScannerResult(
54+
emptyList(),
55+
false
56+
)
57+
) // get value or throw exception if unsuccessful then handle exception with try-catch
58+
}
59+
// @EndTag("Handle Result with getter functions")
60+
61+
// @Tag("Handle Result with chain API")
62+
fun processResultWithChainApi(result: Result<BarcodeScannerResult>) {
63+
// handle success and failure in a chained way without nesting and result type checking
64+
result.onSuccess { barcodeResult ->
65+
// Handle successful barcode scanning result
66+
val barcodes = barcodeResult.barcodes
67+
// Process barcodes as needed
68+
}.onFailure { exception ->
69+
// Handle error during barcode scanning
70+
println("Error scanning barcodes: ${exception.message}")
71+
}
72+
73+
// chained transformations
74+
val barcodeSize = result.mapSuccess { barcodeResult ->
75+
// Transform the successful result to another successful result
76+
barcodeResult.barcodes.size // or Return Result.Unexpected in case of error
77+
//Result.IllegalStateError("Transformation error") // example of returning an error during transformation
78+
}.mapFailure { exception ->
79+
// Transform the error to a default value
80+
0
81+
}.getOrNull()
82+
}
83+
// @EndTag("Handle Result with chain API")
84+
85+
fun chainWithScannerCall(
86+
image: ImageRef,
87+
barcoderScanner: ScanbotSdkBarcodeScanner,
88+
documentScanner: ScanbotSdkDocumentScanner
89+
) {
90+
// @Tag("Chain Result with SDK calls")
91+
val polygon = barcoderScanner.run(image)
92+
.mapSuccess { barcodeScanResult ->
93+
// Transform barcode scan result to document scan result if the document contains barcode with specified type
94+
if (barcodeScanResult.barcodes.none { item -> item.format == BarcodeFormat.QR_CODE }) {
95+
// return from mapSuccess with Result.IllegalStateError
96+
throw Result.IllegalStateError("Document should contain unique qr code to be accepted")
97+
}
98+
// if result of document scanning is failed due to issues it will trigger mapSuccess function to return Unexpected Result type
99+
val documentScanResult = documentScanner.run(image).getOrReturn()
100+
// return document polygon if scanning result if successful
101+
documentScanResult.pointsNormalized
102+
}.getOrDefault(emptyList())
103+
// @EndTag("Chain Result with SDK calls")
104+
}
105+
}

0 commit comments

Comments
 (0)