Anyline OCR Plugin


Examples Source Code

The source code of all example use cases of the Anyline OCR Plugin can be found in the Android SDK Bundle

Plugin Description

Simultaneous Barcode Scanning

Starting from SDK 3.8 Anyline supports simultaneous barcode scanning for any plugin. Additional Information on how to implement this on Android an be found at Simultaneous Barcode Scanning

The Anyline OCR Plugin can be used to create plenty of different use cases. It offers a generic interface, which can be used to scan a variety of texts, from IBAN codes to Scrabble letters. Our Bottlecap, Shipping Container, Universal Serial Number and Vehicle Identification Number scanners are all use cases implemented with the AnylineOCR Plugin.

The description of all parameters can be found at Plugins > AnylineOCR

How to implement the Anyline OCR Plugin

This is a step by step guide on how to implement the Anyline OCR Plugin in Android.

Get the Anyline OCR Plugin in Java

Changes in 4

Starting with Anyline 4, the ScanView is a generic type which should not be cast to each scan view type.

First add the Scan View to your xml-layout.

Add the Anyline OCR ScanView to the layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/main_layout"
    android:animateLayoutChanges="true">


    <io.anyline.view.ScanView
        android:id="@+id/scan_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</RelativeLayout>

In the next step, go to your Java Activity code and get the view from the layout in onCreate() or onActivityCreated()

Get the Scan View
        scanView = (ScanView) findViewById(R.id.scan_view);

Set your Parameters

First, you have to copy the traineddata files you want to use in the OCR part of the scanning process (see tesseractLanguages for a description of the parameter)

Deprectated

Use setLanguages() on the AnylineOCRConfig instead. This is shown below. copyTrainedData will be removed in the future.

Option 1: Set your parameters manually

Also in onCreate() or onActivityCreated(), set your parameters to an instance of AnylineOCRConfig

Set the plugin parameters
        AnylineOcrConfig anylineOcrConfig = new AnylineOcrConfig();
        anylineOcrConfig.setLanguages("tessdata/eng_no_dict.traineddata", "tessdata/deu.traineddata");
        // use predefined whitelist and regular expression
        anylineOcrConfig.setCharWhitelist(AnylineOcrConfig.AnylineOcrRegex.ISBN.getWhiteList());
        anylineOcrConfig.setValidationRegex(AnylineOcrConfig.AnylineOcrRegex.ISBN.getRegex());
        // AUTO ScanMode automatically detects the correct text without any further parameters to be set
        anylineOcrConfig.setScanMode(AnylineOcrConfig.ScanMode.AUTO);

Option 2: Load your Custom Command File

If you are provided with a custom command file by Anyline, you can load the file with an instance of AnylineOCRConfig

Load a Custom Command File
AnylineOcrConfig anylineOcrConfig = new AnylineOcrConfig();
anylineOcrConfig.setCustomCmdFile("license_plates.ale");

// set languages if required
anylineOcrConfig.setLanguages("tessdata/eng_no_dict.traineddata");

Initialize and Configure Anyline

ScanViewPlugin

Initialize ScanViewPlugin configuration

The ScanViewPlugin configuration holds the UI properties for cutout and visual feedback.

Initialize the ScanViewPlugin configuration
        //init the scanViewPlugin config
        ScanViewPluginConfig ocrScanViewPluginConfig = new ScanViewPluginConfig(getApplicationContext(), "isbn_view_config.json");

Initialize ScanViewPlugin

Together with your Anyline License Key Generation, you can now initialise OcrViewPlugin. The OcrScanViewPlugin will internally initialize the OcrScanPlugin. In java, this should be called in onCreate() or onActivityCreated().

Initialize the ScanViewPlugin
        //init the scan view
        OcrScanViewPlugin scanViewPlugin = new OcrScanViewPlugin(getApplicationContext(), getString(R.string.anyline_license_key), anylineOcrConfig, ocrScanViewPluginConfig, "OCR");

ScanView

Set the Base Configuration

The base configurations are holding the camera and the flash properties. You can set it the following way, also in onCreate() or onActivityCreated()

Set the Base Configuration(Camera and Flash configuration) and the View Configuration (Cutout and ScanView configuration)
        //init the base config used for camera and flash
        BaseScanViewConfig ocrBaseScanViewConfig = new BaseScanViewConfig(getApplicationContext(), "isbn_view_config.json");
        //set the scan Base config
        scanView.setScanViewConfig(ocrBaseScanViewConfig);

Set the ScanViewPlugin to the ScanView

A ScanView is holding a ScanViewPlugin, therefore a ScanViewPlugin has to be set to the ScanView.

Set the ScanViewPlugin to the ScanView
        // optionally limit the barcode format to (multiple) specific types

The Anyline OCR Listener

In the Anyline SDK, you will be provided final and intermediate results via a ResultListener.

In case of the Anyline OCR plugin, the listener is called AnylineOcrResultListener. This section describes the AnylineOcrResultListener callbacks in detail.

onResult

Changes in 4

The result listener changed in version 4.

With Anyline 4, all listeners are called ScanResultListener. In case of the Ocr Plugin, the listener is of type OcrScanResult. This section describes the ScanResultListener callback in detail.

In the Ocr Plugin of Anyline SDK, you will be provided the final results via a ResultListener.

This is the main callback method for the Anyline OCR plugin. It is called once a valid result according to the Set your Parameters has been found.

The result is an instance of OcrScanResult, which holds the detected text, the outline of the text on the frame, the confidence of the result, the image that was processed, as well as a thresholded image, a cutout image and the full image.

See AnylineOcrResult in the Javadocs

onResult
	//add the scan result listener
 	scanViewPlugin.addScanResultListener(new ScanResultListener<OcrScanResult>() {
            @Override
            public void onResult(OcrScanResult result) {

                if (!result.toString().isEmpty()) {

                    Intent i = new Intent(ScanIsbnActivity.this, IsbnActivity.class);
                    i.putExtra(IsbnActivity.ISBN_INPUT, result.getResult().toString().trim());

                    startActivity(i);
                }
            }

        });
  • result

Changed in version 3.10.

Type Description
AnylineOcrResult The scanned result and supplementary scan information
Parameter Type Description
getResult() String The scan result
getCutoutImage() AnylineImage An image of the scan, cropped to the cutout
getFullImage() AnylineImage The full image the result was found on
getThresholdedImage() AnylineImage The input image, cropped to the cutout and thresholded
getOutline() List<PointF> The outline of the detected digits in the full image
getConfidence() Integer The confidence of the SDK in the detected result

Enabling or disabling the reporting

The reporting of the (intermediate) results (including an image) helps us to improve the accuracy of the SDK and detect potential shortcomings.

However, you can easily turn the reporting off (onCreate() or onActivityCreated())

setReportingEnabled
        // disable the reporting if set to off in preferences
        scanView.setReportingEnabled(PreferenceManager.getDefaultSharedPreferences(this).getBoolean(
                SettingsFragment.KEY_PREF_REPORTING_ON, true));

Reporting & License

Depending, on your license, the reporting may not be turned on or off.

Multiple Scan Use Cases

If you have multiple scan use cases in your application, and you want different reporting for each usee case, you have to set setReportingEnabled for each use case seperately. I.e. setting reporting to on means that it is turned on for your whole application.

Start Scanning

After everything is initialised, you can start the scanning process, by calling start() on the scanView. It is advised to place this call in the onResume() lifecycle method of Android. The``start()`` method on the scanView will internally call the start method from the scan plugin. The Scan Plugin is the most important in order to start the scanning process.

start
    @Override
    protected void onResume() {
        super.onResume();
	//start the scanning 
	//internally scanView is calling the scanPlugin which is starting the scanning part
        scanView.start();
    }

Note

A postDelayed can be applied in case the scan is to quick. It is just a way of preventing too fast scanning results if the scan target is not properly positioned yet. Another method to prevent results that are too quick and don’t represent the full target, is to set a more sophisticated validationRegex in the /toc/plugins/anyline_ocr/index parameters.

Stop Scanning

To stop the scanning process, call stop() on the scanView.

To make sure the SDK is properly stopped upon leaving the Activity, make sure to place stop() and releaseCamera()/releaseCameraInBackground() in the onPause() lifecycle method of Android. The stop() method is referencing internally the stop method from the scan plugin. The Scan Plugin s the most important in order to stop the scanning process.

stop
    @Override
    protected void onPause() {
        super.onPause();

	
        scanView.stop();
        scanView.releaseCameraInBackground();
    }

Full Example Activity

AnlineOCR Plugin Example
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;

import at.nineyards.anyline.camera.AnylineViewConfig;
import at.nineyards.anyline.modules.ocr.AnylineOcrConfig;
import at.nineyards.anyline.modules.ocr.AnylineOcrResult;
import at.nineyards.anyline.modules.ocr.AnylineOcrResultListener;
import at.nineyards.anyline.modules.ocr.AnylineOcrScanView;
import io.anyline.examples.R;
import io.anyline.examples.SettingsFragment;
import io.anyline.examples.ocr.apis.IsbnActivity;

public class ScanIsbnActivity extends AppCompatActivity {
    private static final String TAG = ScanIsbnActivity.class.getSimpleName();
    private ScanView scanView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set the flag to keep the screen on (otherwise the screen may go dark during scanning)
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        setContentView(R.layout.activity_anyline_ocr);

        String lic = getString(R.string.anyline_license_key);
        scanView = (ScanView) findViewById(R.id.scan_view);

        // see ScanScrabbleActivity for a more detailed description
        AnylineOcrConfig anylineOcrConfig = new AnylineOcrConfig();
        anylineOcrConfig.setLanguages("tessdata/eng_no_dict.traineddata", "tessdata/deu.traineddata");
        // use predefined whitelist and regular expression
        anylineOcrConfig.setCharWhitelist(AnylineOcrConfig.AnylineOcrRegex.ISBN.getWhiteList());
        anylineOcrConfig.setValidationRegex(AnylineOcrConfig.AnylineOcrRegex.ISBN.getRegex());
        // AUTO ScanMode automatically detects the correct text without any further parameters to be set
        anylineOcrConfig.setScanMode(AnylineOcrConfig.ScanMode.AUTO);

        //init the scanViewPlugin config
        ScanViewPluginConfig ocrScanViewPluginConfig = new ScanViewPluginConfig(getApplicationContext(), "isbn_view_config.json");
        //init the scan view
        OcrScanViewPlugin scanViewPlugin = new OcrScanViewPlugin(getApplicationContext(), getString(R.string.anyline_license_key), anylineOcrConfig, ocrScanViewPluginConfig, "OCR");
        //init the base config used for camera and flash
        BaseScanViewConfig ocrBaseScanViewConfig = new BaseScanViewConfig(getApplicationContext(), "isbn_view_config.json");
        //set the scan Base config
        scanView.setScanViewConfig(ocrBaseScanViewConfig);
        //set the scan view plugin to the scan view
        scanView.setScanViewPlugin(scanViewPlugin);
	//add the scan result listener
 	scanViewPlugin.addScanResultListener(new ScanResultListener<OcrScanResult>() {
            @Override
            public void onResult(OcrScanResult result) {

                if (!result.toString().isEmpty()) {

                    Intent i = new Intent(ScanIsbnActivity.this, IsbnActivity.class);
                    i.putExtra(IsbnActivity.ISBN_INPUT, result.getResult().toString().trim());

                    startActivity(i);
                }
            }

        });

        // disable the reporting if set to off in preferences
        scanView.setReportingEnabled(PreferenceManager.getDefaultSharedPreferences(this).getBoolean(
                SettingsFragment.KEY_PREF_REPORTING_ON, true));
    }

    @Override
    protected void onResume() {
        super.onResume();
	//start the scanning 
	//internally scanView is calling the scanPlugin which is starting the scanning part
        scanView.start();
    }

    @Override
    protected void onPause() {
        super.onPause();

	
        scanView.stop();
        scanView.releaseCameraInBackground();
    }
}