Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion classic-components-example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ allprojects {

jvmToolchainVersion = 17

scanbotSdkVersion = "8.1.0"
scanbotSdkVersion = "9.0.0.99-STAGING-SNAPSHOT"

androidCoreKtxVersion = "1.6.0"
constraintLayoutVersion = "2.0.4"
Expand Down
47 changes: 47 additions & 0 deletions classic-components-example/document-enhancer/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
}

android {
namespace = project.ext.submodulesNamespace
compileSdk = project.ext.compileSdkVersion

defaultConfig {
applicationId = project.ext.exampleAppId
minSdk = project.ext.minSdkVersion
targetSdk = project.ext.targetSdkVersion
versionCode = 1
versionName = "1.0"
}

buildTypes {
named("debug") {
// set this to `false` to allow debugging and run a "non-release" build
minifyEnabled = false
debuggable = true
}
}

kotlin {
jvmToolchain(project.ext.jvmToolchainVersion)
}

packagingOptions {
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/DEPENDENCIES'
}

buildFeatures {
viewBinding = true
}
}

dependencies {
implementation(project(":common"))
implementation("io.scanbot:sdk-package-1:${project.ext.scanbotSdkVersion}")
implementation("androidx.appcompat:appcompat:${project.ext.androidxAppcompatVersion}")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
>

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />

<uses-feature android:name="android.hardware.camera" />

<application
android:name=".ExampleApplication"
android:allowBackup="true"
android:label="@string/app_name"
android:theme="@style/AppTheme"
tools:replace="android:theme">
<activity android:name=".DocumentCameraActivity" />
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package io.scanbot.example

import android.Manifest
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Color
import android.graphics.Matrix
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.core.view.WindowCompat
import io.scanbot.common.onSuccess


import io.scanbot.example.common.applyEdgeToEdge
import io.scanbot.sdk.ScanbotSDK
import io.scanbot.sdk.camera.CaptureInfo
import io.scanbot.sdk.document.DocumentScannerFrameHandler
import io.scanbot.sdk.document.ui.DocumentScannerView
import io.scanbot.sdk.document.ui.IDocumentScannerViewCallback
import io.scanbot.sdk.documentscanner.DocumentDetectionStatus
import io.scanbot.sdk.documentscanner.DocumentEnhancer
import io.scanbot.sdk.documentscanner.DocumentScanner
import io.scanbot.sdk.documentscanner.DocumentStraighteningMode
import io.scanbot.sdk.documentscanner.DocumentStraighteningParameters
import io.scanbot.sdk.geometry.AspectRatio
import io.scanbot.sdk.image.ImageRef
import io.scanbot.sdk.process.ImageProcessor
import io.scanbot.sdk.ui.camera.ShutterButton
import io.scanbot.sdk.ui.view.base.configuration.CameraOrientationMode

class DocumentCameraActivity : AppCompatActivity() {

private var lastUserGuidanceHintTs = 0L
private var flashEnabled = false
private var autoSnappingEnabled = true
private val ignoreOrientationMistmatch = true

private lateinit var documentScannerView: DocumentScannerView

private lateinit var resultView: ImageView
private lateinit var userGuidanceHint: TextView
private lateinit var autoSnappingToggleButton: Button
private lateinit var shutterButton: ShutterButton


override fun onCreate(savedInstanceState: Bundle?) {
supportRequestWindowFeature(WindowCompat.FEATURE_ACTION_BAR_OVERLAY)

super.onCreate(savedInstanceState)
setContentView(R.layout.activity_camera)
askPermission()
supportActionBar!!.hide()
applyEdgeToEdge(findViewById(R.id.root_view))

val scanbotSdk = ScanbotSDK(this)

documentScannerView = findViewById(R.id.document_scanner_view)

resultView = findViewById<View>(R.id.result) as ImageView
val documentEnhancer = scanbotSdk.createDocumentEnhancer()
scanbotSdk.createDocumentScanner().onSuccess { documentScanner ->

documentScannerView.apply {
initCamera()
initScanningBehavior(
documentScanner,
{ result, frame ->
// Here you are continuously notified about document scanning results.
// For example, you can show a user guidance text depending on the current scanning status.
result.onSuccess { data ->
}
false // typically you need to return false
},
object : IDocumentScannerViewCallback {
override fun onCameraOpen() {
documentScannerView.viewController.useFlash(flashEnabled)
}

override fun onPictureTaken(image: ImageRef, captureInfo: CaptureInfo) {
documentEnhancer.onSuccess { documentEnhancer ->
processPictureTaken(image, documentEnhancer)
}


// continue scanning
documentScannerView.postDelayed({
documentScannerView.viewController.startPreview()
}, 1000)
}
}
)
}
}

documentScannerView.polygonConfiguration.apply {
setPolygonFillColor(POLYGON_FILL_COLOR)
setPolygonFillColorOK(POLYGON_FILL_COLOR_OK)
}

documentScannerView.viewController.apply {
setAcceptedAngleScore(60.0)
setAcceptedSizeScore(75.0)
setIgnoreOrientationMismatch(ignoreOrientationMistmatch)

// Please note: https://docs.scanbot.io/document-scanner-sdk/android/features/document-scanner/autosnapping/#sensitivity
setAutoSnappingSensitivity(0.85f)
}

shutterButton = findViewById(R.id.shutterButton)
shutterButton.setOnClickListener { documentScannerView.viewController.takePicture(false) }
shutterButton.visibility = View.VISIBLE

findViewById<View>(R.id.flashToggle).setOnClickListener {
flashEnabled = !flashEnabled
documentScannerView.viewController.useFlash(flashEnabled)
}

autoSnappingToggleButton = findViewById(R.id.autoSnappingToggle)
autoSnappingToggleButton.setOnClickListener {
autoSnappingEnabled = !autoSnappingEnabled
setAutoSnapEnabled(autoSnappingEnabled)
}
autoSnappingToggleButton.post { setAutoSnapEnabled(autoSnappingEnabled) }
}

private fun askPermission() {
if (ContextCompat.checkSelfPermission(
this,
Manifest.permission.CAMERA
) != PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CAMERA), 999)
}
}

override fun onResume() {
super.onResume()
documentScannerView.viewController.onResume()
}

override fun onPause() {
super.onPause()
documentScannerView.viewController.onPause()
}

private fun processPictureTaken(image: ImageRef, documentEnhancer: DocumentEnhancer) {
// STRAIGHTEN SCANNED IMAGE ASSUMING DOCUMENT IS BENT
// Run document enhancer unwarping on original image:
val result = documentEnhancer.straighten(image, DocumentStraighteningParameters().apply {
straighteningMode = DocumentStraighteningMode.STRAIGHTEN
// uncomment if you want wo set specific aspect ratios for documents
// aspectRatios = listOf(AspectRatio(29.0, 21.0))
}).getOrNull()

resultView.post {
resultView.setImageBitmap(
result?.straightenedImage?.toBitmap()?.getOrNull()
)
}
}

private fun setAutoSnapEnabled(enabled: Boolean) {
documentScannerView.viewController.apply {
autoSnappingEnabled = enabled
isFrameProcessingEnabled = enabled
}
documentScannerView.polygonConfiguration.setPolygonViewVisible(enabled)

autoSnappingToggleButton.text = "Automatic ${if (enabled) "ON" else "OFF"}"
if (enabled) {
shutterButton.showAutoButton()
} else {
shutterButton.showManualButton()
userGuidanceHint.visibility = View.GONE
}
}

companion object {
private val POLYGON_FILL_COLOR = Color.parseColor("#55ff0000")
private val POLYGON_FILL_COLOR_OK = Color.parseColor("#4400ff00")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package io.scanbot.example

import android.app.Application
import io.scanbot.sdk.ScanbotSDK
import io.scanbot.sdk.ScanbotSDKInitializer
import io.scanbot.sdk.util.log.LoggerProvider
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlin.coroutines.CoroutineContext

class ExampleApplication : Application(), CoroutineScope {

private var job: Job = Job()
override val coroutineContext: CoroutineContext
get() = Dispatchers.IO + job

/*
* TODO 1/2: Add the Scanbot SDK license key here.
* Please note: The Scanbot SDK will run without a license key for one minute per session!
* After the trial period is over all Scanbot SDK functions as well as the UI components will stop working.
* You can get an unrestricted "no-strings-attached" 30 day trial license key for free.
* Please submit the trial license form (https://scanbot.io/trial/) on our website by using
* the app identifier "io.scanbot.example.sdk.android" of this example app.
*/
val licenseKey = ""

override fun onCreate() {
super.onCreate()

ScanbotSDKInitializer()
.withLogging(true)
// TODO 2/2: Enable the Scanbot SDK license key
//.license(this, licenseKey)
.licenseErrorHandler { status, feature, statusMessage ->
LoggerProvider.logger.d("ExampleApplication", "+++> License status: ${status.name}. Status message: $statusMessage")
LoggerProvider.logger.d("ExampleApplication", "+++> Feature not available: ${feature.name}")
}
//.sdkFilesDirectory(this, getExternalFilesDir(null)!!)
.initialize(this)

LoggerProvider.logger.d("ExampleApplication", "Scanbot SDK was initialized")

val licenseInfo = ScanbotSDK(this).licenseInfo
LoggerProvider.logger.d("ExampleApplication", "License status: ${licenseInfo.status}")
LoggerProvider.logger.d("ExampleApplication", "License isValid: ${licenseInfo.isValid}")
LoggerProvider.logger.d("ExampleApplication", "License expirationDate: ${licenseInfo.expirationDateString}")

launch {
// Clear all previously created documents in storage
ScanbotSDK(this@ExampleApplication).documentApi.deleteAllDocuments()
}
}
}
Loading