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!

You can download and test any posting of mine in this thread but if you want to use it then you need to
 

Attachments

  • JohanIcefairyZxing.zip
    416.8 KB · Views: 968
  • JohanIcefairyZxingWidthMod.zip
    417.5 KB · Views: 560
  • JohanIcefairyZxingWidthModAndTextMod.zip
    418.4 KB · Views: 513
  • JohanIcefairyZxingWidthTextBitmap.zip
    421 KB · Views: 487
  • JohanIcefairyZxingWidthTextBitmapTouchTorch.zip
    421.5 KB · Views: 559
  • JohanIcefairyZxingTimeout.zip
    421.5 KB · Views: 576
  • b4aZXINGusingVer1.05.zip
    12.2 KB · Views: 298
  • JhsIceZxing1Ver1.05.zip
    444.1 KB · Views: 288
  • JhsIceZxing1Ver1.06.zip
    444.1 KB · Views: 702
Last edited:

Johan Schoeman

Expert
Licensed User
If mustBeep and mustVibrate are set to false there are no results returned, must be a bug.
Strange - works fine on both my devices (Samsung S4 and Samsung GT-P3100). Just tested it and both return the result with any combination of true/false for the beep and vibrate.
 

bluedude

Well-Known Member
Licensed User
I did not but I fixed the error in the source I have. Will check your original source. Did not work on several devices.
 

bluedude

Well-Known Member
Licensed User
When I moved setResult outside the } it works fine. Before when mustbeep and mustvibrate are false nothing happens.

if (b4aZXingLib.mustBeep == true) { //added by johan schoeman - option to beep
final ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, 300);
tg.startTone(ToneGenerator.TONE_CDMA_ABBR_ALERT);
//setResult(RESULT_OK, data);
}
setResult(RESULT_OK, data);
 

Johan Schoeman

Expert
Licensed User
When I moved setResult outside the } it works fine. Before when mustbeep and mustvibrate are false nothing happens.

if (b4aZXingLib.mustBeep == true) { //added by johan schoeman - option to beep
final ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, 300);
tg.startTone(ToneGenerator.TONE_CDMA_ABBR_ALERT);
//setResult(RESULT_OK, data);
}
setResult(RESULT_OK, data);


Call to "setResult(RESULT_OK, data);" was already moved outside the curly bracket with the project and library in post #32 (src_7.zip). You must have been using a version earlier than this. That is why I could not get the same behaviour.
 

Johan Schoeman

Expert
Licensed User
This update adds a separate event for the scan being cancelled by the user. Posting the updated project (library files are in the /files folder of the project) as well as the updated source code.

Events = result, timedout, and usercancelled
 

Attachments

  • JohanIcefairyZxingTimeoutUserCancel.zip
    425 KB · Views: 362
  • src_8.zip
    471.2 KB · Views: 155

JTmartins

Active Member
Licensed User
Hi Johan,

Been playing with you excelent work, however as someone mentioned before there is a problem with the landscape scan. The image apears rotated and distorted. I did not have this behaviour in the original icefary lib (or any other in fact). I've looked around in the java code, but could not understand where this problem is coming from.

Also in my case (and probably for everyone that does not use English), characters from QRcode are not reading properly. In my case there are some names in the QR. and names as Anónio, or José, come with wrong characters. Basicly every accented chars come wrong. Also I did not experienced this behaviour in the original lib, and as you probably do not use foreign chars, you may not have noticed this.

Your work with the customization of this lib, has been amazing. Thank you very much. I guess that with this 2 litle things solved, this will defenitively be the best code scan lib for B4A.
 

JTmartins

Active Member
Licensed User
Attached is the source code with 2 little corrections

1 - The preview while scanning is now ok. That means that "isportrait" is now working correctly in portrait and landscape modes preview.
2 - Latin characters (as Á,é,Ú, etc) where not apearing correctly if used in codes. Now they are.

This source can be compiled easily with SLC
 

Attachments

  • src_9.zip
    477.1 KB · Views: 225

jruiz1998

Member
Licensed User
Attached is the source code with 2 little corrections

1 - The preview while scanning is now ok. That means that "isportrait" is now working correctly in portrait and landscape modes preview.
2 - Latin characters (as Á,é,Ú, etc) where not apearing correctly if used in codes. Now they are.

This source can be compiled easily with SLC

JTMartins,
It does not read correctly with landscape, neither recognize zx.theBitMapLeft, Top, Width, Height.

I will be glad if you please fix this.
Thanks
 
Last edited:

JTmartins

Active Member
Licensed User
Hi Jruiz,

I just added the mentioned functions to the lib. 99.99% of the work has been done by Johan Schoeman.

Anyway, I use it daily in landscape and it's reading correctly the scanned QRcodes.

What situation are you getting when you use it in landscape ? Can you please explain, so that I or anyone would try to help.

Regards,

JM
 

Johan Schoeman

Expert
Licensed User
JTMartins,
It does not read correctly with landscape, neither recognize zx.theBitMapLeft, Top, Width, Height.

I will be glad if you please fix this.
Thanks
Which one of the projects posted in this thread are you using as your starting point (I.e which one did you download)? Or did you compile the Java source code in post #47 to a library and started your own project from scratch?
 

jruiz1998

Member
Licensed User
Which one of the projects posted in this thread are you using as your starting point (I.e which one did you download)? Or did you compile the Java source code in post #47 to a library and started your own project from scratch?

Yes, I took the one src in post #47 and I compiled. But eliminated first the one I had took in #1.
I did it again, I have the same problems.

Wich one do I have to take?

Thanks
 

Johan Schoeman

Expert
Licensed User
Yes, I took the one src in post #47 and I compiled. But eliminated first the one I had took in #1.
I did it again, I have the same problems.

Wich one do I have to take?

Thanks
Best you start with the B4A project in post #45 (JohanIcefairyZxingTimeoutUserCancel.zip). Use the library files that are in the /files folder of this project. Copy them to your additional libraries folder. If it does not work then please post your complete project including the library files that you are using at present.
 

jruiz1998

Member
Licensed User
Best you start with the B4A project in post #45 (JohanIcefairyZxingTimeoutUserCancel.zip). Use the library files that are in the /files folder of this project. Copy them to your additional libraries folder. If it does not work then please post your complete project including the library files that you are using at present.

Yes, it is working now.
Thanks a lot
 

Sannie72

Member
Licensed User
I tried the demo (post 45), and it works fine. I my case only with CODE_128, though. I have an application where I need to scan both CODE_128 and CODE_39, but CODE_39 just stops the app.
I am using a Samsung Galaxy S6 for testing. Would it be possible to also scan code_39? I am using ABzxing now, but that relies on an external barcode app. Not requiring an other app is a better solution.
 

Johan Schoeman

Expert
Licensed User
I tried the demo (post 45), and it works fine. I my case only with CODE_128, though. I have an application where I need to scan both CODE_128 and CODE_39, but CODE_39 just stops the app.
I am using a Samsung Galaxy S6 for testing. Would it be possible to also scan code_39? I am using ABzxing now, but that relies on an external barcode app. Not requiring an other app is a better solution.
Will look into it. Can't remember if this specific version of ZXing that the project makes use of supported code 39 or not. I had to reinstate Aztec codes and PDF417 in it as it was disabled in the original code. You can in the mean time use QRCPV4 that is posted here

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

It does support code 39
 

Johan Schoeman

Expert
Licensed User
I tried the demo (post 45), and it works fine. I my case only with CODE_128, though. I have an application where I need to scan both CODE_128 and CODE_39, but CODE_39 just stops the app.
I am using a Samsung Galaxy S6 for testing. Would it be possible to also scan code_39? I am using ABzxing now, but that relies on an external barcode app. Not requiring an other app is a better solution.
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
 

Attachments

  • tryThis.zip
    430.3 KB · Views: 256
Last edited:

Sannie72

Member
Licensed User
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.
Tried the attached example, but this only gives me 'App not Installed' after hitting run. Will try to find out what I am doing wrong...
 
Top