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

Johan Schoeman

Expert
Licensed User
Longtime User
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)
@lemonisdead, definitely not in the "official library folder". I have it in my additional library folder only.
 

Johan Schoeman

Expert
Licensed User
Longtime User

Johan Schoeman

Expert
Licensed User
Longtime User
Thanks I did while waiting and it works without problem
And what happen if you copy zbar.jar to the "official library folder"? Does the ZBAR project then run?
 

lemonisdead

Well-Known Member
Licensed User
Longtime User
Sorry Johan I have missed : I have tried to move the library in the "official library folder" and did got an error saying it was duplicating (even if the library was no more in the "additional folder")

It is something I have to fix on my PC I think. Thanks for having verified
 

Johan Schoeman

Expert
Licensed User
Longtime User
Sorry Johan I have missed : I have tried to move the library in the "official library folder" and did got an error saying it was duplicating (even if the library was no more in the "additional folder")

It is something I have to fix on my PC I think. Thanks for having verified

Hope you get it sorted out @lemonisdead....

BadNews-BangHead.gif
 

Richard Goh

Active Member
Licensed User
Longtime User
I was trying to use this scanning function in my project. The programs hit into below error when run the function (zb1.startScanner) without breakpoint in debugging mode. But the scanning view will displayed without error when USING A BREAKPOINT. Any idea or advice?

android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.Resources.getValue(Resources.java:1133)
at android.content.res.Resources.getColor(Resources.java:815)
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:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
 

Johan Schoeman

Expert
Licensed User
Longtime User
I was trying to use this scanning function in my project. The programs hit into below error when run the function (zb1.startScanner) without breakpoint in debugging mode. But the scanning view will displayed without error when USING A BREAKPOINT. Any idea or advice?

android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.Resources.getValue(Resources.java:1133)
at android.content.res.Resources.getColor(Resources.java:815)
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:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
Do you have the necessary resource files in your project? See post #1 where I have said....
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.
 

Richard Goh

Active Member
Licensed User
Longtime User
Hi Johan,

Thanks. Just saw it and it's solved after copy the folders over to my project.

Thank you somuch.
 

jaraiza

Active Member
Licensed User
Longtime User
Hi! Thanks for the lib! I have a doubt, is there any way to expand the scan area to be fullscreen? I've problems scanning the barcodes because the area is so small.

Thanks!
 

Johan Schoeman

Expert
Licensed User
Longtime User

jaraiza

Active Member
Licensed User
Longtime User
Hi!

I had to download zbar.jar from here. I loaded the previous example but I added this @ line 66: Dim Cmr As Int = zb1.CAMERA_BACK because startScanner now needs the CameraID, so I changed it from zb1.startScanner to zb1.startScanner(Cmr)

But now I'm not getting any camera output nor error, it just keeps at the gray screen with 3 blue buttons. I can click the buttons but nothing happens.

Thanks!
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi!

I had to download zbar.jar from here. I loaded the previous example but I added this @ line 66: Dim Cmr As Int = zb1.CAMERA_BACK because startScanner now needs the CameraID, so I changed it from zb1.startScanner to zb1.startScanner(Cmr)

But now I'm not getting any camera output nor error, it just keeps at the gray screen with 3 blue buttons. I can click the buttons but nothing happens.

Thanks!
Attached is the zbar.jar file that I have used in the library - copy it to your additional library folder
 

Attachments

  • zbar.jar
    5.9 KB · Views: 710
  • b4aZbarBarcodeScanner.zip
    10.1 KB · Views: 673

Johan Schoeman

Expert
Licensed User
Longtime User
The result is the same as before :( Here is the source code I'm testing

Thanks for your help! :)
I have just tried your project on two devices - Kitkat and Lollipop and they both work fine...
 
Last edited:

MARCOS SANDRONI LIMA RIOS

New Member
Licensed User
Longtime User
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.
how find files values
android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.Resources.getValue(Resources.java:2354)
at android.content.res.Resources.getColor(Resources.java:2036)
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)
 

Johan Schoeman

Expert
Licensed User
Longtime User
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.
how find files values
android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.Resources.getValue(Resources.java:2354)
at android.content.res.Resources.getColor(Resources.java:2036)
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)
You are missing files as what I have mentioned should be in some folders. Make sure these files are set to READ ONLY before you run B4A project. Some programs used to extract zip files sometimes changes the READ ONLY property of the files and then they get deleted when you compile the B4A project. So, unzip the project again and then make sure the files that I have mentioned is set to READ ONLY and only then compile the B4A project.
 

Ricardo Escalante

Member
Licensed User
Hi all, I'm new to the forum and I have a problem trying to read a Code-128 Auto barcode, don't read it. Attached an example in pdf
I will appreciate any type of help.
Thank you very much
 

Attachments

  • xrEtiquetasvb.pdf
    424.8 KB · Views: 356
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
Hi all, I'm new to the forum and I have a problem trying to read a Code-128 Auto barcode, don't read it. Attached an example in pdf
I will appreciate any type of help.
Thank you very much
Hi Ricardo

Not sure what it is that you are battling with but I can read all your Code 128 barcodes in the PDF in post #58.... Can you upload your sample project?

upload_2017-2-8_18-50-0.png
 

Ricardo Escalante

Member
Licensed User
Hi Ricardo

Not sure what it is that you are battling with but I can read all your Code 128 barcodes in the PDF in post #58.... Can you upload your sample project?

View attachment 52692

Dear johan:
I'm looking at it and I can not believe it, it's just the same example code you have. I think I have to devote myself to it. Thank you so much for your time to respond.
Greetings from my leon gto mexico
 
Top