Implementing Anyline

This section will guide you through the implementation process of an Anyline Scan View, including configuration and handling scan results.

Adding a Scan View

Anyline version 3.10 namespace changes

As of Anyline version 3.10, the Anyline Xamarin namespaces have changed for Android: the Xamarin modules now reside under the namespace AT.Nineyards.Anylinexamarin.Support.Modules.

  • Open the Main.axml file in your Resources/Layout node
  • Switch from the Design tab to the Source tab
  • Change the XML Code to the example shown below
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <at.nineyards.anylinexamarin.support.modules.barcode.BarcodeScanView
        android:id="@+id/AnylineScanView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

If you switch back from to the Design tab, the user interface should look like this:

The Android Layout Design for Xamarin

You now added a Barcode Scan View to your Activity. However, depending on which Module you want to implement, choose one of these module-specific views:

  • at.nineyards.anylinexamarin.support.modules.ocr.AnylineOcrScanView (see /toc/modules/anyline_ocr/index Module)
  • at.nineyards.anylinexamarin.support.modules.energy.EnergyScanView (see /toc/modules/energy/index Module)
  • at.nineyards.anylinexamarin.support.modules.barcode.BarcodeScanView (see /toc/modules/barcode/index Module)
  • at.nineyards.anylinexamarin.support.modules.mrz.MrzScanView (see /toc/modules/mrz/index Module)
  • at.nineyards.anylinexamarin.support.modules.document.DocumentScanView (see /toc/modules/document/index Module)

Providing a View Configuration to the Scan View

With the Anyline View Configuration, you can define the Look & Feel of your scanning app.

The configuration parameters define the visual information presented to the user, as well as the scan settings (like the resolution, etc.) and feedback that is presented to the user.

The full parameter list of the configuration can be found at Anyline View Configuration.

You can adapt your scan view easily by either adding a .json file or using XML-attributes in the layout file (e.g. Main.axml - see android_view_configuration_load_xml in the Android section).

In this example, we will create a .json file and fill it with properties.

Visual Studio

  • In your Assets treenode, right-click on the folder and select Add > New Item..
  • Choose Visual C# > Text File and name it scanConfig.json

Xamarin Studio

  • In your Assets treenode, right-click on the folder and select Add > New File..
  • Choose Misc > Empty Text File and name it scanConfig.json

Make sure that the build action for this file is set to AndroidAsset.

Now open the file and paste the code from the following example:

{
  "captureResolution":"720p",

  "cutout": {
    "style": "rect",
    "maxWidthPercent": "80%",
    "alignment": "top_half",
    "ratioFromSize": {
      "width": 100,
      "height": 80
    },
    "strokeWidth": 4,
    "cornerRadius": 10,
    "strokeColor": "00AFFE",
    "outerColor": "000000",
    "outerAlpha": 0.3
  },
  "flash": {
    "mode": "manual",
    "alignment": "bottom_right"
  },
  "beepOnResult": false,
  "vibrateOnResult": true,
  "blinkAnimationOnResult": true,
  "cancelOnResult": false
}

The View Configuration can be set via

Set the View Configuration
scanView.SetConfigFromAsset("scanConfig.json");

Note

Detailed information for each parameter can be found at Full Parameter List in the View Configuration section

Adding the Module and configure it

You can add the Scan View to your MainActivity in one of the following ways:

// for barcode scanning
using AT.Nineyards.Anylinexamarin.Support.Modules.Barcode;

// for energy meter scanning
using AT.Nineyards.Anylinexamarin.Support.Modules.Energy;

// for MRZ scanning
using AT.Nineyards.Anylinexamarin.Support.Modules.MRZ;

// for Anyline OCR scanning
using AT.Nineyards.Anylinexamarin.Support.Modules.Ocr;

// for Document scanning
using AT.Nineyards.Anylinexamarin.Support.Modules.Document;

The Anyline OCR Module Configuration

In case you want to use the /toc/modules/anyline_ocr/index, you can set your parameters analogous to the Parameters on Android.

Simply set the parameters to an instance of AnylineOCRConfig, and set the instance to your scanView:

Set the Anyline OCR Parameters
AnylineOcrConfig anylineOcrConfig = new AnylineOcrConfig();
// ...
scanView.SetAnylineOcrConfig(anylineOcrConfig);

Syntax

Apart from anylineOcrConfig.SetScanMode() and anylineOcrConfig.SetTesseractLanguages(), the parameters are set directly. For example anylineOcrConfig.MinCharHeight = 20;

Loading a Custom Command File

To load a Custom Command File in Xamarin.Android, simply set it to the AnylineOCRConfig:

Load a Custom Commmand File
anylineOcrConfig.CustomCmdFile = "your_command_file.ale"

OnCreate

The OnCreate lifecycle method is where the Scan View is set up, a View Configuration is loaded, and the Module initialised together with the previously generated License Key.

// Setup our scan view here
protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);

    // Set our view from the "main" layout resource
    SetContentView(Resource.Layout.Main);

    // allocate the element we defined earlier in our Main.axml
    scanView = FindViewById<BarcodeScanView>(Resource.Id.AnylineScanView);

    // Load config from the .json file
    // We don't need this, if we define our configuration in the XML code
    scanView.SetConfigFromAsset("scanConfig.json");

    // Initialize with our license key and our result listener
    // Important: use the license key for your app package
    scanView.InitAnyline(<INSERT YOUR LICENSE KEY HERE>, this);

    // Don't stop scanning when a result is found
    scanView.SetCancelOnResult(false);

    // Register event that shows if the camera is initialized
    scanView.CameraOpened += (s, e) => {
        Log.Debug("Camera", "Camera opened successfully. Frame resolution " + e.Width + " x " + e.Height);
    };

    // Register event that shows if the camera returns an error
    scanView.CameraError += (s, e) => {
        Log.Error("Camera", "OnCameraError: " + e.Event.Message);
    };
}

Tip

InitAnyline takes the license key and a module-specific result listener as arguments. A convenient way is to store the license key in a const String and let the activity implement the interface of the result listener, then pass this as second argument.

Start Scanning in OnResume

OnResume is usually the lifecycle method where you want to start the scanning process. To do so, you can follow the example below.

// This method is invoked if the application state changes from "Paused" or "Started" to "Resumed"
protected override void OnResume()
{
    base.OnResume();
    scanView.StartScanning();
}

Handling Scan Results

The Results of the Modules are provided by a ResultListener. Each Module has its own ResultListener. Below you can see an example of the IBarcodeResultListener Interface.

The easiest way to make use of that listener is to implement the interface of the listener in your scan activity like this:

[Activity(Label = "AnylineExampleAndroid", MainLauncher = true, Icon = "@drawable/icon", HardwareAccelerated = true)]
public class MainActivity : Activity, IBarcodeResultListener // or any other module-specific listener, depending on your scan module
{
    ...
}

The interface can be implemented by right-clicking on the ResultListener Interface and selecting Implement Interface explicitly. A method will be generated that is called each time our scan view is scanning and reporting a result.

// Each time, a result is found, this method is invoked
void IBarcodeResultListener.OnResult(BarcodeResult scanResult)
{
        // Show a short Toast message if a result is found
    Toast.MakeText(this, scanResult.Result.ToString(), ToastLength.Short).Show();
}

Test your app on an Android device

Build your project and deploy your application on your Android device. More detailed information about all Modules in the Anyline SDK can be found in the Modules section of this documentation.