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,256 @@
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 ->
userGuidanceHint.post {
showUserGuidance(data.status)
}
}
false // typically you need to return false
},
object : IDocumentScannerViewCallback {
override fun onCameraOpen() {
// In this example we demonstrate how to lock the orientation of the UI (Activity)
// as well as the orientation of the taken picture to portrait.
documentScannerView.cameraConfiguration.setCameraOrientationMode(
CameraOrientationMode.PORTRAIT
)

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)
}
}
)

// See https://docs.scanbot.io/document-scanner-sdk/android/features/document-scanner/using-scanbot-camera-view/#preview-mode
// cameraConfiguration.setCameraPreviewMode(io.scanbot.sdk.camera.CameraPreviewMode.FIT_IN)
}
}



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)
}

userGuidanceHint = findViewById(R.id.userGuidanceHint)

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 showUserGuidance(result: DocumentDetectionStatus) {
if (!autoSnappingEnabled) {
return
}
if (System.currentTimeMillis() - lastUserGuidanceHintTs < 400) {
return
}

when (result) {
DocumentDetectionStatus.OK -> {
userGuidanceHint.text = "Don't move"
userGuidanceHint.visibility = View.VISIBLE
}

DocumentDetectionStatus.OK_BUT_TOO_SMALL -> {
userGuidanceHint.text = "Move closer"
userGuidanceHint.visibility = View.VISIBLE
}

DocumentDetectionStatus.OK_BUT_BAD_ANGLES -> {
userGuidanceHint.text = "Perspective"
userGuidanceHint.visibility = View.VISIBLE
}

DocumentDetectionStatus.ERROR_NOTHING_DETECTED -> {
userGuidanceHint.text = "No Document"
userGuidanceHint.visibility = View.VISIBLE
}

DocumentDetectionStatus.ERROR_TOO_NOISY -> {
userGuidanceHint.text = "Background too noisy"
userGuidanceHint.visibility = View.VISIBLE
}

DocumentDetectionStatus.OK_BUT_BAD_ASPECT_RATIO -> {
if (ignoreOrientationMistmatch) {
userGuidanceHint.text = "Don't move"
} else {
userGuidanceHint.text = "Wrong aspect ratio.\nRotate your device."
}
userGuidanceHint.visibility = View.VISIBLE
}

DocumentDetectionStatus.ERROR_TOO_DARK -> {
userGuidanceHint.text = "Poor light"
userGuidanceHint.visibility = View.VISIBLE
}

else -> userGuidanceHint.visibility = View.GONE
}
lastUserGuidanceHintTs = System.currentTimeMillis()
}

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")
}
}
Loading