Android Barcode Widget

The Android barcode detection widget processes frames from the device's camera, checking for barcodes. A preview of the camera will be displayed within the widget's bounds, optionally overlaying barcode data when a barcode is detected.

Multiple barcodes can be detected in a single frame. Turning the guide on will set the widget to single detect mode, with only barcodes overlapping the guide being detected.

Callbacks are sent on barcode detection and removal.

Including the Widget

To include the widget in your standalones, include the Android Barcode Scanner in the Inclusions pane of the standalone settings stack.

Including the library

Creating the Widget

A barcode scanning widget can be created by dragging it out from the Tools Palette, where it appears with the following icon:

Alternatively it can be created in script using:

create widget as "com.livecode.widget.native.android.barcodescanner"

Using the Widget

When the scanning widget is present on any active card, it will automatically begin scanning, displaying a preview of the devices camera in the widget's bounds.

The type of barcodes to detect can be limited by setting the widget's acceptedFormats property. This is a comma separated list containing formats from the following list:

If set to empty, all barcode types will be detected.

The camera used to detect can be controlled using the device property of the widget, setting to either front or back.

The resolution and frame rate of the camera can be controlled using the previewWidth, previewHeight and previewFPS properties of the widget. For best results, it is recommended to leave the resolution properties to the defaults. If barcode detection is too slow, then consider reducing frame rate. If the camera preview is too jerky, then consider increasing frame rate.

The widget's torchMode property can be used to determine when the device's flash light should be turned on. The flash light can be used to improve scanning results in low light conditions. If set to off, the flash light will never turn on. If set to on, the flash light will always be on. If set to auto, the flash light will turn on when the ambient light levels are deemed too low.

Overlays

The widget can display various overlays on top of the camera preview to assist with scanning.

Set the overlayShowGuide property of the widget to true to display a scanning guide.

Overlaying the scanning guide

The scanning guide helps users align barcodes correctly and turns from white to red when a barcode is detected.

Set the overlayShowGuide property of the widget to false to puts the widget into multi detect mode. Here, multiple barcodes will be detected in a single frame.

Bounding boxes can be displayed around detected barcodes by setting the widget's overlayShowRects property to true. The values of detected barcodes can be displayed by setting the widget's overlayShowLabels property to true.

Overlaying the barcode rectangles and labels

The color of the bounding boxes and labels can be controlled using the widget's overlayRectColor and overlayLabelColor properties. The font used for labels can be set using the widget's textFont property.

Barcode Detection

Callbacks are sent to the widget's script when barcodes are detected.

The barcodeDeteced message is sent when a new barcode is detected. barcodeClicked is sent when a detected barcode has been clicked by a user. barcodeRemoved is sent when a previously detected barcode is no longer visible.

The following code will print barcodes on detection when is single detect mode (i.e. when the guide is visible) and on click when in multi select mode.

on barcodeDetected pBarcodeA
    if the overlayShowGuide of widget "Android Barcode Scanner" then     
        printBarcode pBarcodeA
   end if
end barcodeDetected

on barcodeClicked pBarcodeA
    if not the overlayShowGuide of widget "Android Barcode Scanner" then     
        printBarcode pBarcodeA
   end if
end barcodeClicked

See the section "The Barcode Array" for details on the format of detected barcodes.

Snapshot Mode

A detected barcode can include an image of the barcode by setting the snapshotMode property of the widget. Setting the property to Never will mean that a snapshot of a detected barcode is never taken. Setting to Clicked will mean that a snapshot is only taken when a barcode is clicked. Setting to Detected will mean that snapshots will be taken of all detected barcodes.

Note that taking snapshot images slows down the performance of the widget.

The following code demonstrated how to set the widget to take snapshots on detection when in single selected mode (i.e. the guide is showing) and on click when in multi select mode.

if the overlayShowGuide of widget "Android Barcode Scanner" then
    set the snapshotMode of widget "Android Barcode Scanner" to "Detected"
else
    set the snapshotMode of widget "Android Barcode Scanner" to "Clicked"
end if

The Barcode Array

Detected barcodes are returned as an array with the following keys:

The following code is an example of printing a barcode to a field:

command printBarcode pBarcodeA, pFieldName
    put "Format:" && pBarcodeA["format"] & return after field pFieldName
    put "Value Format:" && pBarcodeA["value format"] & return after field pFieldName
    put "Raw Value:" && pBarcodeA["raw value"] & return after field pFieldName
    put "Display Value:" && pBarcodeA["display value"] & return after pFieldName
    if pBarcode["parsed value"] is an array then
        put printParsedBarcodeValue(pBarcode["parsed value"], pFieldName)
    end if
end printBarcode

The Parsed Barcode Value

A barcode's parsed value is represented as an array with keys for each attribute of the barcode. The keys of the array depend on the barcode format. For example a phone number will include the fields number and type.

The following code is an example of printing the parsed barcode value to a field.

function printParsedBarcodeValue pParsedBarcodeValueA, pFieldName
    put printArray(pParsedBarcodeValueA) & return after field pFieldName
end printParsedBarcodeValue

function printArray pArray, pIndent
    local tString
    repeat for each key tKey in pArray
        if pArray[tKey] is an array then
            put tKey & ":" & return & printArray(pArray[tKey], pIndent & "..") & return after tString
        else
            put tKey & ":" && pArray[tKey] & return after tString
        end if
    end repeat
    return tString
end printArray

The Barcode Snapshot

The barcodeLibraryDetect will also return a snapshot of any barcode detected. The snapshot is represented as an array, containing keys for the snapshot's pixels as well as its dimensions.

The following code displays the snapshot in a image:

command displayBarcodeSnapshot pSnapshotA, pImageName
    lock screen
    set the lockLoc of image pImageName to true
    set the width of of image pImageName to pSnapshotA["width"]
    set the height of of image pImageName to pSnapshotA["height"]
    set the imageData of image pImageName to pSnapshotA["pixels"]   
    unlock screen   
end displayBarcodeSnapshot