B4A Class ZXBarcodeScanner - similar to native Android ZXing app - with full correct torch control

In recent times I have become quite a student of smartphone barcode scanners - Android and iPhone.

One of the major problems I have encountered is that there seems to be limited appreciation of just how important correct management of the torch is - particularly if the scanning is to be done in many different environments with all sorts of (generally poor) light conditions by users who are not barcode scanning experts.

Case in point: the native Android ZXing app offers a "Use front light" setting with an "Automatic" option - only problem is it monitors the light conditions via the light meter on the screen side of the phone - i.e. on the opposite side of the phone to the camera being used to do the barcode scanning. If the primary light source is behind the user as they try to scan then the phone itself can heavily shadow the barcode - but the torch doesn't come on because the light sensor is seeing sufficient light.

You'd almost think Niccolò Machiavelli did the product design.

Fortunately I found Johan Schoeman - I learnt a long time ago that with software development everyone is standing on someone else's shoulders - I am unapologetically standing on his and they have proved brilliant.

Johan recently posted a new wrapping of the ZXing engine:

https://www.b4x.com/android/forum/t...de-scanner-that-is-100-embedded-in-b4a.63764/

I managed to start a dialog with him on the subject of torch control - the result (at post #39) of the above thread is that he has developed a library that allows you to manage the torch via real time brightness readings taken through the camera as it is previewing the barcode to be scanned. This I suspect is similar to the way the standard camera app manages its flash when you set it to auto.

As far as I am aware, this makes this library totally unique - certainly among the alternatives in this forum.

I have gone off and wrapped Johan's library, adding a few features and tailoring some of Johan's library features to my app's requirements.

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

4.png


The class header documentation explains features, requirements etc:
B4X:
'************************************************************************************
'
'This class module wraps Johan Schoeman's ZxingBarcodeScanner library, see:
'https://www.b4x.com/android/forum/threads/1d-and-2d-barcode-scanner-with-zxing-another-barcode-scanner-that-is-100-embedded-in-b4a.63764/page-2#post-410694
'
'Features:
'
'   o (Courtesy of ZxingBarcodeScanner library) real-time monitoring of brightness
'     via camera on torch side of phone - NOT monitoring of lux via light sensor on
'     screen side of phone - has significant implications when trying to scan
'     barcodes in poor lighting conditions where (for example) barcode might be
'     heavily shadowed by phone
'
'   o Three user definable torch modes: "auto" (torch on whenever real-time
'     brightness drops below a user definable threshold), "manual", "semiauto"
'     ("auto" until user overides it for first time by tapping screen - then
'     "manual")
'
'   o User definable initial torch state
'
'   o User can toggle torch on/off by tapping screen
'
'   o User can cancel scanner by swiping screen
'
'   o A cosmetic overlay to resemble Android ZXing barcode scanner app (full screen
'     mode, grayed boundaries and red "scan line")
'
'   o Cosmetic overlay includes torch on/off and cancel prompts that adjust for
'     screen orientation
'
'   o Manages situation where change of phone roll/pitch orientation does not trigger
'     a Pause/Resume of parent activity - if this management does not occur barcode
'     preview appears upside down or back to front
'
'   o Works under any host activity screen settings
'
'   o Successful scan returns scan code string and scan code type
'
'   o Current brightness, torch mode and torch state can be read at any time
'
'   o Error management
'
'   o Optional fix for (what appears to be) a device dependant bug in
'     ZxingBarcodeScanner library
'
'Methods:
'
'   Initialize
'
'   Flush
'
'Events:
'
'   Scan
'
'       Indicates there has been a successful scan, parent module can then read scan
'       code string and scan code type
'
'   Error
'
'       Indicates there has been an error, parent module can then read error message
'
'Properties:
'
'   Scan_String - last scan code string
'
'   Scan_Type - last scan code type
'
'   Brightness - current brightness
'
'   Torch_Mode - current torch mode
'
'   Torch_State - current torch state
'
'   Error_Msg - last error message
'
'Requirements:
'
'   o B4A libraries:
'
'         JavaObject (version 2.05 or later) - for orientation monitoring
'         ZxingBarcodeScanner (version 1.00 or later) - see:
'             https://www.b4x.com/android/forum/threads/1d-and-2d-barcode-scanner-with-zxing-another-barcode-scanner-that-is-100-embedded-in-b4a.63764/page-2#post-410694
'             Notes:
'                 (1) ZxingBarcodeScanner library must be installed from post #39 or
'                     later (use ZxingBarcodeScannerLogColorStringKilled.zip or
'                     later)
'                 (2) As per post #1 of Johan's thread above android-support.v4.jar
'                     and core-3.2.1.jar must also be installed in additional library
'                     folder - they are zipped together and can be downloaded from
'                     Johan's Dropbox account via:
'                     https://www.dropbox.com/s/h9ts6cjfoo5et6h/core-3.2.1.zip?dl=0
'
'Update history:
'
'   22 Mar 16 - 1.0
'
'************************************************************************************
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 by parent module
'
'Input parameters are:
'
'       ZXBC_Parent = pointer to module creating instance of this class
'       ZXBC_Activity = pointer to host activity
'       ZXBC_Event_Name = event name stub
'       ZXBC_Brightness_Min = brightness level below which torch is turned on (0 =
'                             total darkness, 255 = bright as...)
'       ZXBC_Overlay_Border_Factor = grayed border width as fraction of screen
'                                    width/height
'       ZXBC_Overlay_Alpha = grayed border alpha (0 = transparent, 1 = solid)
'       ZXBC_Focus_Mode = camera focus mode ("auto", "continuous_picture",
'                         "continuous_video", "infinity" or "macro")
'       ZXBC_Torch_Mode = torch mode ("auto" (will turn on whenever brightness is <
'                         ZXBC_Brightness_Min), "manual", "semiauto" ("auto" until
'                         user overides it for first time - then "manual"))
'       ZXBC_Torch_State = flag to indicate initial torch state, True = on, False =
'                          off
'       ZXBC_Torch_Bug_Fix_Interval = torch bug fix interval (millisecs), 0 if fix
'                                     not to be applied, 500 has worked on a
'                                     Samsung S5 - bug presents as turning off torch
'                                     after a successful scan - important only if
'                                     doing multiple scans - could be device
'                                     dependent
'
'Returns:
'
'       -
'
'Notes on this procedure:
'
'       o Initializes and launches ZXBarcodeScanner
'
'************************************************************************************
In addition the example uses the class described here:

https://www.b4x.com/android/forum/threads/programmatically-set-screen-settings.64746/

to programmatically control the screen settings.

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

Some suggestions of things to look closely at:
  • The various barcode settings described in Main.Activity_Create, particularly ZXBC_Brightness_Min.
  • The screen setting alternatives available in Main.Activity_Click, particularly "userlandscape".
  • The alternate "bulk" scanning mode in Main.Event_ZXBC_Scan.
  • Possible alternative ways to control the torch by real-time brightness stream, as outlined in ZXBarcodeScanner.Event_BC_Scanner_Brightness_Changed.
  • Do your own cosmetics by changing the code of ZXBarcode_Scanner.Event_BC_Complete_Initialization_Timer_Tick and /or ZXBarcode_Scanner.BC_Torch_OnOff.
=================================================

IMPORTANT INSTALLATION NOTES

To avoid any confusion, please note that this class and example should be used in conjunction with Johan's library contained in:

ZxingBarcodeScannerLogColorStringKilled.zip

at:

https://www.b4x.com/android/forum/t...-100-embedded-in-b4a.63764/page-2#post-410694

In addition, as noted in Johan's first post in the above thread, the following files must also be installed in your B4A additional library folder:

android-support.v4.jar
core-3.2.1.jar

These files are available in a single zip file from Johan's DropBox account at:

https://www.dropbox.com/s/h9ts6cjfoo5et6h/core-3.2.1.zip?dl=0

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

Any and all feedback welcome...
 

Attachments

  • ZXBarcodeScannerB4A.zip
    18 KB · Views: 486
Last edited:

JackKirk

Well-Known Member
Licensed User
Longtime User
Well done Jack. Something special that you have created here! Torch control is all your idea ;)
Johan,

As I have said elsewhere, ideas are often much easier to come by than implementation.
 
Top