React Native 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 React Native SDK provides a simple and convenient API 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 by passing TESSERACT to the engineMode arguments property:
engineMode: EngineMode- the OCR engine mode, eitherSCANBOT_OCRorTESSERACT;languages: Array<String>- a set of languages to be used for OCR (needed only forTESSERACTmode);
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 (SCANBOT_OCR) supports German and English. These languages are integrated into the SDK and work out-of-the-box, without requiring additional modules.
For 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.
Using the OCR engine
Only for '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 React Native SDK versions 3.0.0 or higher:
- For Scanbot SDK versions 2.x or lower:
Download the desired language files as well as the osd.traineddata file and make sure they will be packaged in your app as:
- for Android: as assets in the sub-folder
android/app/src/main/assets/ocr_blobs - for iOS: as resources in the sub-folder
'Pods/ScanbotSDK/AdditionalData/ScanbotSDKOCRData.bundle'
Alternatively, to keep the app package small, you can download and provide the language files in your app at runtime. Implement a suitable download functionality of the desired language files and the osd.traineddata file and place them in the languageDataPath directory, which you can determine with the getOCRConfigs method at runtime.
Language codes
The Scanbot SDK API uses 2-letter ISO codes, e.g.:
en- Englishde- German
The Tesseract language data files are identified by 3-letter language codes, e.g.:
eng- Englishdeu- German
Example
If you want to perform OCR with languages English and German, you have to download and install the following data files:
eng.traineddata: language file for Englishdeu.traineddata: language file for Germanosd.traineddata: special data file for orientation and script detection
Then, in the Scanbot SDK module, use languages: ["en", "de"].
OCR APIs
Example code for performing OCR
loading...
Example of an OCR result:
{
"plainText":"Ut enim ad minim veniam, quis nostrud exercitation\nullamco laboris nisi ut aliquip ex ea commodo\nconsequat\n",
"pages":[
{
"text":"Ut enim ad minim veniam, quis nostrud exercitation\nullamco laboris nisi ut aliquip ex ea commodo\nconsequat\n",
"words":[
{
"boundingBox":{
"y":0.46182008368200839,
"x":0.011823899371069183,
"width":0.040000000000000001,
"height":0.070606694560669453
},
"text":"Ut",
"confidence":96.546516418457031
},
{
"boundingBox":{
"y":0.46443514644351463,
"x":0.064402515723270437,
"width":0.091320754716981131,
"height":0.06903765690376569
},
"text":"enim",
"confidence":95.442947387695312
},
...
{
"boundingBox":{
"y":0.68462343096234313,
"x":0.010817610062893081,
"width":0.22213836477987423,
"height":0.13702928870292888
},
"text":"consequat",
"confidence":23.432151794433594
}
],
"lines":[
{
"boundingBox":{
"y":0.41684100418410042,
"x":0.011823899371069183,
"width":0.98037735849056606,
"height":0.14225941422594143
},
"text":"Ut enim ad minim veniam, quis nostrud exercitation\n",
"confidence":95.124320983886719
},
{
"boundingBox":{
"y":0.57112970711297073,
"x":0.011572327044025157,
"width":0.86792452830188682,
"height":0.1192468619246862
},
"text":"ullamco laboris nisi ut aliquip ex ea commodo\n",
"confidence":95.142372131347656
},
{
"boundingBox":{
"y":0.68462343096234313,
"x":0.010817610062893081,
"width":0.22213836477987423,
"height":0.13702928870292888
},
"text":"consequat\n",
"confidence":23.432151794433594
}
],
"paragraphs":[
{
"boundingBox":{
"y":0.41684100418410042,
"x":0.011320754716981131,
"width":0.98088050314465414,
"height":0.34884937238493724
},
"text":"Ut enim ad minim veniam, quis nostrud exercitation\nullamco laboris nisi ut aliquip ex ea commodo\nconsequat\n",
"confidence":90.915626525878906
}
]
}
...
]
}
Want to scan longer than one minute?
Generate a free trial license to test the Scanbot SDK thoroughly.
Get free trial license