Android OCR Module
The Scanbot SDK's OCR Engine can transform written text into machine-readable data – both from still images and a live camera stream. It is the backbone of the SDK's Data Capture Modules, enabling fast and accurate data extraction from various document formats.
The Scanbot SDK for Android provides a simple and convenient API (OcrEngine
) to run Optical Character Recognition (OCR) on images.
As a result, you get:
- recognized text as plain text,
- bounding boxes of all recognized paragraphs, lines and words,
- text results and confidence values for each bounding box.
The OCR feature is based on the Scanbot OCR Engine created and polished by the Scanbot SDK team to provide the best text recognition speed and quality for our users.
The Scanbot OCR feature based on the Tesseract OCR engine is still available and can be enabled with the OcrEngine#setOcrConfig(ocrConfig: OcrConfig)
method,
where OcrConfig
is a config class with the following properties:
engineMode: EngineMode
: the OCR engine mode, eitherEngineMode.SCANBOT_OCR
orEngineMode.TESSERACT
languages: Set<Language>
: a set of languages to be used for OCR (needed only forEngineMode.TESSERACT
mode)
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).
To keep the Scanbot SDK package's size as small as possible, it contains no language data files. You have to download and include the desired language files in your app.
Preconditions to achieve good OCR results
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. The SDK's 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.
Size and position
Put the document on a flat surface. Take the photo from straight above and hold the device in parallel to the document to minimize the need for perspective correction. The document should fill as much of the camera frame as possible 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. Try to make sure there are no visible shadows. If you encounter large shadows, take the shot at an angle instead.
We do not recommend using the flashlight – from a small distance, using it creates a light spot at the center of the document that decreases the recognition quality.
Focus
The document needs to be properly focused so that the characters are sharp and clear. The auto-focus of the camera works well if you meet the minimum required distance for the lens to be able to focus, usually around 5–10 centimeters (approx. 2–4 inches).
Typefaces
The Scanbot OCR Engine is optimized for common serif and sans-serif font types. Decorative or script fonts drastically decrease the recognition quality.
Languages
The Scanbot OCR engine (EngineMode.SCANBOT_OCR
) supports German and English. These languages are integrated into the SDK and work out-of-the-box, without requiring additional modules.
For EngineMode.TESSERACT
, 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 that the detected word could match. We recommend using as few languages as possible. Make sure the language you are trying to detect is supported by the SDK and has been added to the project.
Integration
Adding the feature as a dependency
The OCR feature is included in Scanbot SDK package 2. Therefore, add the dependency io.scanbot:sdk-package-2
or higher in your build.gradle
along with the necessary assets:
implementation("io.scanbot:sdk-package-2:$scanbotSdkVersion")
implementation("io.scanbot:sdk-common-ocr-assets:$scanbotSdkVersion") // <<-- please also add this dependency
Get the latest $scanbotSdkVersion
from the changelog.
(Only for EngineMode.TESSERACT
: Downloading and providing the OCR language files)
You can find a list of all supported OCR languages and corresponding download links in the Tesseract documentation.
Please download the proper version of the language data files:
- For Scanbot SDK versions 1.50.0 or higher:
- For Scanbot SDK versions 1.41.0 or lower:
Download the files and place them in the assets sub-folder assets/ocr_blobs/
of your app.
Example:
assets/ocr_blobs/osd.traineddata
(required special data file)assets/ocr_blobs/eng.traineddata
(English language file)assets/ocr_blobs/deu.traineddata
(German language file)
Initialization
To initialize the Scanbot SDK, call the ScanbotSDKInitializer#initialize(context: Context)
method.
In your Application
class:
loading...
For EngineMode.TESSERACT
, call ScanbotSDKInitializer#prepareOCRLanguagesBlobs(true)
before the first usage of the OCR feature.
Then get an instance of the OcrEngine
from ScanbotSDK
.
In your Activity
or Service
class:
loading...
For EngineMode.TESSERACT
, to achieve better OCR results, you can enable image binarization in OcrSettings
:
loading...
Define the list of languages and set the engine mode to EngineMode.TESSERACT
:
loading...
Want to scan longer than one minute?
Generate a free trial license to test the Scanbot SDK thoroughly.
Get free trial license