B4A Library 1D and 2D Barcode Scanner with ZBAR - another Barcode Scanner that is 100% embedded in B4A

Johan Schoeman

Expert
Licensed User
The attached project wraps the ZBAR part of this Github project. It looks very much the same as the project that I have posted here. Both projects use the same ViewFinder but they use different decoding engines (ZXING and ZBAR respectively).

I have not tested all the barcode formats but it should scan the following types:

BarcodeFormat.PARTIAL
BarcodeFormat.EAN8
BarcodeFormat.UPCE
BarcodeFormat.ISBN10
BarcodeFormat.UPCA
BarcodeFormat.EAN13
BarcodeFormat.ISBN13
BarcodeFormat.I25
BarcodeFormat.DATABAR
BarcodeFormat.DATABAR_EXP
BarcodeFormat.CODABAR
BarcodeFormat.CODE39
BarcodeFormat.PDF417
BarcodeFormat.QRCODE
BarcodeFormat.CODE93
BarcodeFormat.CODE128

There seems to be an issue with the scanning of PDF417 codes. I have emailed the author of the Github project (Dushyanth Maguluru) and brought it to his attention. Will update and post new library files once the issue with PDF417 codes has been resolved.

EDIT 26 Feb 2016: See this post as far as scanning of PDF417 is concerned
https://sourceforge.net/p/zbar/support-requests/76/

Please take note of the xml files in the /Objects/res/layout and Objects/res/values folders of the B4A project should you start a new project from scratch.

Posting the following:
1. B4A project demonstrating the Barcode Scanner using the ZBAR decoder
2. Library files - you need to download them from this link (too big to upload to this post) - unzip them and copy them to your additional library folder



1.png

Sample code:

B4X:
#Region  Project Attributes
    #ApplicationLabel: ZbarBarcodeScanner
    #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 zb1 As ZbarBarcodeScanner
    Private b1 As Button
    Private b2 As Button
    Private b3 As Button
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")

    zb1.LaserColor = Colors.Yellow
    zb1.MaskColor = Colors.ARGB(150, 0, 0, 200)
    zb1.BorderColor = Colors.Magenta
    zb1.BorderStrokeWidth = 5
    zb1.BorderLineLength = 40
    zb1.Visible = False

End Sub

Sub Activity_Resume

'   zb1.startScanner

End Sub

Sub Activity_Pause (UserClosed As Boolean)

    zb1.stopScanner
    zb1.Visible = False

End Sub

Sub b1_Click

    zb1.toggleFlash

End Sub

Sub b2_Click
    zb1.Visible = True
    zb1.startScanner

End Sub

Sub b3_Click
    zb1.Visible = False
    zb1.stopScanner

End Sub



Sub zb1_scan_result(scantext As String, scanformat As String)

    Log("B4A scantext = " & scantext)
    Log("B4A scanformat = " & scanformat)

End Sub

The library:

ZbarBarcodeScanner
Author:
Github: Dushyanth Maguluru, Wrapped by: Johan Schoeman
Version: 1
  • ZbarBarcodeScanner
    Events:
    • scan_result (scantext As String, scanformat As String)
    Fields:
    • ba As BA
    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)
    • handleResult (rawResult As Result)
    • startScanner
    • stopScanner
    • toggleFlash
    Permissions:
    • android.permission.CAMERA
    • android.permission.FLASHLIGHT
    Properties:
    • Background As Drawable
    • BorderColor As Int [write only]
    • BorderLineLength As Int [write only]
    • BorderStrokeWidth As Int [write only]
    • Color As Int [write only]
    • Enabled As Boolean
    • Height As Int
    • LaserColor As Int [write only]
    • Left As Int
    • MaskColor As Int [write only]
    • Tag As Object
    • Top As Int
    • Visible As Boolean
    • Width As Int



You can use it in portrait mode and landscape mode by changing the B4A project's attribute and the code in the Designer:
#SupportedOrientations: portrait
 

Attachments

Last edited:

krebsi83

Member
Licensed User
I have tried the lib... It's great and very fast!
But I have a problem... my app (also your example) don't run correctly in release-mode :,(
IN RELEASE-MODE: After the app/lib has recognized the barcode... the picture stands still... no beep, no result
 

Johan Schoeman

Expert
Licensed User
I have tried the lib... It's great and very fast!
But I have a problem... my app (also your example) don't run correctly in release-mode :,(
IN RELEASE-MODE: After the app/lib has recognized the barcode... the picture stands still... no beep, no result
Try it with the set of new lib files at this link https://www.dropbox.com/s/y8ovcdjef0i421n/ZbarBarcodeScannerLib23Feb.zip?dl=0
I have removed the sound for now - seems like some devices don't like it and then for some reason the "handleResult" call back in the wrapper does not fire. Will try and find a workaround for it sometime during this week but the new libs should sort out the "freezing" problem.

Else just add your own sound in the B4A event that gets fired when the scanner returns the scanned result to the B4A project
 

krebsi83

Member
Licensed User
Try it with the set of new lib files at this link https://www.dropbox.com/s/y8ovcdjef0i421n/ZbarBarcodeScannerLib23Feb.zip?dl=0
I have removed the sound for now - seems like some devices don't like it and then for some reason the "handleResult" call back in the wrapper does not fire. Will try and find a workaround for it sometime during this week but the new libs should sort out the "freezing" problem.

Else just add your own sound in the B4A event that gets fired when the scanner returns the scanned result to the B4A project
Thank you so much! You are great!
This Version works without "freezing" in release-mode.
I look forward to the next release. Until then I can live without sound. ;)

By the way: The Zbar-Lib scans faster than your other two Libs. I have a lowprice HOMTOM HT7 china-phone with a bad camera... but with this Zbar-lib I get great results!
 

incendio

Well-Known Member
Licensed User
Thanks for this library. Great library and very fast.

I have a problem, when tried to scan same 1D barcode, over and over, around 10x, sometimes it produce double scan result. So, instead of got 10 scan results, I could got 11 or 12 results.
 

Johan Schoeman

Expert
Licensed User
Thanks for this library. Great library and very fast.

I have a problem, when tried to scan same 1D barcode, over and over, around 10x, sometimes it produce double scan result. So, instead of got 10 scan results, I could got 11 or 12 results.
Are you first pointing the camera away from the barcode before you try to scan it again? My best guess is that a frame is passed to the ZBAR engine and decoded and the decoding takes place fast enough that it grabs another frame and is able to decode it before you have pointed the camera away from the barcode that you are scanning. If you don't want a double scan to occur then compare the new scan result with the previous scan result in Sub zb1_scan_result and discard the new result if it is the same as the previous result. You will then of course "not be able to scan the same barcode consecutively unless a different barcode is scanned inbetween" - or at least as far as the user is concerned.
 

incendio

Well-Known Member
Licensed User
Yes, I have pointed camera right away after scanned.

If I scan 2 different barcode repeatedly, barcode A, barcode B, barcode A, barcode B, and so on, i got correct result.

Scanning same barcode consecutively is important for me, so can not just discard scan result.
 

Johan Schoeman

Expert
Licensed User
Yes, I have pointed camera right away after scanned.

If I scan 2 different barcode repeatedly, barcode A, barcode B, barcode A, barcode B, and so on, i got correct result.

Scanning same barcode consecutively is important for me, so can not just discard scan result.
The only way that it will report a "double" scan is when the ZBAR library (NOT THE WRAPPER) was passed two frames to decode in the time that you have pointed the scanner at the barcode. It decoded a frame and before you could point it away it grabbed and passed another frame to be decoded (probably because it seems to be fast). So not sure how you want to approach the "double" scan...For how long do you want to point the scanner at a barcode without it reporting a "double" scan?

The only way to maybe take care of this (seeing that you want to scan the same code consecutively) is to perhaps build a throttle/time delay into the original code that you can set from the B4A code.
 

Mahares

Well Known Member
Licensed User

incendio

Well-Known Member
Licensed User
The only way that it will report a "double" scan is when the ZBAR library (NOT THE WRAPPER) was passed two frames to decode in the time that you have pointed the scanner at the barcode. It decoded a frame and before you could point it away it grabbed and passed another frame to be decoded (probably because it seems to be fast). So not sure how you want to approach the "double" scan...For how long do you want to point the scanner at a barcode without it reporting a "double" scan?

The only way to maybe take care of this (seeing that you want to scan the same code consecutively) is to perhaps build a throttle/time delay into the original code that you can set from the B4A code.
I have no knowledge of Java, so could not write time delay in the original code.

I have tried though in B4A, to stop scanner after scan result, delay a few nano seconds
and then start the scanner again, but performance is bad, to slow.

Just curious: Have you tried Johan's Zxing lib instead. It also scans very fast and apparently scans the same type codes. I have not encountered the problem you describe, but our situations may be different.
https://www.b4x.com/android/forum/threads/1d-and-2d-barcode-scanner-with-zxing-another-barcode-scanner-that-is-100-embedded-in-b4a.63764/
Yes, i have tried this one also, same problem also for unknown reason, this one take a very slow to get a result, so didn't use it.

The ZBAR is very fast, a pitty that has this problem, can't use it for my app.

For now, I use third party barcode app, Barcode Scanner+ from play store, a bit slower for a batch scanning, but very accurate.

I noticed, on Barcode Scanner+, scanning between barcodes was not instantly, there was a delay but this delay is still very acceptable, may be only 0.5 sec or less. On ZBAR, scanning was almost instantly.
 
Last edited:

Johan Schoeman

Expert
Licensed User
I have no knowledge of Java, so could not write time delay in the original code.

I have tried though in B4A, to stop scanner after scan result, delay a few nano seconds
and then start the scanner again, but performance is bad, to slow.



Yes, i have tried this one also, same problem also for unknown reason, this one take a very slow to get a result, so didn't use it.

The ZBAR is very fast, a pitty that has this problem, can't use it for my app.

For now, I use third party barcode app, Barcode Scanner+ from play store, a bit slower for a batch scanning, but very accurate.
ZBAR does not have a problem....you are moving too slow for ZBAR....:) Move faster!
 

Johan Schoeman

Expert
Licensed User

incendio

Well-Known Member
Licensed User
Works like charm.

There are 2 things :
1) I didn't add zb1.ScanDelay = 200, but it seem there is already default delay, scanning next barcode wasn't instantly, but it was enough delay so that it won't scanning twice accidentally
2) There was no beep sound on success scan, but this is easy to solve, I added beeper

At last, I have a working barcode scanner inside B4A, thanks to you :)
 

Johan Schoeman

Expert
Licensed User
...but it seem there is already default delay...
...There was no beep sound on success scan, but this is easy to solve, I added beeper...
Yes, I have set the default delay as 100ms
I have removed the sound from the library as it is simple to add it via B4A code
 
Last edited:

ALMRosario

New Member
Licensed User
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #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 Button1 As Button
    Private Button2 As Button
    Private Button3 As Button
    Private ZbarBarcodeScanner1 As ZbarBarcodeScanner
    Private Label1 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("Layout1")
Activity.LoadLayout("Main")
ZbarBarcodeScanner1.LaserColor = Colors.Yellow
    ZbarBarcodeScanner1.MaskColor = Colors.ARGB(150, 0, 0, 200)
    ZbarBarcodeScanner1.BorderColor = Colors.Magenta
    ZbarBarcodeScanner1.BorderStrokeWidth = 5
    ZbarBarcodeScanner1.BorderLineLength = 40
    ZbarBarcodeScanner1.Visible = False
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
ZbarBarcodeScanner1.stopScanner
    ZbarBarcodeScanner1.Visible = False
End Sub


Sub Button1_Click
    ZbarBarcodeScanner1.Visible=True
    ZbarBarcodeScanner1.startScanner
End Sub

Sub Button2_Click
    ZbarBarcodeScanner1.Visible=False
    ZbarBarcodeScanner1.stopScanner
End Sub

Sub Button3_Click
    ZbarBarcodeScanner1.toggleFlash
End Sub

Sub ZbarBarcodeScanner1_scan_result(scantext As String, scanformat As String)
    Label1.Text=scantext
End Sub

When I click on the button 1 is closed, what it is wrong ?
 

Johan Schoeman

Expert
Licensed User
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #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 Button1 As Button
    Private Button2 As Button
    Private Button3 As Button
    Private ZbarBarcodeScanner1 As ZbarBarcodeScanner
    Private Label1 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("Layout1")
Activity.LoadLayout("Main")
ZbarBarcodeScanner1.LaserColor = Colors.Yellow
    ZbarBarcodeScanner1.MaskColor = Colors.ARGB(150, 0, 0, 200)
    ZbarBarcodeScanner1.BorderColor = Colors.Magenta
    ZbarBarcodeScanner1.BorderStrokeWidth = 5
    ZbarBarcodeScanner1.BorderLineLength = 40
    ZbarBarcodeScanner1.Visible = False
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
ZbarBarcodeScanner1.stopScanner
    ZbarBarcodeScanner1.Visible = False
End Sub


Sub Button1_Click
    ZbarBarcodeScanner1.Visible=True
    ZbarBarcodeScanner1.startScanner
End Sub

Sub Button2_Click
    ZbarBarcodeScanner1.Visible=False
    ZbarBarcodeScanner1.stopScanner
End Sub

Sub Button3_Click
    ZbarBarcodeScanner1.toggleFlash
End Sub

Sub ZbarBarcodeScanner1_scan_result(scantext As String, scanformat As String)
    Label1.Text=scantext
End Sub

When I click on the button 1 is closed, what it is wrong ?
What is the error in the Log? Please post it.
But before you do so...read post #1 thoroughly - especially the sentence in BOLD.
 
Last edited:
Top