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

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

  • b4aZbarBarcodeScanner.zip
    9.9 KB · Views: 2,724
Last edited:

ma7tin

Member
Licensed User
Hi guys, I got question about zbarBarcodescanner. Can I add it to TabStripViewPager or it is working only as a separeta application ?
Problem is that when I add this to my tabstrip1 and I am gonna to startscanner app is going to stopped without any errors, please help.
 

Ed Brown

Active Member
Licensed User
Longtime User
Hello @Johan Schoeman

Thanks for a great library! Works very well.
Just wondering if it is possible to switch between the back and front facing cameras on a device?
Also, can this library be used with B4i?

Thanks again for the great effort.
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hello @Johan Schoeman

Thanks for a great library! Works very well.
Just wondering if it is possible to switch between the back and front facing cameras on a device?
Also, can this library be used with B4i?

Thanks again for the great effort.
Will have to add some code to the wrapper to allow for the front camera to be used. Will do so if I find some time in this week. Unfortunately it can not be used with B4I
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hello @Johan Schoeman

Thanks for a great library! Works very well.
Just wondering if it is possible to switch between the back and front facing cameras on a device?
Also, can this library be used with B4i?

Thanks again for the great effort.
Here you go (read the pop up help on the methods zb1.FrontFacingCamera and zb1.BackFacingCamera when using them - it will tell you about the available cameras):
1. You can download the new library files from here https://www.dropbox.com/s/8kfyg5jkt51q4js/ZbarBarcodeScannerLibFiles.zip?dl=0
2. Have added the following:
B4X:
Dim frontCamId As Int
Dim backCamId As Int
...and....
B4X:
frontCamId = zb1.FrontFacingCamera
Log("frontCamId = " & frontCamId)
backCamId = zb1.BackFacingCamera
Log("backCamId = " & backCamId)
....and....
B4X:
Sub b2_Click

zb1.Visible = True  
zb1.startScanner(backCamId)    'changed the call to accommodate front/back camera
  
End Sub

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
    Private l1 As Label
    Dim frontCamId As Int
    Dim backCamId As Int
  
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
  
    zb1.ScanDelay = 200
  
    frontCamId = zb1.FrontFacingCamera
    Log("frontCamId = " & frontCamId)
    backCamId = zb1.BackFacingCamera
    Log("backCamId = " & backCamId)

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(backCamId)         'changed the call to accommodate front/back camera
  
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)
    l1.Text = scantext
  
End Sub
 

Attachments

  • b4aZbarBarcodeScanner.zip
    10.1 KB · Views: 484

carycai

Member
Licensed User
Longtime User
@Johan Schoeman
Thanks for your great library.I am in China,but i can't get your library files in dropbox cause i can't access dropbox website for our country's reasons.
Could you send me the library files? Thanks a lot in advance.
My email: [email protected]
 

incendio

Well-Known Member
Licensed User
Longtime User
Dear Johan,

Is it possible to widen Camera View Window Width ? It looks so tiny on my phone & unable to scan long barcode cause not fit in the Camera View Window, see the attachment.

Thanks in advance.
 

Attachments

  • SS1.png
    SS1.png
    95 KB · Views: 284
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User

carycai

Member
Licensed User
Longtime User
Dear Johan,

Is it possible to widen Camera View Window Width ? It looks so tiny on my phone & unable to scan long barcode cause not fit in the Camera View Window, see the attachment.

Thanks in advance.
I think the same way. Waiting for reply.
 

Johan Schoeman

Expert
Licensed User
Longtime User
Dear Johan,

Is it possible to widen Camera View Window Width ? It looks so tiny on my phone & unable to scan long barcode cause not fit in the Camera View Window, see the attachment.

Thanks in advance.
I think time has come for you to start digging into some Java code. Here is a link to my complete ZXING and ZBAR code/wrappers - the whole lot! I will assist you to set it up so that you can compile it with Simple Library Compiler - but you can do the changes and once done post the updated source code and the new B4A library files....;)

https://www.dropbox.com/s/ge1hb16xj7cthm9/barcodescanner-master.zip?dl=0
 

incendio

Well-Known Member
Licensed User
Longtime User
I think time has come for you to start digging into some Java code. Here is a link to my complete ZXING and ZBAR code/wrappers - the whole lot! I will assist you to set it up so that you can compile it with Simple Library Compiler - but you can do the changes and once done post the updated source code and the new B4A library files....;)

https://www.dropbox.com/s/ge1hb16xj7cthm9/barcodescanner-master.zip?dl=0
Dear Johan,

Thanks for your tips, & have downloaded this source code, but from now, I have a simple solution
1) make panel that holds ZBAR bigger
2) change the codes to this
B4X:
    zb.MaskColor             = Colors.Transparent    
zb.BorderColor             = Colors.Transparent
    zb.BorderStrokeWidth   = 3
    zb.BorderLineLength     = 0

Everything is OK now.
 

lemonisdead

Well-Known Member
Licensed User
Longtime User

Johan Schoeman

Expert
Licensed User
Longtime User
Hello Johan,
I beg your pardon because perhaps I did not get all. I installed the library version 1.00 from the link at first post (https://www.dropbox.com/s/8kfyg5jkt51q4js/ZbarBarcodeScannerLibFiles.zip?dl=0).
When I compile (using B4A V6.00), I get that error :

Thanks
That is very strange. Why will it be looking for zbar.jar in the B4A lib folder and not in your additional lib folder? Try it again by putting zbar.jar in the B4A lib folder. I have not downloaded V6 yet - will do so when back from work tonight.
 

Johan Schoeman

Expert
Licensed User
Longtime User

lemonisdead

Well-Known Member
Licensed User
Longtime User
I have just upgraded to V6 and have no problem with it whatsoever. Runs perfectly.
Could you please check if you don't have the zbar.jar in your "official library folder", please ? (and thanks of course for having tried)
 
Top