B4i Library [CLASS] ZXBarcodeScanner - similar to Android ZXing app - with full torch control

I have had the need to make the iBarcode library function more like the Android ZXing barcode app, principally proper control of the torch, return of code type and cosmetics.

To that end I have created a class for my own use which may be of some interest to others.

The attached zip file has the class wrapped in a super simple B4I example which presents as follows:

5.jpg

The class header documentation pretty well explains the intent:
B4X:
'************************************************************************************
'
'This class module wraps the iBarcode library
'
'   Features:
'
'   o Initial torch on/off state set by reading of ambient light brightness via:
'
'         https://www.b4x.com/android/forum/threads/a-solution-for-getting-ambient-light-brightness.63106/
'
'   o User can subsequently toggle torch on/off by tapping screen - there ia an
'     accompanying prompt on the screen
'
'   o A cosmetic overlay to resemble Android ZXing barcode scanner app (full screen
'     mode, grayed boundaries and red "scan line")
'
'   o Cosmetic overlay and torch prompt visually encourage user to use iPhone in
'     landscape mode which experience to date suggests makes scanner read faster
'     - works regardless of Main procedure #iPhoneOrientations/#iPadOrientations
'     settings
'
'   o An audio prompt to tutor user in how to use iPhone as a scanner
'
'   o A successful scan triggers an event in parent module - which can then read scan
'     code string, scan code type and ambient light brightness reading - scan code
'     type extracted via:
'
'         https://www.b4x.com/android/forum/threads/ibarcode-lib.49567/#post-317829
'
'   Requirements:
'
'   o B4I libraries:
'
'         iBarcode (version 1.20 or later)
'         iPhone (version 1.60 or later) - for TTS
'
'   o To allow control of status bar, following must be added to host app:
'
'         #Region  Project Attributes
'
'         'Following attribute added to hide/show Status Bar, see:
'         'https://www.b4x.com/android/forum/threads/full-screen-apps.47866/#content
'         #PlistExtra: <key>UIViewControllerBasedStatusBarAppearance</key><false/>
'
'************************************************************************************

And the documentation of the Initialization method of the class describes the parameters that need to be passed:
B4X:
'************************************************************************************
'
'This procedure gets control when Initialize method is called
'
'Input parameters are:
'
'       ZXBC_Parent = pointer to module creating instance of this class
'       ZXBC_Event_Name = event name stub
'       ZXBC_App = pointer to Application
'       ZXBC_NavControl = pointer to NavigationController
'       ZXBC_Brightness_Min = brightness level below which torch is initially turned on
'       ZXBC_Overlay_Border_Factor = grayed border width as fraction of screen width/height
'       ZXBC_Overlay_Alpha = grayed border alpha (0 = transparent, 1 = solid)
'       ZXBC_Types = string of acceptable barcode types (e.g. "type_128, type_qr" - not case sensitive, "" for all)
'       ZXBC_User_Handedness = user handedness ("L" = left handed, "R" = right handed)
'       ZXBC_Audio = text of audio prompt
'
'Returns:
'
'       ZXBC_Parent can read .BC_Scan_String, .BC_Scan_Type and .BC_Brightness after
'       it receives (ZXBC_Event_Name)_Scan event
'
'Notes on this procedure:
'
'       o Initializes and launches barcode scanner
'
'************************************************************************************

This is my first major foray into B4I classes so I would be interested in any feedback about how to do things more elegantly.

The torch stuff relies on:

https://www.b4x.com/android/forum/threads/a-solution-for-getting-ambient-light-brightness.63106/

I am still interested in getting an Objective C expert look over this effort.

Hope this is of benefit to someone...

=================================================

UPDATE - see next post for better class.
 

Attachments

  • ZXBarcodeScanner.zip
    7.1 KB · Views: 71
Last edited:

JackKirk

Well-Known Member
Licensed User
A significantly improved class is attached.

Improvements include:
  • Simpler and more elegant - now uses existing page as base so don't have to do any page rebuilding when finished scanning.
  • Screen swipe to cancel scanning.
  • Full error handling with an Error event, including check for host app camera permission.
  • Improved documentation.
As before, the attached zip file has the class wrapped in a super simple B4I example which presents as follows:

5.jpg

(Note small addition of "Swipe screen to cancel" prompt).

The class header documentation hopefully explains the intent a bit better:
B4X:
'************************************************************************************
'
'This class module wraps the iBarcode library
'
'Features:
'
'   o Initial torch on/off state set by reading of ambient light brightness
'
'   o User can subsequently toggle torch on/off by tapping screen - there ia an
'     accompanying dynamic prompt on the screen
'
'   o A cosmetic overlay to resemble Android ZXing barcode scanner app (full screen
'     mode, grayed boundaries and red "scan line")
'
'   o Cosmetic overlay and torch prompt visually encourage user to use iPhone in
'     landscape mode which experience to date suggests makes scanner read faster
'     - works regardless of Main procedure #iPhoneOrientations/#iPadOrientations
'     settings
'
'   o An audio prompt to tutor user in how to use iPhone as a scanner
'
'   o User can cancel scan by swiping screen
'
'   o Successful scan returns scan code string, scan code type and ambient light
'     brightness
'
'   o Comprehensive error management
'
'Methods:
'
'   Initialize
'
'   Flush
'
'   Resize
'
'Events:
'
'   Scan
'
'       Indicates there has been a successful scan, parent module can then read scan
'       code string, scan code type and ambient light brightness
'
'   Error
'
'       Indicates there has been an error, parent module can then read error message
'
'Properties:
'
'   Scan_String - latest scan code string
'
'   Scan_Type - latest scan code type
'
'   Brightness - latest ambient light brightness
'
'   Error_Msg - last error message
'
'Requirements:
'
'   o B4I libraries:
'
'         iBarcode (version 1.20 or later)
'         iMedia (version 1.21 or later) - for LLCamera.AuthorizationDenied
'         iPhone (version 1.60 or later) - for TTS
'
'   o To allow control of status bar, following must be added to host app:
'
'         #Region  Project Attributes
'
'         'Following attribute added to hide/show Status Bar, see:
'         'https://www.b4x.com/android/forum/threads/full-screen-apps.47866/#content
'         #PlistExtra:<key>UIViewControllerBasedStatusBarAppearance</key><false/>
'
'Update history:
'
'   6 Feb 16 - 1.0
'   8 Feb 16 - 2.0
'       o Simpler and more elegant - now uses existing page as base so don't have to
'         do any page rebuilding when finished scanning
'       o Screen swipe to cancel scanning
'       o Full error handling with an Error event, including check for host app
'         camera permission
'       o Improved documentation
'   8 Feb 16 - 2.1
'       o Fix to reduce sensitivity to "swipe to cancel"
'   11 Feb 16 - 2.2
'       o Fix to correctly handle all possible initial states of host app Navigation
'         and Tool bars
'   15 Feb 16 - 2.3
'       o Minor improvement to cosmetics
'   18 Feb 16 - 2.4
'       o Made all properties read only
'       o Significant improvements to naming and in-line documentation of methods
'         and properties
'   21 Feb 16 - 2.4
'       o Removed user handedness parameter and now get orientation from monitoring
'         phone attitude roll
'   24 Feb 16 - 2.6
'       o Fixed bug associated with camera authorization denied check
'       o Added XTimer to improve Timer accuracy
'   3 Mar 16 - 2.7
'       o Added backdrops to torch/cancel prompts
'
'************************************************************************************

And the documentation of the Initialization method of the class, describing parameters that need to be passed, has had a minor update:
B4X:
'************************************************************************************
'
'This procedure gets control when Initialize method is called by parent module
'
'Input parameters are:
'
'       ZXBC_Parent = pointer to module creating instance of this class
'       ZXBC_Page = pointer to host page
'       ZXBC_Event_Name = event name stub
'       ZXBC_App = pointer to Application
'       ZXBC_NavControl = pointer to NavigationController
'       ZXBC_Brightness_Min = brightness level below which torch is initially turned on
'       ZXBC_Overlay_Border_Factor = grayed border width as fraction of screen width/height
'       ZXBC_Overlay_Alpha = grayed border alpha (0 = transparent, 1 = solid)
'       ZXBC_Types = string of acceptable barcode types (e.g. "type_128, type_qr" - not case sensitive, "" for all)
'       ZXBC_Audio = text of audio prompt
'
'Returns:
'
'       None
'
'Notes on this procedure:
'
'       o Initializes and launches ZXBarcodeScanner
'
'************************************************************************************

As before the torch stuff relies on:

https://www.b4x.com/android/forum/threads/a-solution-for-getting-ambient-light-brightness.63106/

and I am still interested in getting an Objective C expert to look over it - are there any out there?

Any and all feedback welcome...

=================================================

UPDATES - the zip in this post has been replaced on several occasions:

8 Feb 16 - exhibits reduced sensitivity to "swipe to cancel".

11 Feb 16 - correctly handles all possible initial states of host app Navigation and Tool bars.

15 Feb 16 - some minor improvements to cosmetics.

18 Feb 16 - all properties are read only and significant improvements to naming and in-line documentation of methods and properties

21 Feb 16 - removed user handedness parameter and now get orientation from monitoring phone attitude roll

24 Feb 16 - fixed bug associated with camera authorization denied check and added XTimer to improve Timer accuracy

3 Mar 16 - added backdrops to torch/cancel prompts
 

Attachments

  • ZXBarcodeScanner2.zip
    11.8 KB · Views: 82
Last edited:
Top