This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
plugins:barcode_reader [2015/03/26 10:51] octazid created |
plugins:barcode_reader [2015/03/26 18:53] (current) |
||
---|---|---|---|
Line 115: | Line 115: | ||
<code javascript> | <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(); | ||
+ | } | ||
</ | </ | ||