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,983
  • JohanIcefairyZxingWidthMod.zip
    417.5 KB · Views: 1,339
  • JohanIcefairyZxingWidthModAndTextMod.zip
    418.4 KB · Views: 1,271
  • JohanIcefairyZxingWidthTextBitmap.zip
    421 KB · Views: 1,206
  • JohanIcefairyZxingWidthTextBitmapTouchTorch.zip
    421.5 KB · Views: 1,320
  • JohanIcefairyZxingTimeout.zip
    421.5 KB · Views: 1,355
  • b4aZXINGusingVer1.05.zip
    12.2 KB · Views: 1,063
  • JhsIceZxing1Ver1.05.zip
    444.1 KB · Views: 1,100
  • JhsIceZxing1Ver1.06.zip
    444.1 KB · Views: 1,711
Last edited:

Sannie72

Member
Licensed User
Longtime User
J
Have changed it to portrait (in the code as well as in the manifest) and then it does read code 39 (also code 128). See attached project. The library files are in the /files folder of the attached project. Copy them to your additional library folder. If you touch the display/screen while the scanner is active the torch will switch on/off. Same applies to the torch (while the scanner is active) when pressing the volume up/down button of your device.

JhsIceZxing1
Author:
Johan/IceFairy333
Version: 1

  • Methods:
    • isChineseCharacter (chineseStr As String) As Boolean
  • JhsIceZxing1
    Events:
    • result (atype as String As , Value as String As , image as Bitmap As )
    • timedout (timedOut as boolean As )
    • usercancelled (userCancelled as boolean As )
    Fields:
    • bottomPromptColor As Int
      Specify the color of the bottom prompt message
      Example:<code>
      zx.bottomPromptColor = Colors.Yellow
      </code>
    • bottomPromptDistanceFromBottom As Int
      Specify the distance of the bottom prompt message from the bottom
      Example:<code>
      zx.bottomPromptDistanceFromBottom = 1%x
      </code>
    • isportrait As Boolean
      Specify if it should be portrait or landscape mode
      Example:<code>
      zx.isportrait = True
      </code>
    • mustBeep As Boolean
      Specify if the device should make a beep sound upon a successful scan
      The default value is False
      The beep sound will be ToneGenerator.TONE_CDMA_ABBR_ALERT
      Example:<code>
      zx.mustBeep = True
      </code>
    • mustVibrate As Boolean
      Specify if the device should vibrate upon a successful scan
      The default value is False
      It will vibrate for 400ms
      Example:<code>
      zx.mustVibrate = True
      </code>
    • theBitMap As Bitmap
      Specify the bitmap that should be loaded into the mask
      Example:<code>
      Dim bm As Bitmap
      bm.Initialize(File.DirAssets,"B4A.png")
      zx.theBitMap = bm
      </code>
    • theBottomPromptMessage As String
      Specify the bottom prompt message
      Example:<code>
      zx.theBottomPromptMessage = "B4A"
      </code>
    • theBottomPromptTextSize As Int
      Specify the text size of the bottom prompt message
      Example:<code>
      zx.theBottomPromptTextSize = 5%y
      </code>
    • theFrameColor As Int
      Specify the color of the outer frame of the view finder rectangle
      Example:<code>
      zx.theFrameColor = Colors.Black
      </code>
    • theLaserColor As Int
      Specify the color of the laser
      Example:<code>
      zx.theLaserColor = Colors.Red
      </code>
    • theMaskColor As Int
      Specify the color of the mask around the view finder rectangle
      Example:<code>
      zx.theMaskColor = Colors.ARGB(95, 0, 0, 255)
      </code>
    • theResultColor As Int
      Specify the color of the mask around the view finder rectangle after a successful scan
      Example:<code>
      zx.theResultColor = Colors.Black
      </code>
    • theResultPointColor As Int
      Specify the color of the possible result points being displayed during a scan
      Example:<code>
      zx.theResultPointColor = Colors.Green
      </code>
    • theTopPromptMessage As String
      Specify the top prompt message
      Example:<code>
      zx.theTopPromptMessage = "Hallo"
      </code>
    • theTopPromptTextSize As Int
      Specify the text size of the top prompt message
      Example:<code>
      zx.theTopPromptTextSize = 5%y
      </code>
    • theViewFinderXfactor As Double
      Scale factor for the width of the view finder rectangle
      The factor ranges from 0 to 1 (for a width < 240 a default of 240 pixels will be applied)
      Example:<code>
      zx.theViewFinderXfactor = 0.5
      </code>
    • theViewFinderYfactor As Double
      Scale factor for the height of the view finder rectangle
      The factor ranges from 0 to 1 (for a height < 240 a default of 240 pixels will be applied)
      Example:<code>
      zx.theViewFinderYfactor = 0.5
      </code>
    • timeoutDuration As Int
      Specify the timeout duration - we need to get a successful scan before this else time out
      Example:<code>
      zx.timeoutDuration = 15
      </code>
      Set this value to for eg 2000000000 for and "indefinate" timeout (2 000 000 000 = 63 years+)
    • topPromptColor As Int
      Specify the color of the top prompt message
      Example:<code>
      zx.topPromptColor = Colors.Magenta
      </code>
    • topPromptDistanceFromTop As Int
      Specify the distance of the top prompt message from the top
      Example:<code>
      zx.topPromptDistanceFromTop = 1%x
      </code>
    • useFrontCam As Boolean
      Specify if the front facing or rear facing camera should be used
      Example:<code>
      zx.useFrontCam = False
      </code>
    Methods:
    • BeginScan (EventName As String)
      Start the scan
      Example:<code>
      Dim zx As JhsIceZxing1 '(declare this in Sub Globals)
      Dim bm As Bitmap '(declare this in Sub Globals)
      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
      zx.topPromptColor = Colors.Red
      zx.topPromptDistanceFromTop = 1%y 'pixel distance from top

      zx.theBottomPromptMessage = "Just for fun......"
      zx.theBottomPromptTextSize = 5%y 'text size
      zx.bottomPromptColor = Colors.Blue
      zx.bottomPromptDistanceFromBottom = 5%y 'pixel distance from top
      'add a bitmap
      zx.theBitMap = bm
      zx.theBitmapPosition(40%x,10%y,20%x,20%x)
      zx.BeginScan("myzx")
      End Sub
      Sub myzx_result(atype As String,Values As String)
      Msgbox(Values,"type:" & atype)
      End Sub
      Sub myzx_noscan(atype As String,Values As String)
      Msgbox(Values,"type:" & atype)
      End Sub
      </code>
    • theBitmapPosition (left As Int, top As Int, width As Int, height As Int)
      Specify the left, top, width, and height of the bitmap that will be placed on the mask
      Example:<code>
      zx.theBitmapPosition(40%x,10%y,20%x,20%x)
      </code>
    Permissions:
    • android.hardware.camera
    • android.hardware.camera.autofocus
    • android.permission.CAMERA
    • android.permission.FLASHLIGHT
    • android.permission.VIBRATE
    • android.permission.WRITE_EXTERNAL_STORAGE

...and would it be possible to allow another property zx.thePromptTextStyle? So I can change the text style from always italic to something else...
Would be nice ;)
 

Johan Schoeman

Expert
Licensed User
Longtime User
TextSkew1.png
J
...and would it be possible to allow another property zx.thePromptTextStyle? So I can change the text style from always italic to something else...
Would be nice ;)
See new lib files and project posted here. Copy/replace them in your additional libraries folder. It will allow you to set the "Italic skewness" of the top and bottom text messages individually. A value of zero will apply no skewness to the text. A (+) value = text slants to the left. A (-) value = text slants to the right.
See code below.
B4X:
    zx.textSkewnessTop = 0.25
zx.textSkewnessBottom = 0.0

TextSkew.png
 

Attachments

  • JhsIceZxing1.zip
    413.7 KB · Views: 484
  • b4aTextSkewness.zip
    430.4 KB · Views: 469
Last edited:

John Decowski

Member
Licensed User
Longtime User
This is great exactly what I was looking for! In the next week or two I am going to post my code for QR code generating.
 

Johan Schoeman

Expert
Licensed User
Longtime User

joilts

Member
Licensed User
Longtime User
Johan, see if you can help me on this. I´m using your lib to read QRCodes and it works fine. I´m also using in the same app, cameraExClass to take pictures. After open the camera for pictures, the first time I call ZXingLib, it opens but the camera gets no image. After timeout, if I open it again, everything works right. Any idea why this happens?

Thanks in advance,
Jorge

UPDATE: Thanks for your time and attention. I found that calling CAMEX.CloseNow immediately release the camera and I was able to use ZXingLib correct.
 
Last edited:

iCAB

Well-Known Member
Licensed User
Longtime User
Hi Johan

First let me thank you for the great work you have done in here.
As I read through this forum there are many releases, can you please post the latest version of the library to POST#1, so it will be easier to find

Thank you
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi Johan

First let me thank you for the great work you have done in here.
As I read through this forum there are many releases, can you please post the latest version of the library to POST#1, so it will be easier to find

Thank you
The library files in post #62 above
 

Fernando Melo

Member
Licensed User
Longtime User
Hi Johan,

congratulations for your great job. I am testing your library, but sometimes, can be a little slow to recognize the bar code. Its possible to choose the format we want? This will improve somehow the recognition?

Thanks.
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi Johan,

congratulations for your great job. I am testing your library, but sometimes, can be a little slow to recognize the bar code. Its possible to choose the format we want? This will improve somehow the recognition?

Thanks.
It think it can be done. I will try to amend the code sometime this coming weekend and then post the new library files if the change is successful.
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi Johan,

congratulations for your great job. I am testing your library, but sometimes, can be a little slow to recognize the bar code. Its possible to choose the format we want? This will improve somehow the recognition?

Thanks.
Not too sure if this is going to improve the speed of recognition (test and let me know) but it allows you to pass in the categories of barcodes to scan that are supported by class DecodeFormatManager in the ZXING project that have been wrapped:

B4X:
'START OF ADDED 7 NOVEMBER 2015:
'   Specify the bar code formats to scan
'   If not specified then all code formats will be scanned
'   Specify one of the following: PRODUCT_FORMATS, ONE_D_FORMATS, QR_CODE_FORMATS, DATA_MATRIX_FORMATS, AZTEC_FORMATS, PDF_417_FORMATS, TWO_D_FORMATS
'   PRODUCT_FORMATS are the following: UPC A, UPC E, EAN 13, EAN 8, RSS 14
'   ONE_D_FORMATS are all the PRODUCT FORMATS plus the following: CODE 39, CODE 93, CODE 128, ITF
'   TWO_D_FORMATS are the following: QR CODE, DATA MATRIX, AZTEC CODE, PDF 417

    'COMMENT THE BELOW LINE OR USE zx.ScanMode = "" to scan all the 1D and 2D type barcodes that this version of the ZXING library supports
    zx.ScanMode = "ONE_D_FORMATS"   
            
    'END OF ADDED 7 NOVEMBER 2015
   
    zx.BeginScan("myzx")

Attached the new library files and the B4A project.
 

Attachments

  • b4aZXING.zip
    11.9 KB · Views: 350
  • JhsIceZxing1LibFiles.zip
    414.3 KB · Views: 361

Johan Schoeman

Expert
Licensed User
Longtime User
The attached library files should (theoretically) speed up the process but I am of the opinion that speed of recognition has a lot to do with the autofocus capability of a device so that the code can recognise the barcode being scanned. Use the attached library files with the project in Post #74 above (replace your library files with the attached library files).

There is a slight change in the B4A code. Sure you will figure it out...

B4X:
    zx.ScanMode = zx.FORMATS_ONE_D

Also posting the Java Code.
 

Attachments

  • JhsIceZxing1LibFilesNew.zip
    414.8 KB · Views: 386
  • src9.zip
    472.3 KB · Views: 362
Last edited:

Fernando Melo

Member
Licensed User
Longtime User
The attached library files should (theoretically) speed up the process but I am of the opinion that speed of recognition has a lot to do with the autofocus capability of a device so that the code can recognise the barcode being scanned. Use the attached library files with the project in Post #74 above (replace your library files with the attached library files).

There is a slight change in the B4A code. Sure you will figure it out...

B4X:
    zx.ScanMode = zx.FORMATS_ONE_D

Also posting the Java Code.
thanks for your effort but you are right. It has to do with autofocus capability.
 

corradotisi

Member
Licensed User
Longtime User
Hi Johan,
the B4aZing example, after 2 or 3 serial barcode reading, crash main module without any messages. I'm on a Samsung android 5
thanks
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi Johan,
the B4aZing example, after 2 or 3 serial barcode reading, crash main module without any messages. I'm on a Samsung android 5
thanks
I don't have an Android 5 device so cannot debug it at present. See if the one at the below link will work better on Android 5. As far as I understand it already makes provision for at least Android 5. Work through the thread to get the latest version of this posting

https://www.b4x.com/android/forum/t...ew-lib-files-in-post-26-for-datamatrix.60155/

Please try QRCP4 too and let me know if it behaves the same. You can find it here

https://www.b4x.com/android/forum/threads/qr-code-library.41408/#post-259448
 
Last edited:

Kurt McCullum

Member
Licensed 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?
 
Top