B4A Library ZXingLib by icefairy333 - modified by Johan Schoeman (Scan QR Codes and other 1D/2D Barcodes)

ScannerActive.png


See the attached project (with library files in the /files folder of the project). This is based on the original work of @icefairy333. I have modified @icefairy333's library so that it can do the following:

1. Set the color of the border of the framing rectangle from B4A code
2. Set the color of the laser from B4A code
3. Set the color of the mask around the framing rectangle from B4A code
4. Set the color of the result points from B4A code
5. Set the color of the final panel being displayed before control is being returned to the B4A project from B4A code
6. Ability to switch on/off the torch when making use of the back camera scanner by means of the volume up/down buttons. Use the volume up/down buttons to toggle the torch on/off when the scanner is active.

a. It retains the Portrait /Landscape option as per @icefairy333 's version 1.5 posted in post #1 of his original post.
b. It retains use of the front / back camera (not sure if it supports all types of devices)

See post #5 for changes that might be required to the Manifest of the project.

Edit: Posting JohanIceFairyZxingWidthMod.zip that will allow you to change the width / height of the ViewFinder. New library files are in the /files folder of the attached project. Replace the old lib files with the new ones.

Edit: Posting JohanIceFairyZxingWidthModAndTextMod.zip that will allow you also add text to the mask around the viewfinder of the scanner.

Edit: Posting JohanIceFairyZxingWidthTextBitmap.zip that will allow you to also add a bitmap to the mask around the viewfinder of the scanner.

Edit: Posting JohanIcefairyZxingWidthTextBitmapTouchTorch.zip that retains all previously added functionality but you can now also switch on/off the flash when using the back camera by either using the volume up/down buttons or just touch the screen while the scanner is active.

Edit: attached file src.zip contains the java source code. It can be compiled into a B4A library with Simple Library Compiler (SLC) as is, provided you set up the directory structure correctly and you set up SLC correctly. See elsewhere in this thread how to do it.

Edit: Attached JohanIcefairyZxingTimeout.zip with all the previously added functionality but adding an option to specify a timeout duration. Default timeout duration is 15 seconds (should you not specify any timeout duration). Also posting src_2.zip with the amended java source code

eg of Button1_Click for project in JohanIcefairyZxingTimeout.zip
B4X:
Sub Button1_Click
  zx.isportrait = True
  zx.useFrontCam = False

  'set the timeoutDuration to a very high value (such as 2000000000) if you dont want it to time out
  '2000000000 = 63 years+
  zx.timeoutDuration = 30

  'change these factors between 0 and 1 to change the size of the viewfinder rectangle
  'the library will limit the minimum size to 240 x 240 pixels and the maximum to (screen width) x   (screen height) pixels
  zx.theViewFinderXfactor = 0.7
  zx.theViewFinderYfactor = 0.5

  zx.theFrameColor = Colors.Blue
  zx.theLaserColor = Colors.Yellow
  zx.theMaskColor = Colors.argb(95, 0, 0, 255)
  zx.theResultColor = Colors.Green
  zx.theResultPointColor = Colors.Red

  'set the prompt messages
  zx.theTopPromptMessage = "This was done......"
  zx.theTopPromptTextSize = 5%y'text size in pixels
  zx.topPromptColor = Colors.Red
  zx.topPromptDistanceFromTop = 1%y'pixel distance from top

  zx.theBottomPromptMessage = "Just for fun......"
  zx.theBottomPromptTextSize = 5%y'text size in pixels
  zx.bottomPromptColor = Colors.Blue
  zx.bottomPromptDistanceFromBottom = 5%y'pixel distance from top

  'add a bitmap
  zx.theBitMap = bm
  zx.theBitMapLeft = 40%x
  zx.theBitMapTop = 10%y
  zx.theBitMapWidth = 20%x
  zx.theBitMapHeight = 20%x

  zx.BeginScan("myzx")
End Sub

Enjoy!
 

Attachments

  • JohanIcefairyZxing.zip
    416.8 KB · Views: 1,680
  • JohanIcefairyZxingWidthMod.zip
    417.5 KB · Views: 1,122
  • JohanIcefairyZxingWidthModAndTextMod.zip
    418.4 KB · Views: 1,056
  • JohanIcefairyZxingWidthTextBitmap.zip
    421 KB · Views: 996
  • JohanIcefairyZxingWidthTextBitmapTouchTorch.zip
    421.5 KB · Views: 1,101
  • JohanIcefairyZxingTimeout.zip
    421.5 KB · Views: 1,124
  • b4aZXINGusingVer1.05.zip
    12.2 KB · Views: 834
  • JhsIceZxing1Ver1.05.zip
    444.1 KB · Views: 855
  • JhsIceZxing1Ver1.06.zip
    444.1 KB · Views: 1,441
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
I've found what I think may be a bug with this library. I'm using a Nexus 7 tablet which doesn't have a torch. When scanning a barcode, if I tap anywhere on the screen the application fails. The error message seems indicate that the error due to myTorchParametersOn. Is there anyway to disable this feature for those phones or tables that don't have a torch?
Will amend the library and then post a new set of library files.
 

JTmartins

Active Member
Licensed User
Longtime User
I remember this feature !!! it's actually very usefull when scanning barcodes in dark areas like wharehouses...As long as the device has a torch..obviously. :)
 

Johan Schoeman

Expert
Licensed User
Longtime User
I've found what I think may be a bug with this library. I'm using a Nexus 7 tablet which doesn't have a torch. When scanning a barcode, if I tap anywhere on the screen the application fails. The error message seems indicate that the error due to myTorchParametersOn. Is there anyway to disable this feature for those phones or tables that don't have a torch?
Try with the new attached library files. I have tested it on a device with a flash/torch and on a device without a flash/torch - seems to be working.
 

Attachments

  • b4aZXING.zip
    11.9 KB · Views: 388
  • JhsIceZxing1NewLibFiles.zip
    438.7 KB · Views: 392

MarcoRome

Expert
Licensed User
Longtime User
Hi Johan. In a device i have this effect ( inverted image ). 4 works properly but about a device i see inverted image.

rovescio.jpg


The code is :
B4X:
'    ImageView1.Background = Null
   
    zx.isportrait = True
    zx.useFrontCam = False
    zx.timeoutDuration = 30
   

    zx.theViewFinderXfactor = 0.7      'portrait x schermo pieno = 1
    zx.theViewFinderYfactor = 0.5      'portrait x schermo pieno = 1
   
 
   
    zx.theFrameColor = Colors.Black
    zx.theLaserColor = Colors.Red
    zx.theMaskColor = Colors.Black 'Colors.argb(255, 0, 0, 255)
    zx.theResultColor = Colors.ARGB(255, 238,187,117)
    zx.theResultPointColor = Colors.Yellow
    zx.mustBeep = True
    zx.mustVibrate = True
   

   
    zx.BeginScan("myzx")

Any idea ?
Thank you
Marco
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi Johan. In a device i have this effect ( inverted image ). 4 works properly but about a device i see inverted image.

View attachment 42947

The code is :
B4X:
'    ImageView1.Background = Null
 
    zx.isportrait = True
    zx.useFrontCam = False
    zx.timeoutDuration = 30
 

    zx.theViewFinderXfactor = 0.7      'portrait x schermo pieno = 1
    zx.theViewFinderYfactor = 0.5      'portrait x schermo pieno = 1
 

 
    zx.theFrameColor = Colors.Black
    zx.theLaserColor = Colors.Red
    zx.theMaskColor = Colors.Black 'Colors.argb(255, 0, 0, 255)
    zx.theResultColor = Colors.ARGB(255, 238,187,117)
    zx.theResultPointColor = Colors.Yellow
    zx.mustBeep = True
    zx.mustVibrate = True
 

 
    zx.BeginScan("myzx")

Any idea ?
Thank you
Marco
Best is to keep the orientation of the device as is and stand on your head when you scan the barcodes....:)

Jokes aside - what happens when you use this same device in landscape mode (remember to also do the change in the Manifest to landscape). Is it then still upside down?
 

MarcoRome

Expert
Licensed User
Longtime User
Best is to keep the orientation of the device as is and stand on your head when you scan the barcodes....:)

Jokes aside - what happens when you use this same device in landscape mode (remember to also do the change in the Manifest to landscape). Is it then still upside down?

I tried to scan the QR Code, putting it on the client's head and I must say it works great :D:D:D.
Hi Johan and thank you for your response.
Yes in landscape havent this problem but in portrait yes. ( This only about Nexus 5 ).
 

Johan Schoeman

Expert
Licensed User
Longtime User
I tried to scan the QR Code, putting it on the client's head and I must say it works great :D:D:D.
Hi Johan and thank you for your response.
Yes in landscape haven't this problem but in portrait yes. ( This only about Nexus 5 ).
This is not a very tidy solution but you can use it to sort out the issue on the Nexus 5. I have added the ability to specify the display orientation.

B4X:
    zx.displayOrientation = 90     '90 is the default value that was set in the library. It works for most devices
                                   'although for eg the Nexus 5 will show an upside down preview with this setting.
                                   'I guess zx.displayOrientation = 270 will fix it for the Nexus 5 when used in portrait mode.


zx.displayOrientation = .... will only take effect if portrait mode is specified:
B4X:
    zx.isportrait = True

The default value in the library is 90 as it seems to work for most devices. If you don't specify zx.displayOrientation = .... in the code then 90 will automatically be applied. I guess for the Nexus 5 zx.displayOrientation = 270 will do the trick. Have not checked the forum if there is a way to determine the type of device that one is using. If there is then you can take care of other types of devices with a similar problem via B4A code.

Sample project and new lib files attached.

For what it might be worth, read the postings at this link
https://github.com/zxing/zxing/issues/484

There seems to be a solution incorporated in newer versions of ZXING (newer than what this library is based on) but it will take some serious open heart surgery to adapt the existing code - therefore the "quick fix"
 

Attachments

  • JhsIceZxing1LibFiles.zip
    438.8 KB · Views: 301
  • b4aZXING.zip
    12 KB · Views: 260

Johan Schoeman

Expert
Licensed User
Longtime User
This update is based on a request from @Carlos marin. Posting updated library files and a B4A project (for demonstration purposes) that will allow you to select between normal and color inverted codes to be scanned:
1. Normal = white background and black modules
2. Inverted = black background and white modules.

Use the spinner to make your selection for either normal or inverted codes to be scanned. Added this code to the spinner:
B4X:
Sub Spinner1_ItemClick (Position As Int, Value As Object)
   
    If Spinner1.SelectedIndex = 0 Then zx.scanInvertedCodes = False                     'scan normal colored codes (i.e background = white and modules = black)
    If Spinner1.SelectedIndex = 1 Then zx.scanInvertedCodes = True                      'scan inverted colored codes (i.e background = black and modules = white)
       
End Sub


Select from the spinner and click SCAN CODE:
start.png


NORMAL CODE (i.e white background and black modules)
Normal.png


INVERTED CODE (i.e black background and white modules)
Inverted.png
 

Attachments

  • b4aZXING.zip
    12.2 KB · Views: 262
  • JhsIceZxing1NewLibFiles.zip
    439 KB · Views: 315

Johan Schoeman

Expert
Licensed User
Longtime User
This is an update of the library as amended previously in post #90. It will now automatically scan normal and inverted colored codes. No need to select from the spinner for choosing between normal and inverted codes. I have removed the spinner from the attached B4A example.

1.png
 

Attachments

  • JhsIceZxing1NewLibFiles.zip
    439 KB · Views: 326
  • b4aZXING.zip
    11.9 KB · Views: 297

MarcoRome

Expert
Licensed User
Longtime User
Hi great Johan. When i try ( last library #91 but also more "old" library ) with Samsung S7 Edge i have this result in Log Panel :

** Activity (main) Pause, UserClosed = false **
** Activity (act_barcode) Create, isFirst = true **
** Activity (act_barcode) Resume **
** Activity (act_barcode) Pause, UserClosed = false **
Portrait
Screen 1440 2560
EAN_13:9788897192008###myzx_result
sending message to waiting queue (OnActivityResult)
running waiting messages (1)
userCancelled true
** Activity (act_barcode) Resume **
** Activity (act_barcode) Pause, UserClosed = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **

but even xxx_result dont work.
B4X:
Sub myzx_result(atype As String, Value As String, image As Bitmap)
       
   
    Log("type:" & atype &  "Value:" & Value)
   

End Sub

Any idea ?? :confused:
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi great Johan. When i try ( last library #91 but also more "old" library ) with Samsung S7 Edge i have this result in Log Panel :



but even xxx_result dont work.
B4X:
Sub myzx_result(atype As String, Value As String, image As Bitmap)
      
  
    Log("type:" & atype &  "Value:" & Value)
  

End Sub

Any idea ?? :confused:
And you are trying it with the B4A project in #91?
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi great Johan. When i try ( last library #91 but also more "old" library ) with Samsung S7 Edge i have this result in Log Panel :



but even xxx_result dont work.
B4X:
Sub myzx_result(atype As String, Value As String, image As Bitmap)
      
  
    Log("type:" & atype &  "Value:" & Value)
  

End Sub

Any idea ?? :confused:
Try this one - I just need to add some code to return info to the B4A project. But it is far better.

https://www.b4x.com/android/forum/t...zxing-project-as-it-is-on-10-july-2016.68829/
 

Johan Schoeman

Expert
Licensed User
Longtime User

nw11

Member
Licensed User
Longtime User
if i scan a qrcode without torch (off) the library works correctly and fire xxx_result .. if i scan a qrcode with torch (on) the Library don't fire xxx_result and exit from the app immediatly after scan

can someone help me ?

tnk in advance

ps: the library used is #91
 

Johan Schoeman

Expert
Licensed User
Longtime User
if i scan a qrcode without torch (off) the library works correctly and fire xxx_result .. if i scan a qrcode with torch (on) the Library don't fire xxx_result and exit from the app immediatly after scan

can someone help me ?

tnk in advance

ps: the library used is #91
I have tested it on two devices and on both the myzx_result gets fired regardless of the torch being on or off. I have nevertheless changed the wrapper for the event to be raised from a different thread. Attached is the amended library files. See if it sorts out your problem.
 

Attachments

  • JhsIceZxing1DiffThread.zip
    437.8 KB · Views: 265

kokixy

New Member
Licensed User
Longtime User
Hi!
This library worked flawlessly on my phone previously, but now (i think after Android 6.0.1 update) it exits from the app immediately after scan. It doesn't fire xxx_result. I don't see any relation with the torch. I have a Note4, but it's also doesn't work on a fresh updated S7. I'm using it in portrait mode, if it counts.
Please help.
Thanks!
 
Last edited:
Top