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: 3,053
  • b4aZXscannerLiveView.zip
    19 KB · Views: 3,010
Last edited:

Gerrard

Member
Licensed User
Longtime User
Johan, you will be please to know that the problem has been solved. You can see it in the thread "Problem with ADB" started by lucarr67 on 2016/06/27.
The solution is in post #5 by Rick Harris, which I followed and now all is well after loading the earlier version of the Platform-Tools.
Regards Gerrard
 

pcicom

Member
Licensed User
Longtime User
Hi. excelent work..

I need read barcode inside a panel, or how read in other layout..

layoutMain = Menu, in menu 1 botton to loadLayour reder
LayoutReaded = button for read or capture codebar.

this a simple, but cant solved

Tanks..
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi. excelent work..

I need read barcode inside a panel, or how read in other layout..

layoutMain = Menu, in menu 1 botton to loadLayour reder
LayoutReaded = button for read or capture codebar.

this a simple, but cant solved

Tanks..
If it does not work for you making use of a panel then consider making use of 2 activities. One for you main layout and another one where the scanner can be started from.
 

klaus

Expert
Licensed User
Longtime User
I have tryed this library with problems.
Trying to use it in another project the camera doesn't display.
Whith the original code I got it working, but just changing the Package name the camera doesn't display anymore.
For another test, I used the original code and added new code, it worked on my phone.
Compiling it on my tablet, exactly the same code, the camera doesn't display.
And now, even with the original code, on the phone, the camera doesn't display anymore, I removed all old apps on the device.
So I am really confused on what happens and what I am doing wrong ?
That's what I get.

upload_2016-10-23_19-6-47.png


I use B4A version 6.30 and my devices have Android 6.0.1 on both.
 

Johan Schoeman

Expert
Licensed User
Longtime User
I have tryed this library with problems.
Trying to use it in another project the camera doesn't display.
Whith the original code I got it working, but just changing the Package name the camera doesn't display anymore.
For another test, I used the original code and added new code, it worked on my phone.
Compiling it on my tablet, exactly the same code, the camera doesn't display.
And now, even with the original code, on the phone, the camera doesn't display anymore, I removed all old apps on the device.
So I am really confused on what happens and what I am doing wrong ?
That's what I get.

View attachment 49320

I use B4A version 6.30 and my devices have Android 6.0.1 on both.
Hi Klaus, can you upload your project with the changed package name?
 

klaus

Expert
Licensed User
Longtime User
It was your original code and I just added "1" at the end.
I tested it now again, reinstalling it and removing all old stuff, and it works again, even changing the Package name.
I did the whole process for the other project once again, and now it works !!!
Probably I left some old stuff on the devices.
Sorry for having bothered you.

There is another problem with the Sub zxslv_scanner_stopped event routine when running the original code the first time.
It throws this error:
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
Unexpected event (missing RaiseSynchronousEvents): zxslv_scanner_stopped
Check the unfiltered logs for the full stack trace.
Error occurred on line: 91 (Main)
java.lang.NullPointerException: Attempt to read from field 'anywheresoftware.b4a.BA JHS.zxScannerLiveView.main.activityBA' on a null object reference
at JHS.zxScannerLiveView.main._zxslv_scanner_stopped(main.java:487)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:708)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:337)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
at anywheresoftware.b4a.shell.Shell$2.run(Shell.java:311)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
** Activity (main) Resume **

It seems yoou are missing a RaiseSynchronousEvents in the code when you compile it to a library.
 

Johan Schoeman

Expert
Licensed User
Longtime User
It was your original code and I just added "1" at the end.
I tested it now again, reinstalling it and removing all old stuff, and it works again, even changing the Package name.
I did the whole process for the other project once again, and now it works !!!
Probably I left some old stuff on the devices.
Sorry for having bothered you.

There is another problem with the Sub zxslv_scanner_stopped event routine when running the original code the first time.
It throws this error:
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
Unexpected event (missing RaiseSynchronousEvents): zxslv_scanner_stopped
Check the unfiltered logs for the full stack trace.
Error occurred on line: 91 (Main)
java.lang.NullPointerException: Attempt to read from field 'anywheresoftware.b4a.BA JHS.zxScannerLiveView.main.activityBA' on a null object reference
at JHS.zxScannerLiveView.main._zxslv_scanner_stopped(main.java:487)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:708)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:337)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
at anywheresoftware.b4a.shell.Shell$2.run(Shell.java:311)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
** Activity (main) Resume **

It seems yoou are missing a RaiseSynchronousEvents in the code when you compile it to a library.
Thanks Klaus - will look into it. Does it only happen on the first run after the initial first time installation?
 

Johan Schoeman

Expert
Licensed User
Longtime User
It was your original code and I just added "1" at the end.
I tested it now again, reinstalling it and removing all old stuff, and it works again, even changing the Package name.
I did the whole process for the other project once again, and now it works !!!
Probably I left some old stuff on the devices.
Sorry for having bothered you.

There is another problem with the Sub zxslv_scanner_stopped event routine when running the original code the first time.
It throws this error:
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
Unexpected event (missing RaiseSynchronousEvents): zxslv_scanner_stopped
Check the unfiltered logs for the full stack trace.
Error occurred on line: 91 (Main)
java.lang.NullPointerException: Attempt to read from field 'anywheresoftware.b4a.BA JHS.zxScannerLiveView.main.activityBA' on a null object reference
at JHS.zxScannerLiveView.main._zxslv_scanner_stopped(main.java:487)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:708)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:337)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
at anywheresoftware.b4a.shell.Shell$2.run(Shell.java:311)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
** Activity (main) Resume **

It seems yoou are missing a RaiseSynchronousEvents in the code when you compile it to a library.
Klaus, please try with the attached lib files and see if the error still occurs (Version 1.01)
 

Attachments

  • zxScannerLiveViewLibFiles_V1_01.zip
    49.6 KB · Views: 493

xpectmore

Member
Licensed User
Longtime User
nice stuff !!!
cool job !

to avoid Unfortunately zxScannerLiveView was stopped .... and then crash after that 5 secconds(cause i press start twice)
put in Activity_create
B4X:
b2.Visible=False

then

B4X:
Sub b1_Click

   zxslv.Visible = True
   zxslv.startScanner
   b1.Visible=False
   b2.Visible=True
End Sub
Sub b2_Click

   zxslv.Visible = False
   zxslv.stopScanner
   l1.Text = ""
   b1.Visible=True
   b2.Visible=False
End Sub

great job !!!!


.. anyway i found it much sexy like this :
 

Attachments

  • aaaaa2.zip
    21.2 KB · Views: 238
  • test54126758d83d112b5297263d86b3f185.png
    test54126758d83d112b5297263d86b3f185.png
    538 bytes · Views: 252
  • muchsexy2.zip
    20.3 KB · Views: 235
  • 3.png
    3.png
    230.1 KB · Views: 271
  • MASTERPIECE.zip
    20.3 KB · Views: 254
Last edited:

fabton1963

Member
Licensed User
Longtime User
Is the zxScannerLiveView library compatible with B4A 7.1 and android 5.1 ?
If I run Johan sample it works,
if I replicate a new app using cut and paste of code and manifest, my new app crash when I try to execute zxslv.Initialize("zxslv") instruction.
Any ideas ?
Fabrizio.
 

Johan Schoeman

Expert
Licensed User
Longtime User
Is the zxScannerLiveView library compatible with B4A 7.1 and android 5.1 ?
If I run Johan sample it works,
if I replicate a new app using cut and paste of code and manifest, my new app crash when I try to execute zxslv.Initialize("zxslv") instruction.
Any ideas ?
Fabrizio.
Please post the error in the B4A log that you are getting. I guess you have not copied the required resourse files
 

fabton1963

Member
Licensed User
Longtime User
Please post the error in the B4A log that you are getting. I guess you have not copied the required resourse files

Thank you solved, i just copy the missing resource files from your demo app as you suggest.
I whoul'd like to see my B4A logs but log window just show a white window with message "Logger connected to: HUAWEI HUAWEI VNS-L31".
Now I'll try to uninstall and install again B4A.

Thank you for your good works with barcode library.
Fabrizio
 

MarcoRome

Expert
Licensed User
Longtime User
Hi Johan.
Is it possible that take also "picture" when raise zxslv_scanresult ?
Thank you
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi Johan.
Is it possible that take also "picture" when raise zxslv_scanresult ?
Thank you
Hi Marco

Do you want the bitmap returned of the barcode that was scanned in the B4A event?
 

MarcoRome

Expert
Licensed User
Longtime User
Hi dear.
i will want the picture when scan code... if possible. Example i Have a box with this label barcode... Now i Have result this barcode i will want Also picture with box
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi dear.
i will want the picture when scan code... if possible. Example i Have a box with this label barcode... Now i Have result this barcode i will want Also picture with box
Hi Marco

Will see if I can bring back the bitmap.
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi Marco

Will see if I can bring back the bitmap.
See PM that I have sent you with new lib files. Thanks for the donation. Much appreciated!

Rgds

JS
 

rkmoray

Active Member
Licensed User
Longtime User
I love this lib, but I have 1 issue.
While I have a button for the flash (on/off), the flash also turns on/off if the scanner area is touched while scanning.
I do not want the user to turn on the torch, unless the "torch"button is selected.
How do I do that
 
Top