Barcode Plugin


Examples Source Code

The source code of all example use cases of the Barcode 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 plugins. Additional Information on how to implement this on Android an be found at Simultaneous Barcode Scanning

With the Anyline Barcode-Plugin 23 different formats of Bar- and QR-Codes can be scanned.
Futhermore, the Plugin able to scan barcodes that are rotated, which makes it easier to scan the codes.

The description of all parameters can be found at Modules > Barcode

How to implement the Barcode

This is a step by step guide on how to implement the Barcode Module in Android.

Get the Scan View 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 Barcode ScanView to the layout
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="io.anyline.examples.barcode.ScanBarcodeActivity"
    android:animateLayoutChanges="true">

    <io.anyline.view.ScanView
        android:id="@+id/barcode_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
        barcodeScanView = (ScanView) findViewById(R.id.scan_view);

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 configuration which hold the scan view ui configuration (cutoutConfig and ScanFeedbackConfig)		
	ScanViewPluginConfig barcodeScanviewPluginConfig = new ScanViewPluginConfig(getApplicationContext(), "barcode_view_config.json");

Initialize ScanViewPlugin

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

Initialize the ScanViewPlugin
	//init the scan view
	BarcodeScanViewPlugin scanViewPlugin = new BarcodeScanViewPlugin(getApplicationContext(), getString(R.string.anyline_license_key), barcodeScanviewPluginConfig, "BARCODE");

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)
	//set the base scanViewConfig to the ScanView
	barcodeScanView.setScanViewConfig(barcodeBaseScanViewConfig);

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
	//set the scanViewPlugin to the ScanView
	barcodeScanView.setScanViewPlugin(scanViewPlugin);

The Barcode Result Listener

Changes in 4

The result listener changed in version 4.

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

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

onResult

This is the main callback method for the Barcode module. It is called once a valid result has been found.

The callback returns the result as a String, the BarcodeFormat, as well as an image of the result.

The result is an instance of BarcodeResult, which holds the detected barcode-string, the detected barcode format, the outline of the text on the frame, the confidence of the result, the image that was processed, as well as a cutout image and the full image.

See BarcodeResult in the Javadocs

onResult
	//add result listener
	scanViewPlugin.addScanResultListener(new ScanResultListener<BarcodeScanResult>() {
		@Override
		public void onResult(BarcodeScanResult result) {
			resultText.setText(result.getResult());
		}
	});
Type Description
BarcodeResult The barcode data and supplementary scan information
Parameter Type Description
getResult() String The barcode result
getCutoutImage() AnylineImage An image of the barcode, cropped to the cutout
getFullImage() AnylineImage The full image the result was found on
getBarcodeFormat() BarcodeScanView.BarcodeFormat The scan mode that was used
getOutline() List<PointF> The outline of the detected barcode in the full image

Scan Plugin

BareCodeScanPlugin is initialized internally once the BarcodeScanViewPlugin is instantiated.

Enabling or disabling the reporting

The reporting of the (intermediate) results is turned off for this Module.

Start Scanning

After everything is initialised, you can start the scanning process, by calling startScanning() on the scanView. It is advised to place this call in the onResume() lifecycle method of Android.

startScanning
    @Override
    protected void onResume() {
        super.onResume();
        //start the actual scanning
        barcodeScanView.start();
    }

Stop Scanning

To stop the scanning process, call cancelScanning() on the barcodeScanView.

To make sure the SDK is properly stopped upon leaving the Activity, make sure to place cancelScanning() and releaseCamera()/releaseCameraInBackground() in the onPause() lifecycle method of Android.

cancelScanning
    @Override
    protected void onPause() {
        super.onPause();
        //stop the scanning
        barcodeScanView.stop();
        //release the camera (must be called in onPause, because there are situations where
        // it cannot be auto-detected that the camera should be released)
        barcodeScanView.releaseCameraInBackground();
    }

Set your Barcode Format

Optionally, you can set the desired Barcode Format, to limit the scanning process, and speed it up.

Set the Barcode Format
barcodeScanView.setBarcodeFormats(BarcodeScanView.BarcodeFormat.AZTEC, BarcodeScanView.BarcodeFormat.QR_CODE);

Full Example Activity

Barcode Module Example
/*
 * Anyline
 * ScanBarcodeActivity.java
 *
 * Copyright (c) 2015 9yards GmbH
 *
 * Created by martin at 2015-07-03
 */

package io.anyline.examples.barcode;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.WindowManager;
import android.widget.TextView;

import at.nineyards.anyline.camera.CameraController;
import at.nineyards.anyline.camera.CameraOpenListener;
import at.nineyards.anyline.models.AnylineImage;
import at.nineyards.anyline.modules.barcode.BarcodeResult;
import at.nineyards.anyline.modules.barcode.BarcodeResultListener;
import at.nineyards.anyline.modules.barcode.BarcodeScanView;
import io.anyline.examples.R;

/**
 * Example activity for the Anyline-Barcode-Module
 */
public class ScanBarcodeActivity extends AppCompatActivity implements CameraOpenListener {

    private static final String TAG = ScanBarcodeActivity.class.getSimpleName();
    private BarcodeScanView barcodeScanView;
    private TextView resultText;

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

        resultText = (TextView) findViewById(R.id.text_result);

        barcodeScanView = (ScanView) findViewById(R.id.scan_view);
        // add a camera open listener that will be called when the camera is opened or an error occurred
        //  this is optional (if not set a RuntimeException will be thrown if an error occurs)
        barcodeScanView.setCameraOpenListener(this);
        // the view can be configured via a json file in the assets, and this config is set here
        // (alternatively it can be configured via xml, see the Energy Example for that)
        //barcodeScanView.setScanConfig("barcode_view_config.json");

        // optionally limit the barcode format to (multiple) specific types
        //barcodeScanView.setBarcodeFormats(BarcodeScanView.BarcodeFormat.QR_CODE,
        //        BarcodeScanView.BarcodeFormat.CODE_128);
	//init the scanViewPlugin configuration which hold the scan view ui configuration (cutoutConfig and ScanFeedbackConfig)		
	ScanViewPluginConfig barcodeScanviewPluginConfig = new ScanViewPluginConfig(getApplicationContext(), "barcode_view_config.json");
	//init the scan view
	BarcodeScanViewPlugin scanViewPlugin = new BarcodeScanViewPlugin(getApplicationContext(), getString(R.string.anyline_license_key), barcodeScanviewPluginConfig, "BARCODE");
	//init the base scanViewconfig which hold camera and flash configuration
	BaseScanViewConfig barcodeBaseScanViewConfig = new BaseScanViewConfig(getApplicationContext(), "barcode_view_config.json");
	//set the base scanViewConfig to the ScanView
	barcodeScanView.setScanViewConfig(barcodeBaseScanViewConfig);
	//set the scanViewPlugin to the ScanView
	barcodeScanView.setScanViewPlugin(scanViewPlugin);
	//add result listener
	scanViewPlugin.addScanResultListener(new ScanResultListener<BarcodeScanResult>() {
		@Override
		public void onResult(BarcodeScanResult result) {
			resultText.setText(result.getResult());
		}
	});
    }

    @Override
    protected void onResume() {
        super.onResume();
        //start the actual scanning
        barcodeScanView.start();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //stop the scanning
        barcodeScanView.stop();
        //release the camera (must be called in onPause, because there are situations where
        // it cannot be auto-detected that the camera should be released)
        barcodeScanView.releaseCameraInBackground();
    }

    @Override
    public void onCameraOpened(CameraController cameraController, int width, int height) {
        //the camera is opened async and this is called when the opening is finished
        Log.d(TAG, "Camera opened successfully. Frame resolution " + width + " x " + height);
    }

    @Override
    public void onCameraError(Exception e) {
        //This is called if the camera could not be opened.
        // (e.g. If there is no camera or the permission is denied)
        // This is useful to present an alternative way to enter the required data if no camera exists.
        throw new RuntimeException(e);
    }
}