The BarcodeReader plugin can be used to add barcode reading capabilities to your DroidScript apps. One and two dimensional barcodes (including QR Codes) can be read from a CameraView or Image control.
In order to use BarcodeReader, you must first load the plugin at the top of your script
using the LoadPlugin method like this:
app.LoadPlugin( "BarcodeReader" );
Then you can create an instance of the plugin object when you need it like this:
plg = app.CreateObject( "BarcodeReader" );
Reading barcodes is a simple case of calling the Decode method, passing either an Image control or CameraView.
reader.Decode( cam );
Note that when decoding from a CameraView, the "UseYUV" option is required when creating the CameraView.
The following example periodically attempts to decode a barcode from the camera and displays the decoded content when it finds one:
Example - Reading from a CameraView
app.LoadPlugin( "BarcodeReader" );
function OnStart()
{
app.SetOrientation( "Landscape" );
lay = app.CreateLayout( "Linear", "FillXY" );
reader = app.CreateObject( "BarcodeReader" );
cam = app.CreateCameraView( 0.8, 1, "VGA, UseYUV" );
cam.SetOnReady( cam_OnReady );
lay.AddChild( cam );
app.AddLayout( lay );
}
function cam_OnReady()
{
cam.StartPreview();
cam.SetFocusMode( "macro" );
DecodeFromCamera();
}
function DecodeFromCamera()
{
var result = reader.Decode( cam );
if( result != null )
{
app.ShowPopup( result.barcodeType + ": " + result.content );
}
setTimeout( DecodeFromCamera, 200 );
}
If a barcode is successfully decoded, the Decode method will return a result object. The result object contains the following properties:
Property |
Description |
barcodeType |
The type of the barcode, e.g. QR Code, Data Matrix, EAN 13, etc. See Supported Barcodes for the complete list. |
contentType |
The type of the barcode contents. The content can represent product codes, address book contacts, telephone numbers, etc. See Content Types for the complete list. |
content |
The human readable representation of the barcode contents. |
raw |
Contains the barcode content in it's original form. In some cases this will be the same as the content property, but if the contentType is ADDRESSBOOK for example, the raw property will contain the vCard or meCard contact data. |
By default, when Decode is called, the plugin will attempt to detect all types of barcodes defined in the Suppoted Barcodes section. The SetBarcodeTypes method allows you to specify which barcode types you are interested in. The required barcode types can be passed to SetBarcodeTypes as a comma separated string. For example, to decode only QR Code and Data Matrix types:
reader.SetBarcodeTypes( "QR CODE,DATA MATRIX" );
Supported Barcodes
The following table lists the supported barcode types, represented by the barcodeType property of the result object returned by the Decode method:
Content Types
The following table lists the possible values for the contentType property of the result object returned by the Decode method:
Content Type |
Description |
ADDRESSBOOK |
Contact Information |
CALENDAR |
Calendar Event |
EMAIL ADDRESS |
Email Address |
GEO |
Geo Location |
ISBN |
International Standard Book Number |
PRODUCT |
Product Code |
SMS |
SMS Message |
TEL |
Telephone Number |
TEXT |
Plain Text |
URI |
URI |
VIN |
Vehicle Identification Number |
WIFI |
Wifi Network Information |
License
The BarcodeReader plugin uses the ZXing project licensed under the Apache 2.0 license. Please include the link to the project in any DroidScript apps built using this plugin - this could be placed in your apps About box for example.
Sample App
Below is an extended sample BarcodeReader App, which includes the option to use the flash while scanning for barcodes:
Sample App
app.LoadPlugin( "BarcodeReader" );
//Called when application is started.
function OnStart()
{
//Fix orientation to landscape.
app.SetOrientation( "Landscape" );
//Stop screen turning off.
app.PreventScreenLock( true );
//Create a layout to fill the screen.
lay = app.CreateLayout( "Absolute", "FillXY" );
//Create the barcode reader.
reader = app.CreateObject( "BarcodeReader" );
//Create camera view control
//(UseYUV is required for use with BarcodeReader)
cam = app.CreateCameraView( 0.8, 1.0, "VGA, UseYUV" );
cam.SetPosition( 0.1, 0 );
cam.SetOnReady( cam_OnReady );
lay.AddChild( cam );
//Add main layout to app.
app.AddLayout( lay );
}
//Called when camera is ready.
function cam_OnReady()
{
//Create an image control over the top of the
//camera view with transparency (alpha) so
//we can show a framed area.
img = app.CreateImage( null, 1.0, 1.0 );
img.SetAlpha( 0.5 );
lay.AddChild( img );
img.SetPaintColor( "#ff0000" );
img.SetPaintStyle( "Line" );
img.SetLineWidth( 2.5 );
img.DrawRectangle( 0.2, 0.2, 0.8, 0.8 );
//Create a layout for instruction text
//and flash checkbox.
overlay = app.CreateLayout( "Linear", "FillXY" );
lay.AddChild(overlay);
//Create the instruction text.
txt = app.CreateText( "Position a barcode in the frame to read it" );
txt.SetMargins( 0,0.05,0,0 );
txt.SetPadding( 0.01,0,0.01,0 );
txt.SetTextColor( "#ffffff" );
txt.SetBackColor( "#44000000" );
txt.SetTextSize( 18 );
overlay.AddChild( txt );
//Create Use Flash check box.
chkFlash = app.CreateCheckBox( "Use Flash" );
chkFlash.SetMargins( 0,0.75,0,0 );
chkFlash.SetPadding( 0.01,0,0.01,0 );
chkFlash.SetTextColor( "#ffffff" );
chkFlash.SetBackColor( "#44000000" );
chkFlash.SetTextSize( 18 );
chkFlash.SetOnTouch( chkFlash_OnTouch );
overlay.AddChild( chkFlash );
//Start preview.
cam.StartPreview();
//Macro focus mode works best for
//reading barcodes.
cam.SetFocusMode( "macro" );
//Start decoding the camera preview.
DecodeFromCamera();
}
//Handle Use Flash checkbox touch
function chkFlash_OnTouch( value )
{
cam.SetFlash( value );
}
function DecodeFromCamera()
{
//Attempt to detect a barcode in the
//camera preview.
var result = reader.Decode( cam );
//If result is not null, a barcode
//was detected.
if( result != null )
{
//Vibrate and show result dialog.
app.Vibrate( "0,100,30,100,50,300" );
ShowResult( result );
}
else
{
//Decode again in 200 milliseconds.
setTimeout( DecodeFromCamera, 200 );
}
}
function ShowResult( result )
{
//Create dialog window.
resultDlg = app.CreateDialog( result.barcodeType, "NoCancel" );
//Create a layout for dialog.
layDlg = app.CreateLayout( "Linear", "Vertical, FillXY" );
layDlg.SetPadding( 0.02, 0, 0.02, 0.02 );
resultDlg.AddLayout( layDlg );
//Create text for the content type name.
txtDlg = app.CreateText( result.contentType );
txtDlg.SetTextSize( 22 );
txtDlg.SetTextColor( "#0099CC" );
layDlg.AddChild( txtDlg );
//Create a scroller for the content.
scrDlg = app.CreateScroller( 0.4, 0.45 );
scrDlg.SetMargins( 0, 0.01, 0, 0 );
layDlg.AddChild( scrDlg );
//Create text for the content.
txtDlg = app.CreateText( result.content, 0.4, -1, "Multiline" );
txtDlg.SetTextSize( 18 );
scrDlg.AddChild( txtDlg );
//Create an Ok button to dismiss the dialog.
btnDlg = app.CreateButton( "Ok" );
btnDlg.SetOnTouch( resultDlg_OnOk );
layDlg.AddChild( btnDlg );
//Show dialog.
resultDlg.Show();
}
//Called when the result dialog Ok button is pressed
function resultDlg_OnOk()
{
resultDlg.Dismiss();
//Start decoding again.
DecodeFromCamera();
}