B4A Library Barcode Scanner - 100% embedded within B4A (15 Feb 2016 : New library files in Post #105)

The attached project wraps this Github project. It is a barcode scanner based on the ZXING project. I have successfully scanned the following 1D and 2D barcodes:
1. Code 39
2. Code 93
3. Code 128
4. Two-of-Five Interleaved (TFI)
5. EAN13
6. EAN8
7. PDF417
8. QR Code
9. Aztec Code
10. Codabar

It is 100% embedded within the B4A project via a CustomView. Thus, you can add buttons, background images, background colors, labels, textviews, or whatever you like to the activity via the designer or via code. It does not have all the bells and whistles that the ZXING project offers (i.e ability to set the laser color etc) but it works 100% within B4A. It does not take over control of the B4A activity - set the size (width / height / left / top) of the CustomView in the B4A code and that will be the size of your barcode View Finder. Add buttons to start and stop the scanner. It is as simple as that.

For as long as what the scanner is active it will scan 1D/2D barcodes i.e no need to stop and start the scanner to start scanning a new barcode. Just point the scanner at the next barcode to scan.

I have added the following events to the B4A project:
1. scanresult - here you can get the decoded string/text result when a successful scan occurred
2. scanner_started - this event is raised when the scanner is activated via B4A code
3. scan_error - sorry, but have not seen this event in action yet as I have had no miss scans thus far
4. scanner_stopped - this event is raised when the scanner is stopped.

There are a number of files in the B4A project's Object/res/..... folders that are set to read only. Keep them like that.

There are permissions added to the B4A manifest - make sure you take note of it should you start a new B4A project.

You will need core-3.1.0.jar and android-support-v4.jar to be in your additional library folder. I have zipped them together - you can download them from HERE. Extract them from the zipped file and copy them to your additional library folder.

1.png


2.png


Some sample code:

B4X:
#Region  Project Attributes
    #ApplicationLabel: zxScannerLiveView
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: landscape
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private zxslv As zxScannerLiveView
    Private b1 As Button
    Private b2 As Button
    Private l1 As Label

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("main")

    zxslv.Initialize("zxslv")

    Activity.AddView(zxslv, 50%x - 45%y, 5%y, 90%y, 90%y)

    zxslv.HudVisible = True
    zxslv.PlaySound = True
    zxslv.Visible = False

End Sub

Sub Activity_Resume


End Sub

Sub Activity_Pause (UserClosed As Boolean)

    zxslv.Visible = False
    zxslv.stopScanner

End Sub

Sub b1_Click

    zxslv.Visible = True
    zxslv.startScanner

End Sub
Sub b2_Click

    zxslv.Visible = False
    zxslv.stopScanner
    l1.Text = ""

End Sub

Sub zxslv_scanresult

    Log(zxslv.ScanResult)
    l1.Text = zxslv.ScanResult

End Sub

Sub zxslv_scanner_started

    Log("Scanner Started")

End Sub

Sub zxslv_scan_error

    Log("Scan Error")

End Sub

Sub zxslv_scanner_stopped

    Log("Scanner stopped")

End Sub


And this will add a coloured frame around the view finder - making use of panels via B4A code:

B4X:
#Region  Project Attributes
    #ApplicationLabel: zxScannerLiveView
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: landscape
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private zxslv As zxScannerLiveView
    Private b1 As Button
    Private b2 As Button
    Private l1 As Label
    Private l2, l3, l4, l5 As Label

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("main")

    zxslv.Initialize("zxslv")
    Dim framecolor As Int = Colors.Red

    l2.Initialize("")
    l3.Initialize("")
    l4.Initialize("")
    l5.Initialize("")

    Activity.AddView(zxslv, 50%x - 45%y, 5%y, 90%y, 90%y)
    Activity.AddView(l2, 50%x - 45%y - 1%y, 4%y, 1%y, 92%y)
    Activity.AddView(l3, 50%x + 45%y, 4%y, 1%y, 92%y)
    Activity.AddView(l4, 50%x - 45%y - 1%y, 4%y, 91%y, 1%y)
    Activity.AddView(l5, 50%x - 45%y - 1%y, 5%y + 90%y, 91%y, 1%y)


    l2.Color = framecolor
    l2.Visible = False

    l3.Color = framecolor
    l3.Visible = False

    l4.Color = framecolor
    l4.Visible = False

    l5.Color = framecolor
    l5.Visible = False

    zxslv.HudVisible = True
    zxslv.PlaySound = True
    zxslv.Visible = False


End Sub

Sub Activity_Resume


End Sub

Sub Activity_Pause (UserClosed As Boolean)

    zxslv.Visible = False
    zxslv.stopScanner

    l2.Visible = False
    l3.Visible = False
    l4.Visible = False
    l5.Visible = False

End Sub

Sub b1_Click

    zxslv.Visible = True
    zxslv.startScanner

    l2.Visible = True
    l3.Visible = True
    l4.Visible = True
    l5.Visible = True

End Sub
Sub b2_Click

    zxslv.Visible = False
    zxslv.stopScanner
    l1.Text = ""

    l2.Visible = False
    l3.Visible = False
    l4.Visible = False
    l5.Visible = False



End Sub

Sub zxslv_scanresult

    Log(zxslv.ScanResult)
    l1.Text = zxslv.ScanResult

End Sub

Sub zxslv_scanner_started

    Log("Scanner Started")

End Sub

Sub zxslv_scan_error

    Log("Scan Error")

End Sub

Sub zxslv_scanner_stopped

    Log("Scanner stopped")

End Sub

It will look like this:

3.png


zxScannerLiveView
Author: Github: Dmitri Livotov, Wrapper: Johan Schoeman
Version: 1



zxScannerLiveView
Events:

  • scan_error ( )
  • scanner_started ( )
  • scanner_stopped ( )
  • scanresult ( )
Fields:
  • BACK As String
  • FRONT As String
  • ba As BA
  • mfrontOrBack As String
Methods:
  • BringToFront
  • DesignerCreateView (base As PanelWrapper, lw As LabelWrapper, props As Map)
  • Initialize (EventName As String)
  • Invalidate
  • Invalidate2 (arg0 As Rect)
  • Invalidate3 (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
  • IsInitialized As Boolean
  • RemoveView
  • RequestFocus As Boolean
  • SendToBack
  • SetBackgroundImage (arg0 As Bitmap)
  • SetColorAnimated (arg0 As Int, arg1 As Int, arg2 As Int)
  • SetLayout (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
  • SetLayoutAnimated (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int, arg4 As Int)
  • SetVisibleAnimated (arg0 As Int, arg1 As Boolean)
  • startScanner
    Starts scanner, using device default camera
  • stopScanner
    Stops currently running scanner
  • toggleFlash
Properties:
  • BackgroudImage As String [write only]
  • Background As Drawable
  • CameraToUse As String [write only]
  • Color As Int [write only]
  • Enabled As Boolean
  • Height As Int
  • HudVisible As Boolean [write only]
  • Left As Int
  • PlaySound As Boolean [write only]
  • SameCodeRescanProtectionTime As Long [write only]
  • ScanResult As String [read only]
  • Tag As Object
  • Top As Int
  • Visible As Boolean
  • Width As Int
You can download and test any posting of mine in this thread but if you want to use it then you need to
 

Attachments

  • zxScannerLiveViewLibFiles.zip
    44.9 KB · Views: 2,284
  • b4aZXscannerLiveView.zip
    19 KB · Views: 2,265
Last edited:

sorex

Expert
Licensed User
nice, Johan.

Does it still need that extra bar code scanner app or will it work independent?
 

Johan Schoeman

Expert
Licensed User
nice, Johan.

Does it still need that extra bar code scanner app or will it work independent?
Completely independent. Nothing else required other than having the library files that I have posted as well as the library files that I have put in Dropbox (see link in post #1) in your additional library folder...and then you have complete control over the appearance of the activity. Now is that not nice.....?
 

Mahares

Expert
Licensed User
Johan: You are amazing. You take something very complex and turn it into something simple that all of us in the forum can understand and most importantly use in the real world. Between you and @DonManfred cranking up these new libs at a tremendous clip, your libs and those of Manfred combined should be referred to as JohanManfred B4AHub.
 

incendio

Well-Known Member
Licensed User
Hi Johan,

Thanks for this library.

I want to ask, on my lib folder, there is already android-support-v4.jar file, but it size only 377KB, your is about 1.2MB.
Is it OK to replace this file with yours?
 

Johan Schoeman

Expert
Licensed User
Hi Johan,

Thanks for this library.

I want to ask, on my lib folder, there is already android-support-v4.jar file, but it size only 377KB, your is about 1.2MB.
Is it OK to replace this file with yours?
Yes, the one that I have posted is a more recent version with additional classes. Should work without a problem (but keep a backup of your file somewhere for just in case....)
 

Johan Schoeman

Expert
Licensed User
Now you can have full control over the scanner. Have added control of the torch (on/off) to B4A while the scanner is active. Note that the torch will only switch on/off once the scanner has started - not before that (see attached B4A project for code to set the torch and the Boolean variable that I have added to track when the scanner has started so that the torch can be switched on/off)

Posting the following:
1. The new B4A library files (you will still require core-3.1.0.jar and android-support-v4.jar that I have put in Dropbox - link to download them is in post #1 of this thread)
2. B4A project demonstrating use of the torch while the barcode scanner is active.

Torch on:
4.png


Torch Off:

5.png


Some sample code:

B4X:
#Region  Project Attributes
    #ApplicationLabel: zxScannerLiveView
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: landscape
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private zxslv As zxScannerLiveView
    Private b1 As Button
    Private b2 As Button
    Private l1 As Label
    Private l2, l3, l4, l5 As Label

    Private b3 As Button
    Private scanStarted As Boolean = False

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("main")

    zxslv.Initialize("zxslv")
    Dim framecolor As Int = Colors.Red

    l2.Initialize("")
    l3.Initialize("")
    l4.Initialize("")
    l5.Initialize("")

    Activity.AddView(zxslv, 50%x - 45%y, 5%y, 90%y, 90%y)
    Activity.AddView(l2, 50%x - 45%y - 1%y, 4%y, 1%y, 92%y)
    Activity.AddView(l3, 50%x + 45%y, 4%y, 1%y, 92%y)
    Activity.AddView(l4, 50%x - 45%y - 1%y, 4%y, 91%y, 1%y)
    Activity.AddView(l5, 50%x - 45%y - 1%y, 5%y + 90%y, 91%y, 1%y)   


    l2.Color = framecolor
    l2.Visible = False

    l3.Color = framecolor
    l3.Visible = False

    l4.Color = framecolor
    l4.Visible = False

    l5.Color = framecolor
    l5.Visible = False

    zxslv.HudVisible = True
    zxslv.PlaySound = True
    zxslv.Visible = False


End Sub

Sub Activity_Resume


End Sub

Sub Activity_Pause (UserClosed As Boolean)

    zxslv.Visible = False
    zxslv.stopScanner

    l2.Visible = False
    l3.Visible = False
    l4.Visible = False
    l5.Visible = False   

End Sub

Sub b1_Click

    zxslv.Visible = True
    zxslv.startScanner

    l2.Visible = True
    l3.Visible = True
    l4.Visible = True
    l5.Visible = True   

End Sub
Sub b2_Click

    zxslv.Visible = False
    zxslv.stopScanner
    l1.Text = ""

    l2.Visible = False
    l3.Visible = False
    l4.Visible = False
    l5.Visible = False   



End Sub

Sub zxslv_scanresult

    Log(zxslv.ScanResult)
    l1.Text = zxslv.ScanResult

End Sub

Sub zxslv_scanner_started

    scanStarted = True
    Log("Scanner Started")

End Sub

Sub zxslv_scan_error

    Log("Scan Error")

End Sub

Sub zxslv_scanner_stopped
    scanStarted = False
    Log("Scanner stopped")

End Sub
Sub b3_Click

    If scanStarted = True Then
      zxslv.toggleFlash
    End If

End Sub

Updated B4A library:

zxScannerLiveView
Author: Github: Dmitri Livotov, Wrapper: Johan Schoeman
Version: 1



    • zxScannerLiveView
      Events:
      • scan_error ( )
      • scanner_started ( )
      • scanner_stopped ( )
      • scanresult ( )
      Fields:
      • BACK As String
      • FRONT As String
      • ba As BA
      • mfrontOrBack As String
      Methods:
      • BringToFront
      • DesignerCreateView (base As PanelWrapper, lw As LabelWrapper, props As Map)
      • Initialize (EventName As String)
      • Invalidate
      • Invalidate2 (arg0 As Rect)
      • Invalidate3 (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
      • IsInitialized As Boolean
      • RemoveView
      • RequestFocus As Boolean
      • SendToBack
      • SetBackgroundImage (arg0 As Bitmap)
      • SetColorAnimated (arg0 As Int, arg1 As Int, arg2 As Int)
      • SetLayout (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
      • SetLayoutAnimated (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int, arg4 As Int)
      • SetVisibleAnimated (arg0 As Int, arg1 As Boolean)
      • startScanner
        Starts scanner, using device default camera
      • stopScanner
        Stops currently running scanner
      • toggleFlash
      Properties:
      • BackgroudImage As String [write only]
      • Background As Drawable
      • CameraToUse As String [write only]
      • Color As Int [write only]
      • Enabled As Boolean
      • Height As Int
      • HudVisible As Boolean [write only]
      • Left As Int
      • PlaySound As Boolean [write only]
      • SameCodeRescanProtectionTime As Long [write only]
      • ScanResult As String [read only]
      • Tag As Object
      • Top As Int
      • Visible As Boolean
      • Width As Int
 

Attachments

  • b4aZXscannerLiveViewWithTorch.zip
    19.3 KB · Views: 640
  • zxScannerLiveViewLibFiles.zip
    45.1 KB · Views: 621
Last edited:

Anser

Well-Known Member
Licensed User
This is an excellent lib. Thank you very much.:)

Hi Johan,
Thanks for this library.

I want to ask, on my lib folder, there is already android-support-v4.jar file, but it size only 377KB, your is about 1.2MB.
Is it OK to replace this file with yours?

Yes, the one that I have posted is a more recent version with additional classes. Should work without a problem (but keep a backup of your file somewhere for just in case....)
So will it be a problem when there is an new update from Android for the android-support-v4.jar via Android SDK. I understand that many libs here are depended on android-support-v4.jar. For eg AppCompat, FusedLocationProvider, GooglePlay Services etc.
 
Last edited:

Johan Schoeman

Expert
Licensed User

bluedude

Well-Known Member
Licensed User
Front and back camera switch would be nice. For example the Nexus 7 only has front camera. Tablets can be used for loyalty checkins etc. and the Nexus 7 is suitable for that.
 

Johan Schoeman

Expert
Licensed User
Posting updated library files. Use it with the B4A project in post #12 above. This allows you to switch the flash/torch on and off by touching the scanner view when the scanner is active. You can therefore toggle the flash by either using the button or by touching the scanner view. The "button click" and "touch" events compliment one another i.e you can use them together. The touch event adds no additional methods to the B4A library - it is handled by the wrapper.

You still need the two library files that I have posted in Dropbox (see post #1) to be in your additional library folder.
 

Attachments

  • zxScannerLiveViewLibFiles.zip
    47.2 KB · Views: 490
Last edited:

Mahares

Expert
Licensed User
Barcode scanner example as it applies to SQLite:

Attached is a full functioning project that makes use of @Johan Schoeman’s latest and greatest barcode library where the scanner is 100% embedded in B4A. The project does the following:
1. A SQLite table is created with 5 columns and few records are inserted.
2. Once an item represented by the data in the first column PRODUCT_ID is scanned, the program extracts all the data pertinent to the scanned ID from the table and displays the results on a label on the device screen.
3. QR Codes representing the PRODUCT_ID are located in the attached JohanQRCode.txt file that you can print and use to test the scanning and app. IMPORTANT: JohanQRCode.txt must be renamed to JohanQRCode.xps after you download it, so it can be opened using XPS viewer for printing or displaying the QRCodes on your PC screen. The forum would not allow a file name with xps extension.
 

Attachments

  • ZXscannerJohanUsingSQLite111015.zip
    35.1 KB · Views: 482
  • JohanQRCode.txt
    38.6 KB · Views: 424

incendio

Well-Known Member
Licensed User
I tested the example, it scan fast, but when orientation change to portrait, it was very slow to scan, most of the time, failed to scan at all.

I changed the size of zxScannerLiveView, and noticed, with different size, scan speed also changed. Is there an optimum size to get better scan speed?
 

Johan Schoeman

Expert
Licensed User
I tested the example, it scan fast, but when orientation change to portrait, it was very slow to scan, most of the time, failed to scan at all.

I changed the size of zxScannerLiveView, and noticed, with different size, scan speed also changed. Is there an optimum size to get better scan speed?
The scanner uses the ZXING engine to identify and to decode the barcodes (core-3.1.0.jar that you have downloaded in post #1). The Github project that I have wrapped handles mostly the camera (open, close, etc). That being said, I am of the opinion that the camera appearance/distortion is far better/less than that of the camera manager in the ZXING project when orientation is changed from landscape to portrait or vice versa.

I am using a Sumsung S4 mini and in both landscape and portrait modes it scans any of the type of codes that I have listed in post #1 without missing a beat.
 
Last edited:
Top