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: 1,964
Last edited:

uniplan

Active Member
Licensed User
I tried the library and it is fantastic.
It is really simple to use.
Everything works perfectly.
The only problem is the area that displays the barcode is really very small. See picture that I attach.

Is there a way to enlarge this area?

Thanks.
 

Attachments

  • scan2mod.png
    scan2mod.png
    25.1 KB · Views: 266

Dr. Guaranexx

New Member
Licensed User
I tried the library and it is fantastic.
It is really simple to use.
Everything works perfectly.
The only problem is the area that displays the barcode is really very small. See picture that I attach.

Is there a way to enlarge this area?

Thanks.

It has been a couple months since I've worked with this library, but if I recall correctly, I was able to resize the dimension of the window by using the SetLeftAndRight() and SetTopAndBottom() methods on the scanner object in the variant specific script area. I would imagine setting the width and height would work as well. If it doesn't work while setting the property in the designer, try setting it in the variant specific scripts.
 
Last edited:

Johan Schoeman

Expert
Licensed User
I tried the library and it is fantastic.
It is really simple to use.
Everything works perfectly.
The only problem is the area that displays the barcode is really very small. See picture that I attach.

Is there a way to enlarge this area?

Thanks.
See post #51
 

ByteCounter

Member
Licensed User
Could someone please help with this:

I have hopefully installed everything correctly and the sample b4aZbarBarcodeScanner compiles and runs, but I don't see any camera image on the screen (only the 3 blue buttons appear on a gray background). Clicking Start/Stop scan does nothing.
I think this is the same problem mentioned in post #52 on Page 3. I have tried both debug & release mode, no change.

Device is 7" tablet, Android 4.4.4

For a beginner the install process is not given step by step, so this is what I did:
1. ZbarBarcodeScanner_13112016.zip extracted the files ZbarBarcodeScanner.jar & ZbarBarcodeScanner.xml to additional libraries folder. (downloaded from https://www.dropbox.com/s/8e1k1zebs4jiaze/ZbarBarcodeScanner_13112016.zip?dl=0 from Post #51)

2. zbar.jar copy to additional libraries folder (downloaded from Post #53)

3. b4aZbarBarcodeScanner.zip example B4A project (downloaded from Post #53).

Note I am not using a new project but the example one from Post #53

Also, I don't understand the note in Post #1 "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."
Does this mean I have to copy the contents of these xml files from the example folders to my new project folder (assuming I get this thing working)?

Thanks
 

Johan Schoeman

Expert
Licensed User
Could someone please help with this:

I have hopefully installed everything correctly and the sample b4aZbarBarcodeScanner compiles and runs, but I don't see any camera image on the screen (only the 3 blue buttons appear on a gray background). Clicking Start/Stop scan does nothing.
I think this is the same problem mentioned in post #52 on Page 3. I have tried both debug & release mode, no change.

Device is 7" tablet, Android 4.4.4

For a beginner the install process is not given step by step, so this is what I did:
1. ZbarBarcodeScanner_13112016.zip extracted the files ZbarBarcodeScanner.jar & ZbarBarcodeScanner.xml to additional libraries folder. (downloaded from https://www.dropbox.com/s/8e1k1zebs4jiaze/ZbarBarcodeScanner_13112016.zip?dl=0 from Post #51)

2. zbar.jar copy to additional libraries folder (downloaded from Post #53)

3. b4aZbarBarcodeScanner.zip example B4A project (downloaded from Post #53).

Note I am not using a new project but the example one from Post #53

Also, I don't understand the note in Post #1 "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."
Does this mean I have to copy the contents of these xml files from the example folders to my new project folder (assuming I get this thing working)?

Thanks
Is there any error in the B4A log? If so, can you please post it?
 

ByteCounter

Member
Licensed User
Is there any error in the B4A log? If so, can you please post it?

Hmm, I just rebooted the tablet and it now display the camera image with the scanning line.
But there are 2 problems:

1. Very difficult to scan barcode. Not sure if this cheap tablet has a poor camera but it is very difficult to scan even with more light and varying distance. I notice misreads sometimes too. Maybe a USB barcode scanner is better for my application.

2. More worrying, at some point a message comes up during the app:
"To protect you Google blocked installation of this app: Tcash
This app is fake. It can damage your device and steal your data"
Only started getting this after b4aZbarBarcodeScanner installed.
Can you confirm file integrity?
 

Johan Schoeman

Expert
Licensed User
Hmm, I just rebooted the tablet and it now display the camera image with the scanning line.
But there are 2 problems:

1. Very difficult to scan barcode. Not sure if this cheap tablet has a poor camera but it is very difficult to scan even with more light and varying distance. I notice misreads sometimes too. Maybe a USB barcode scanner is better for my application.

2. More worrying, at some point a message comes up during the app:
"To protect you Google blocked installation of this app: Tcash
This app is fake. It can damage your device and steal your data"
Only started getting this after b4aZbarBarcodeScanner installed.
Can you confirm file integrity?
Have never seen such a message on any of my 3 x devices that I am using. The file is fine - nothing in it that will steal your data. I am too dumb to add things like that.
 

Filippo

Expert
Licensed User
Hi @Johan Schoeman ,

First I must say that I am one of the many B4x-user of your work here is very appreciates. :)
If I can, then I would wish that the update of your library always to the first post store, otherwise it becomes increasingly difficult to find the current version. :(
 

Ricardo Escalante

Member
Licensed User
Hello to all again, i have a little problem, i hope somebody can help me.
Now with the changes to the android:targetSdkVersion="26", there's a new right to put in the manifest editor?
Becose the "zar1.startScanner" does not activate the scanner.

Sorry for my write english
 

Johan Schoeman

Expert
Licensed User
Hello to all again, i have a little problem, i hope somebody can help me.
Now with the changes to the android:targetSdkVersion="26", there's a new right to put in the manifest editor?
Becose the "zar1.startScanner" does not activate the scanner.

Sorry for my write english
This works:

Change your B4A Manifest to....
B4X:
<uses-sdk android:minSdkVersion="17" android:targetSdkVersion="26"/>
....so that it allows for SDK 26

Change your B4A code to:
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.
    Dim rp As RuntimePermissions

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
    Dim cnt As Int = 0
    
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")
    
    Dim Result As Boolean = True                                                                       
    If Not(rp.Check(rp.PERMISSION_CAMERA)) Then                                                       
        rp.CheckAndRequest(rp.PERMISSION_CAMERA)                                                       
        Wait For Activity_PermissionResult (Permission As String, Result As Boolean)                   
    End If                                                                                             
    If Result Then
    
        zb1.LaserColor = Colors.Yellow
        zb1.MaskColor = Colors.ARGB(150, 0, 0, 200)
        zb1.BorderColor = Colors.Magenta
        zb1.BorderStrokeWidth = 5
        zb1.BorderLineLength = 40
        zb1.Visible = True
        
        zb1.ScanDelay = 200
        
        frontCamId = zb1.FrontFacingCamera
        Log("frontCamId = " & frontCamId)
        backCamId = zb1.BackFacingCamera
        Log("backCamId = " & backCamId)
    Else
        Return
    End If       

End Sub

Sub Activity_Resume
    

End Sub

Sub Activity_Pause (UserClosed As Boolean)

    zb1.Visible = False
    zb1.stopScanner
    
End Sub

Sub b1_Click
    
    zb1.toggleFlash
    
End Sub

Sub b2_Click
    
    zb1.Visible = True   
    Log("trying to start scanner")
    zb1.startScanner(0)
    
End Sub

Sub b3_Click
    zb1.Visible = False
    zb1.stopScanner
    
End Sub



Sub zb1_scan_result(scantext As String, scanformat As String)
    cnt = cnt + 1
    Log("B4A scantext = " & scantext)
    Log("B4A scanformat = " & scanformat)
    l1.Text = scantext & " : " & cnt
    
End Sub

You need to enable the RuntimePermissions library in the libs tab of your IDE

It is working on Android 8.0.0 (Samsung Experience version 9.0)
 

Ricardo Escalante

Member
Licensed User
I really appreciate your prompt response and I regret giving you another annoying more, but when I select the library that you mention, it throws the following error.

Thank you very much for your help
 

Attachments

  • Error.png
    Error.png
    128.4 KB · Views: 119

Johan Schoeman

Expert
Licensed User

Ricardo Escalante

Member
Licensed User
I'm really sorry, but the error persists. Even when the current library is 1.03
I'm trying to solve it on my own, but if you have time to give me any suggestions, I'll thank you very much.
 

Ricardo Escalante

Member
Licensed User
Ready dear friends!
Reading other post I saw this suggestion.

Install the needed Libs in android sdk.

Open Android SDK Manager and install the latest versions of:
- Android Support Repository
- Android Support Library
- Google Play Services
- Google Repository

I installed it and it worked.

Thank you very much for your help. Thanks thanks
 

figorra

Member
Licensed User
I have this error on ZbarBarcodeScanner......



at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Copying updated assets files (5)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
** Activity (main) Pause, UserClosed = false **
** Activity (escaneo) Create, isFirst = true **
frontCamId = 1
backCamId = 0
** Activity (escaneo) Resume **
android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:216)
at android.content.res.Resources.getColor(Resources.java:964)
at android.content.res.Resources.getColor(Resources.java:940)
at main.java.me.dm7.barcodescanner.core.ViewFinderView.<init>(ViewFinderView.java:39)
at main.java.me.dm7.barcodescanner.core.BarcodeScannerView.createViewFinderView(BarcodeScannerView.java:57)
at main.java.me.dm7.barcodescanner.core.BarcodeScannerView.setupLayout(BarcodeScannerView.java:40)
at main.java.me.dm7.barcodescanner.core.BarcodeScannerView.setupCameraPreview(BarcodeScannerView.java:70)
at main.java.me.dm7.barcodescanner.core.CameraHandlerThread$1$1.run(CameraHandlerThread.java:31)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
 

Johan Schoeman

Expert
Licensed User
I have this error on ZbarBarcodeScanner......



at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Copying updated assets files (5)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
** Activity (main) Pause, UserClosed = false **
** Activity (escaneo) Create, isFirst = true **
frontCamId = 1
backCamId = 0
** Activity (escaneo) Resume **
android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:216)
at android.content.res.Resources.getColor(Resources.java:964)
at android.content.res.Resources.getColor(Resources.java:940)
at main.java.me.dm7.barcodescanner.core.ViewFinderView.<init>(ViewFinderView.java:39)
at main.java.me.dm7.barcodescanner.core.BarcodeScannerView.createViewFinderView(BarcodeScannerView.java:57)
at main.java.me.dm7.barcodescanner.core.BarcodeScannerView.setupLayout(BarcodeScannerView.java:40)
at main.java.me.dm7.barcodescanner.core.BarcodeScannerView.setupCameraPreview(BarcodeScannerView.java:70)
at main.java.me.dm7.barcodescanner.core.CameraHandlerThread$1$1.run(CameraHandlerThread.java:31)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

Sure you have all the files in the Objects/res/layout folder and in the Objects/res/values folder and the files are all set to READ ONLY?
 

Schakalaka

Active Member
Licensed User
Hello.
i haven't understand the solution for problem on post #61.
I have update library to version 1.0.3, but now the scanner not work anymore.
it log "IN setAutoFocus" when i click start scanner button..
What are the solutions?
Thanks all.
 

Johan Schoeman

Expert
Licensed User
Hello.
i haven't understand the solution for problem on post #61.
I have update library to version 1.0.3, but now the scanner not work anymore.
it log "IN setAutoFocus" when i click start scanner button..
What are the solutions?
Thanks all.
Can you post a sample project where it is not working?
 

Schakalaka

Active Member
Licensed User
yes there is.
try it.
be clear
"not working" = "i can' t make it work with the code that i wrote"
 

Attachments

  • scannerexample.zip
    17.8 KB · Views: 122
Last edited:

Johan Schoeman

Expert
Licensed User
yes there is.
try it.
be clear
"not working" = "i can' t make it work with the code that i wrote"
See if this starts the scanner. I have replaced your Main Activity with my original Main Activity code, have removed some views from your B4A layout, and commented out just about everything in your code modules. Have also replaced your b4a manifest entries. The scanner starts......

I would suggest you start adding additional stuff bit by bit and test every step after adding additional code to make sure you can trap when and where it goes skew.

1.png
 

Attachments

  • b4aZbarBarcodeScanner_a.zip
    16.5 KB · Views: 193
Top