Skip to main content

Xamarin Native | Xamarin Document Scanner

Integration with Xamarin.Android and Xamarin.iOS

The NuGet package ScanbotSDK.Xamarin is a universal package for Android and iOS. It contains the Xamarin Bindings for the Native Scanbot SDKs for Android and iOS and the Wrappers.

Namespaces

The bindings for Native SDKs and the Wrappers are stored in different namespaces which are explained here.

Xamarin Wrapper

  • Namespace for Android: ScanbotSDK.Xamarin.Android
  • Namespace for iOS: ScanbotSDK.Xamarin.iOS

The idea of the Scanbot SDK Xamarin Wrapper is to provide a unified and convenient API for iOS and Android. However, since not all Native SDK functionalities are available in the Wrapper namespace, you can use and call them as needed directly from the native namespaces.

Currently the following Package II functionalities are available for the Xamarin Wrapper classes:

  • Document detection on images
  • Image processing (cropping and perspective correction, rotating, etc)
  • Image filters
  • PDF creation
  • TIFF creation
  • Text Recognition (OCR)

Native SDKs

Native SDKs for Android and iOS are provided as Xamarin Bindings Libraries and can be found in these namespaces:

  • Android: Net.Doo.Snap, IO.Scanbot.Sdk
  • iOS: ScanbotSDK.iOS

The Xamarin Bindings for the Native SDKs provide the most available functionality of the Scanbot SDK.

👉 Documentation for Native SDKs classes can be found here:

Getting started

Initialize SDK

The Scanbot SDK must be initialized before usage. Make sure to run the initialization as early as possible. We recommend implementing the initialization in the Application class of the Android app, and in the AppDelegate class of the iOS app.

In the following examples we will use the convenient wrapper class SBSDK.

Add the following code to your Application class:

using ScanbotSDK.Xamarin.Android.Wrapper;

[Application(LargeHeap = true)]
public class MainApplication : Application
{
...
public override void OnCreate()
{
// You can pass null as licenseKey for trial mode. See the "License" section for more details.
SBSDK.Initialize(this, licenseKey, new SBSDKConfiguration { EnableLogging = true });
....
}
...
}

Furthermore, it is highly recommended to add the flag LargeHeap = true to the Application attribute on the MainApplication since image processing is very memory-intensive.

Storage Encryption

Scanbot SDK provides the ability to store the generated image files (JPG, PNG) and PDF files encrypted. This feature provides an additional level of security to the default secure storage locations of the native SDKs.

By default the file encryption is disabled. If you use ScanbotSDK wrapper, you have to pass the following config parameters on SDK initialization:

Common Wrapper:

new SBSDKConfiguration
{
Encryption = new SBSDKEncryption
{
Mode = EncryptionMode.AES256,
Password = "SomeSecretPa$$w0rdForFileEncryption"
}
};

By activating the storage encryption the native Scanbot SDKs will use the built-in AES 128 or AES 256 encryption. All generated image files (JPG, PNG) including the preview image files, as well as the exported PDF files will be encrypted in memory and stored as encrypted data files on the flash storage of the device.

The Scanbot SDK derives the AES key from the given password, an internal salt value, and the internal number of iterations using the PBKDF2 function.

When applying image operations like cropping, rotation or image filters, Scanbot SDK will decrypt the image file in memory, apply the changes, encrypt and store it again.

Scanbot SDK UI Components

RTU UI Components

The Ready-To-Use UI (RTU UI) is a set of easy to integrate and customize high-level UI components (View Controllers for iOS and Activities for Android) for the most common tasks in Scanbot SDK. The design and behavior of these RTU UI Components are based on our many years of experience as well as the feedback from our SDK customers.

The following RTU UI Components and classes are currently provided:

  • Document Scanner - DocumentScannerActivity
  • Cropping - CroppingActivity
  • MRZ Scanner - MRZScannerActivity
  • Barcode and QR-Code Scanner - BarcodeScannerActivity & BatchBarcodeScannerActivity
  • Generic Document Recognizer - GenericDocumentRecognizerActivity
  • European Health Insurance Card Scanner - HealthInsuranceCardScannerActivity
  • Check Recognizer - CheckRecognizerActivity
  • Text Data Scanner - TextDataScannerActivity
  • VIN Scanner - VinScannerActivity

For more details please see the corresponding API docs of the classes as well as our example app.

Customization of RTU UI:

The main idea of the RTU UI is to provide simple-to-integrate and simple-to-customize screen components. Due to this idea there are some limitations with the possibilities of customization:

  • UI: All colors and text resources (localization)
  • Behavior: Enable or disable features like Multi-Page Scanning, Auto Snapping, Flashlight

If you need more customization options you have to implement custom screens (View Controllers for iOS and Activities for Android) using our Classic UI Components.

RTU UI Examples:

Please see our example app scanbot-sdk-example-xamarin on GitHub.

Classic UI Components

Our Classic UI Components allows you to build your custom screens which are very flexible and fully customizable. It is a set of easy to integrate and customize components (Views, Buttons, Handlers, Controllers, etc.) which can be embedded and extended in your custom screens.

For more details please see the corresponding API docs for iOS and Android.

Classic UI Examples:

Please see our example app scanbot-sdk-example-xamarin on GitHub.

Document Detection

The Scanbot SDK uses digital image processing algorithms to find rectangular, document like polygons in a digital image. As input a UIImage on iOS or Bitmap on Android are accepted.


Bitmap image = ...;
var detectionResult = SBSDK.DetectDocument(image);
if (detectionResult.Status == DocumentDetectionStatus.Ok)
{
var resultImage = detectionResult.Image as Bitmap;
var polygon = detectionResult.Polygon;
...
}

The result is an object of type DocumentDetectionResult which contains the detection status and on success the warped and cropped document image as well as the detected polygon. If there was no document detected the status enum provides the reason (noisy background, too dark, etc). The polygon is a list with 4 float points (one for each corner). Each point has coordinates in the range [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.

Alternatively, you can pass a Uri on Android or NSUrl on iOS of the source image.

public static DocumentDetectionResult DetectDocument(Android.Net.Uri imageUri, Android.Content.Context context)

Please note that NSUrl must be a valid file URL (file:///). Assets URLs (assets-library://) are not supported.

Image Filtering

Bitmap image = ...;
var resultImage = SBSDK.ApplyImageFilter(image, ImageFilter.Binarized);

Supported image filters:

  • ImageFilter.ColorEnhanced - Optimizes colors, contrast and brightness.
  • ImageFilter.Grayscale - Grayscale filter
  • ImageFilter.Binarized - Standard binarization filter with contrast optimization. Creates an 8-bit grayscale image with mostly black or white pixels.
  • ImageFilter.ColorDocument - MagicColor filter. Fixes white balance and cleans up the background.
  • ImageFilter.PureBinarized - A filter for binarizing an image. Creates an image with pixel values set to either pure black or pure white.
  • ImageFilter.BackgroundClean - Cleans up the background and tries to preserve photos within the image.
  • ImageFilter.BlackAndWhite - Black and white filter with background cleaning. Creates an 8-bit grayscale image with mostly black or white pixels.
  • ImageFilter.OtsuBinarization - A filter for black and white conversion using OTSU binarization.
  • ImageFilter.DeepBinarization - A filter for black and white conversion primary used for low contrast documents.
  • ImageFilter.EdgeHighlight - A filter that enhances edges in low contrast documents.
  • ImageFilter.LowLightBinarization - A binarization filter primarily intended for use on low contrast documents with hard shadows.

PDF Creation

The Scanbot SDK renders images into a PDF document and stores it as a given target file. For each image a separate page is generated.

Android.Net.Uri[] images = ...;
Android.Net.Uri pdfOutputFileUri = ...;
SBSDK.CreatePDF(images, pdfOutputFileUri, PDFPageSize.A4);

The following PDF page sizes are supported:

  • Letter: The page has US letter size: 8.5x11 inches. The image is fitted and centered within the page.
  • Legal: The page has US legal size: 8.5x14 inches. The image is fitted and centered within the page.
  • A3: The page has A3 size: 297x420 mm. The image is fitted and centered within the page.
  • A4: The page has the aspect ratio of the image, but is fitted into A4 size. Whether portrait or landscape depends on the images aspect ratio.
  • A5: The page has A5 size: 148x210 mm. The image is fitted and centered within the page.
  • B4: The page has B4 size: 250x353 mm. The image is fitted and centered within the page.
  • B5: The page has B5 size: 176x250 mm. The image is fitted and centered within the page.
  • Executive: The page has US executive size: 7.25x10.5 inches. The image is fitted and centered within the page.
  • US4x6: The page has US 4x6 size: 4x6 inches. The image is fitted and centered within the page.
  • US4x8: The page has US 4x8 size: 4x8 inches. The image is fitted and centered within the page.
  • US5x7: The page has US 5x7 size: 4x6 inches. The image is fitted and centered within the page.
  • Comm10: The page has COMM10 size: 4.125x9.5 inches. The image is fitted and centered within the page.
  • Custom: Each page is as large as its image at 72 dpi.

Detect barcodes from still image

The Scanbot SDK detects barcodes from an existing image (UIImage or Bitmap). Result format is equivalent to Barcode Scanner

var SDK = new IO.Scanbot.Sdk.ScanbotSDK(context);
BarcodeScanningResult result = sdk.CreateBarcodeDetector().DetectFromBitmap(bitmap, 0);

European Health Insurance Card Scanner

The Scanbot SDK detects and extracts data from European Health Insurance Cards.

Ready-To-Use-UI-Demo/Droid/MainActivity.cs
loading...
Ready-To-Use-UI-Demo/Droid/MainActivity.cs
loading...

Check Recognizer UI

You can use the Check Recognizer UI to conveniently scan and extract data from checks.

Ready-To-Use-UI-Demo/Droid/MainActivity.cs
loading...
Ready-To-Use-UI-Demo/Droid/MainActivity.cs
loading...

OCR - Optical Character Recognition

The Scanbot SDK provide simple and convenient APIs to run Optical Character Recognition (OCR) on images.

As result you can get:

  • a searchable PDF document with the recognized text layer (aka. sandwiched PDF document)
  • recognized text as plain text,
  • bounding boxes of all recognized paragraphs, lines and words,
  • text results and confidence values for each bounding box.

The Scanbot OCR feature is based on the Tesseract OCR engine with some modifications and enhancements. The OCR engine supports a wide variety of languages. For each desired language a corresponding OCR training data file (.traineddata) must be provided. Furthermore, the special data file osd.traineddata is required (used for orientation and script detection). The Scanbot SDK package contains no language data files to keep the SDK small in size. You have to download and include the desired language files in your app.

Preconditions to achieve a good OCR result

Conditions while scanning

A perfect document for OCR is flat, straight, in the highest possible resolution and does not contain large shadows, folds, or any other objects that could distract the recognizer. Our UI and algorithms do their best to help you meet these requirements. But as in photography, you can never fully get the image information back that was lost during the shot.

Languages

You can use multiple languages for OCR. But since the recognition of characters and words is a very complicated process, increasing the number of languages lowers the overall precision. With more languages, there are more results where the detected word could match. We suggest using as few languages as possible. Make sure that the language you are trying to detect is supported by the SDK and added to the project.

Size and position

Put the document on a flat surface. Take the photo from straight above in parallel to the document to make sure that the perspective correction does not need to be applied much. The document should fill most of the camera frame while still showing all of the text that needs to be recognized. This results in more pixels for each character that needs to be detected and hence, more detail. Skewed pages decrease the recognition quality.

Light and shadows

More ambient light is always better. The camera takes the shot at a lower ISO value, which results in less grainy photos. You should make sure that there are no visible shadows. If you have large shadows, it is better to take the shot at an angle instead. We also do not recommend using the flashlight - from this low distance it creates a light spot at the center of the document which decreases the recognition quality.

Focus

The document needs to be properly focused so that the characters are sharp and clear. The autofocus of the camera works well if you meet the minimum required distance for the lens to be able to focus. This usually starts at 5-10cm.

Typefaces

The OCR trained data is optimized for common serif and sans-serif font types. Decorative or script fonts drastically decrease the quality of the recognition.

Download and Provide OCR Language Files

You can find a list of all supported OCR languages and download links on this Tesseract page.

⚠️️️ Please choose and download the proper version of the language data files:

  • For the latest version of Scanbot Xamarin SDK 1.4.0 or newer - import TesseractLanguages from '/docs/shared/tesseract/_tesseract-languages.md';
  • For the older versions of Scanbot Xamarin SDK <= 1.3.0 -

    Data Files for Version 3.04/3.05

Download the desired language files as well as the osd.traineddata file and place them in the Assets sub-folder SBSDKLanguageData/ of your Android app or in the Resources sub-folder ScanbotSDKOCRData.bundle/ of your iOS app.

Droid/Assets/SBSDKLanguageData/eng.traineddata  // english language file
Droid/Assets/SBSDKLanguageData/deu.traineddata // german language file
Droid/Assets/SBSDKLanguageData/osd.traineddata // required special data file

OCR API

Android.Net.Uri[] images = ...;
Android.Net.Uri pdfOutputFileUri = ...;
OcrResult result = SBSDK.PerformOCR(images, new []{ "en", "de" }, pdfOutputFileUri);

// recognized plain text
string text = result.recognizedText;

// bounding boxes and text results of recognized paragraphs, lines and words:
List<OcrResultBlock> paragraphs = result.Paragraphs;
List<OcrResultBlock> lines = result.Lines;
List<OcrResultBlock> words = result.Words;

See the API reference of the OcrResult class for more details.

Document Quality Analyzer

Analyzes the quality of text in a document on a still image. The quality values are returned in the form of an enum DocumentQuality.

var qualityAnalyzer = new IO.Scanbot.Sdk.ScanbotSDK(applicationContext).CreateDocumentQualityAnalyzer();
var result = qualityAnalyzer.AnalyzeInBitmap(bitmap, 0);

Below are the enum values for document quality. Refer IO.Scanbot.Sdk.Process.Model.DocumentQualityResult for Android and SBSDKDocumentQuality for iOS.

  • NoDocument
  • VeryPoor
  • Poor
  • Reasonable
  • Good
  • Excellent

However, this is not as easy as it seems. If a scanned document has a predominantly white background, it will be considered a very poor quality document. It is therefore best to use the Document Quality Analyzer feature in conjunction with a finder view or on an already cropped document.

Camera UI for document scanning

The Scanbot SDK provides UI components for guided, automatic document scanning. These component classes handle all the camera and detection implementation details for you and can be used in your app. It is possible to customize the appearance and behavior of the guidance UI.

In the following examples we use the classes from the native SDK namespaces. Please make sure that you have defined the required permissions in your app.

The Android camera API might seem to be very tricky and far from being developer-friendly. To help you skip the same issues which we encountered while developing Scanbot we created ScanbotCameraXView. You can use it in your app's Activity or Fragment. ScanbotCameraX View has a method getPreviewBuffer() which allows you to register for preview frames from the camera. While you can implement your own smart features, Scanbot SDK comes with the built-in ContourDetectorFrameHandlerWrapper which performs contour detection (document detection) and outputs results to listeners. To draw detected contours, the Scanbot SDK provides the class PolygonView. To further improve the user experience you might want to automatically take a photo when a document is detected and conditions are good - we call this Autosnapping. For this purpose we have the AutoSnappingController class.

The following example demonstrates the use of all modules.

Example layout - CameraXViewDemo.axml:

Classical-Components-Demo/Droid/Resources/layout/CameraXViewDemo.axml
loading...

And the Document Scanner Activity class - CameraXViewDemoActivity.cs:

Classical-Components-Demo/Droid/Activities/CameraXViewDemoActivity.cs
loading...

Cropping UI

The Scanbot SDK provides some smart elements like magnetic lines or magnifier to build a UI for manual cropping of images.

In the following examples we use the classes from the native SDK namespaces.

Example code for an Activity:

Activity layout MyCroppingView.axml:

Classical-Components-Demo/Droid/Resources/layout/CroppingImageDemo.axml
loading...

And the Activity class:

Classical-Components-Demo/Droid/Activities/CroppingImageDemoActivity.cs
loading...

DocumentDetectionStatus enum:

  • Ok - Document detection was successful. The detected contour looks like a valid document.
  • OkButTooSmall - Document was detected, but it does not fill the desired area in the camera viewport. The quality can be improved by moving the camera closer to the document.
  • OkButBadAngles - Document was detected, but the perspective is wrong (camera is tilted relative to the document). Quality can be improved by holding the camera directly over the document.
  • OkButBadAspectRatio - Document was detected, but it has a wrong rotation relative to the camera sensor. Quality can be improved by rotating the camera 90 degrees.
  • ErrorTooDark - Document was not found, most likely because of bad lighting conditions.
  • ErrorTooNoisy - Document was not found, most likely because there is too much background noise (maybe too many other objects on the table, or the background texture is not monotonic).
  • ErrorNothingDetected - Document was not found. It is probably not in the viewport. Usually, it does not make sense to show any information to the user at this point.

Text Data Scanner UI

The Text Data Scanner UI can be used for conveniently scanning and extracting text.

Ready-To-Use-UI-Demo/Droid/MainActivity.cs
loading...
Ready-To-Use-UI-Demo/Droid/MainActivity.cs
loading...

VIN Scanner UI

The VIN Scanner UI can be used for scanning Vehicle Identification Numbers.

Ready-To-Use-UI-Demo/Droid/MainActivity.cs
loading...
Ready-To-Use-UI-Demo/Droid/MainActivity.cs
loading...

Want to scan longer than one minute?

Generate your free "no-strings-attached" Trial License and properly test the Scanbot SDK.

Get your free Trial License

What do you think of this documentation?