Skip to main content

Document detection | Android Document Scanner

ContourDetector is used for performing document detection.

Document Detection#

The Document Detection of Scanbot SDK is based on edge detection. To detect the edges and get the boundaries of a document sheet on a still image use the detect(image) method of ContourDetector:

val detector = ScanbotSDK(context).createContourDetector()val result = detector.detect(origininalBitmap)  // pass a Bitmap or the image data as byte[]val polygon = detector.getPolygonF()

Here we are detecting the boundaries and receiving a DetectionResult . It is an enum which represents the recognition status.

Regardless of the DetectionResult, you can acquire the detected boundaries as a polygon using the detector.getPolygonF() method. If the polygon was not detected, it will return an empty List. On success you will get a List with 4 points (one for each corner - top_left, top_right, bottom_right, bottom_left). Each point has coordinates in a range between [0..1], representing a position relative to the image size. For instance, if a point has the coordinates (0.5, 0.5), it means that it is located exactly in the center of the image.

Types of Document Detection algorithms#

User can select which technology will be used for the document detection. There are two options: Edge-based and ML-based. The Edge based technology detects all the edges on the picture and then predicts which of those edges may form the document. The ML-based approach finds a document using computer-vision technologies. The ML-based approach might be much faster for Live-detection than the Edge-based approach.

Since Scanbot SDK 1.81.0 it is included in io.scanbot:sdk-package-X modules by default. If the application does not require the document detection feature at all or an edge-based approach is preferred, it is possible to reduce the .apk size (~4MB) by excluding this Gradle dependency as follows.

implementation("io.scanbot:sdk-package-Y:$scanbotSdkVersion") { // REPLACE 'Y' with your SDK-package number!!!    exclude group: "io.scanbot", module: "sdk-ml-docdetector"}

To select the type of Document Detection technology, use the following snippet during the SDK-initialization. The ML-based approach will be used by default.

import io.scanbot.sdk.ScanbotSDKInitializer
class ExampleApplication : Application() {
    override fun onCreate() {        super.onCreate()
        // Initialize the Scanbot Scanner SDK:        ScanbotSDKInitializer()             .contourDetectorType(ContourDetector.Type.ML_BASED) // ML_BASED is default. Set it to EDGE_BASED to use the edge-based approach             .initialize(this)    }}

Then, to get an instance of a ContourDetector, use the ScanbotSDK object. It will create a NEW contour detector for each call.

import io.scanbot.sdk.ScanbotSDKInitializer        ...    val detector = ScanbotSDK(context).createContourDetector()

Warning! ContourDetector() constructor was marked as deprecated. It will create an instance of the document detector with Edge-based technology under the hood and ignore the setting passed to ScanbotSDKInitializer