This shows you the differences between two versions of the page.
plugins:barcode_reader [2015/03/26 10:53] octazid [Sample App] |
plugins:barcode_reader [2015/03/26 18:53] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== BarcodeReader-Plugin ====== | ||
- | |||
- | :!:\\ | ||
- | //(You must buy and install the plugin over the plugin section within DroidScript)// | ||
- | |||
- | //Taken from the official DroidScript documentation// | ||
- | |||
- | 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, | ||
- | < | ||
- | |||
- | Then you can create an instance of the plugin object when you need it like this: | ||
- | < | ||
- | |||
- | Reading barcodes is a simple case of calling the **Decode** method, passing either an Image control or CameraView. | ||
- | < | ||
- | |||
- | Note that when decoding from a CameraView, the **" | ||
- | |||
- | 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 ==== | ||
- | <code javascript> | ||
- | app.LoadPlugin( " | ||
- | |||
- | function OnStart() | ||
- | { | ||
- | app.SetOrientation( " | ||
- | |||
- | lay = app.CreateLayout( " | ||
- | |||
- | reader = app.CreateObject( " | ||
- | |||
- | 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( " | ||
- | DecodeFromCamera(); | ||
- | } | ||
- | |||
- | function DecodeFromCamera() | ||
- | { | ||
- | var result = reader.Decode( cam ); | ||
- | |||
- | if( result != null ) | ||
- | { | ||
- | app.ShowPopup( result.barcodeType + ": " + result.content ); | ||
- | } | ||
- | |||
- | setTimeout( DecodeFromCamera, | ||
- | } | ||
- | </ | ||
- | |||
- | If a barcode is successfully decoded, the Decode method will return a result object. The result object contains the following properties: | ||
- | |||
- | ^Property^ Description^ | ||
- | |barcodeType | ||
- | |contentType | ||
- | |content | ||
- | |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 **Supported 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: | ||
- | |||
- | < | ||
- | |||
- | ==== Supported Barcodes ==== | ||
- | |||
- | The following table lists the supported barcode types, represented by the **barcodeType** property of the result object returned by the Decode method: | ||
- | ^Barcode Type^ Description^ | ||
- | |CODABAR |[[http:// | ||
- | |CODE 128 |[[http:// | ||
- | |CODE 39 |[[http:// | ||
- | |CODE 93 |[[http:// | ||
- | |DATA MATRIX |[[http:// | ||
- | |EAN 13 |European Article Number (Aka International Article Number). [[http:// | ||
- | |EAN 8 |European Article Number. [[http:// | ||
- | |ITF |Interleaved 2 of 5. [[http:// | ||
- | |QR CODE |Quick Response Code. [[http:// | ||
- | |RSS |Reduced Space Symbology (Aka GS1 DataBar). [[http:// | ||
- | |UPC |Universal Product Code. [[http:// | ||
- | |||
- | ==== 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 [[https:// | ||
- | |||
- | ===== Sample App ===== | ||
- | |||
- | Below is an sample BarcodeReader App, which includes the option to use the flash while scanning for barcodes: | ||
- | |||
- | <code javascript> | ||
- | app.LoadPlugin( " | ||
- | |||
- | //Called when application is started. | ||
- | function OnStart() | ||
- | { | ||
- | //Fix orientation to landscape. | ||
- | app.SetOrientation( " | ||
- | |||
- | //Stop screen turning off. | ||
- | app.PreventScreenLock( true ); | ||
- | |||
- | // | ||
- | lay = app.CreateLayout( " | ||
- | |||
- | // | ||
- | reader = app.CreateObject( " | ||
- | |||
- | // | ||
- | // | ||
- | 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() | ||
- | { | ||
- | // | ||
- | // | ||
- | //we can show a framed area. | ||
- | img = app.CreateImage( null, 1.0, 1.0 ); | ||
- | img.SetAlpha( 0.5 ); | ||
- | lay.AddChild( img ); | ||
- | img.SetPaintColor( "# | ||
- | img.SetPaintStyle( " | ||
- | img.SetLineWidth( 2.5 ); | ||
- | img.DrawRectangle( 0.2, 0.2, 0.8, 0.8 ); | ||
- | |||
- | // | ||
- | //and flash checkbox. | ||
- | overlay = app.CreateLayout( " | ||
- | lay.AddChild(overlay); | ||
- | |||
- | // | ||
- | txt = app.CreateText( " | ||
- | txt.SetMargins( 0,0.05,0,0 ); | ||
- | txt.SetPadding( 0.01, | ||
- | txt.SetTextColor( "# | ||
- | txt.SetBackColor( "# | ||
- | txt.SetTextSize( 18 ); | ||
- | overlay.AddChild( txt ); | ||
- | |||
- | // | ||
- | chkFlash = app.CreateCheckBox( "Use Flash" ); | ||
- | chkFlash.SetMargins( 0,0.75,0,0 ); | ||
- | chkFlash.SetPadding( 0.01, | ||
- | chkFlash.SetTextColor( "# | ||
- | chkFlash.SetBackColor( "# | ||
- | chkFlash.SetTextSize( 18 ); | ||
- | chkFlash.SetOnTouch( chkFlash_OnTouch ); | ||
- | overlay.AddChild( chkFlash ); | ||
- | |||
- | //Start preview. | ||
- | cam.StartPreview(); | ||
- | |||
- | //Macro focus mode works best for | ||
- | // | ||
- | cam.SetFocusMode( " | ||
- | |||
- | //Start decoding the camera preview. | ||
- | DecodeFromCamera(); | ||
- | } | ||
- | |||
- | //Handle Use Flash checkbox touch | ||
- | function chkFlash_OnTouch( value ) | ||
- | { | ||
- | cam.SetFlash( value ); | ||
- | } | ||
- | |||
- | function DecodeFromCamera() | ||
- | { | ||
- | // | ||
- | // | ||
- | var result = reader.Decode( cam ); | ||
- | |||
- | //If result is not null, a barcode | ||
- | //was detected. | ||
- | if( result != null ) | ||
- | { | ||
- | // | ||
- | app.Vibrate( " | ||
- | ShowResult( result ); | ||
- | } | ||
- | else | ||
- | { | ||
- | // | ||
- | setTimeout( DecodeFromCamera, | ||
- | } | ||
- | } | ||
- | |||
- | function ShowResult( result ) | ||
- | { | ||
- | // | ||
- | resultDlg = app.CreateDialog( result.barcodeType, | ||
- | |||
- | // | ||
- | layDlg = app.CreateLayout( " | ||
- | layDlg.SetPadding( 0.02, 0, 0.02, 0.02 ); | ||
- | resultDlg.AddLayout( layDlg ); | ||
- | |||
- | // | ||
- | txtDlg = app.CreateText( result.contentType ); | ||
- | txtDlg.SetTextSize( 22 ); | ||
- | txtDlg.SetTextColor( "# | ||
- | layDlg.AddChild( txtDlg ); | ||
- | |||
- | // | ||
- | scrDlg = app.CreateScroller( 0.4, 0.45 ); | ||
- | scrDlg.SetMargins( 0, 0.01, 0, 0 ); | ||
- | layDlg.AddChild( scrDlg ); | ||
- | |||
- | // | ||
- | txtDlg = app.CreateText( result.content, | ||
- | txtDlg.SetTextSize( 18 ); | ||
- | scrDlg.AddChild( txtDlg ); | ||
- | |||
- | // | ||
- | btnDlg = app.CreateButton( " | ||
- | 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(); | ||
- | } | ||
- | </ | ||