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

Danie Steyn

Member
Licensed User
Longtime User
Hi Johan. Thanks for the great lib.
I was testing the scanner with a PDF417 barcode, (I think it's a PDF417 barcode, the ones printed on South African car licenses and drivers license), but it's not scanning. I there anything that I to set or enable for this to work?
It reads normal EAN barcodes perfectly with or without the flash on.
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi Johan. Thanks for the great lib.
I was testing the scanner with a PDF417 barcode, (I think it's a PDF417 barcode, the ones printed on South African car licenses and drivers license), but it's not scanning. I there anything that I to set or enable for this to work?
It reads normal EAN barcodes perfectly with or without the flash on.
Nothing else required Danie. Have just scanned the attached code with a Samsung S4 mini (don't hold the scanner too close to the PDF417 code - it also helps to move the scanner a little bit away and then again a little bit closer.

DanieSteyn.png
 

Danie Steyn

Member
Licensed User
Longtime User
Nothing else required Danie. Have just scanned the attached code with a Samsung S4 mini (don't hold the scanner too close to the PDF417 code - it also helps to move the scanner a little bit away and then again a little bit closer.

View attachment 40399
Thanks Johan. After I sent the message I tried it again and I managed to get it to work. It's a bit tricky but I am sure I will figure it out.
 

Danie Steyn

Member
Licensed User
Longtime User
Nothing else required Danie. Have just scanned the attached code with a Samsung S4 mini (don't hold the scanner too close to the PDF417 code - it also helps to move the scanner a little bit away and then again a little bit closer.

View attachment 40399
I changed my AutoFocusInterval to 5000 and now it seems to work like a charm or it might be the updated library (if that makes any sense).
Baie dankie Johan.
 

imbault

Well-Known Member
Licensed User
Longtime User
I'm trying this good lib, is there a property to get the barcode type scanned ? (code 39...)

Thanks
 

Johan Schoeman

Expert
Licensed User
Longtime User
I'm trying this good lib, is there a property to get the barcode type scanned ? (code 39...)

Thanks
The original Github code does not get/fetch the barcode format from the ZXING library. I will have to add some code to the Github project to get/fetch the format. Will look into that at some stage...
 

Johan Schoeman

Expert
Licensed User
Longtime User
It would be great to retrieve this info, thank you

Regards
Attached are new B4A library files that will return the type / format of the scanned barcode to B4A as a String. Example (see Log(zxslv.BarcodeFormat))

B4X:
Sub zxslv_scanresult
  
    Log(zxslv.ScanResult)
    Log(zxslv.BarcodeFormat)
    l1.Text = zxslv.ScanResult
  
End Sub
 

Attachments

  • zxScannerLibFilesFormat.zip
    49.3 KB · Views: 381
Last edited:

killiak

Member
Licensed User
Longtime User
Ok, i'm getting the error

B4X:
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Error occurred on line: 36 (Main)
android.content.res.Resources$NotFoundException: Resource ID #0x0
    at android.content.res.Resources.getValue(Resources.java:2354)
    at android.content.res.Resources.loadXmlResourceParser(Resources.java:3681)
    at android.content.res.Resources.getLayout(Resources.java:2170)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
    at eu.livotov.labs.android.camview.ScannerLiveView.initUI(ScannerLiveView.java:58)
    at eu.livotov.labs.android.camview.ScannerLiveView.<init>(ScannerLiveView.java:53)
    at zxscanwrapper.zxScanWrapper._initialize(zxScanWrapper.java:90)
    at zxscanwrapper.zxScanWrapper.Initialize(zxScanWrapper.java:83)
    at b4a.example.main._activity_create(main.java:360)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:697)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:246)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at b4a.example.main.afterFirstLayout(main.java:102)
    at b4a.example.main.access$000(main.java:17)
    at b4a.example.main$WaitForLayout.run(main.java:80)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:146)
    at android.app.ActivityThread.main(ActivityThread.java:5593)
    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:1283)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
    at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Resume **

That the Res (no edit) i read about it, copy and paste Res from Example to res of my start from scratch example still having the problem. Put it the res in Read Only also... but something is missing?

My code

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #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 Button1 As Button
    Private Label1 As Label
    Private sc As zxScannerLiveView
   
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")
    sc.Initialize("sc")
    Activity.AddView(sc, 50%x - 45%y, 5%y, 90%y, 90%y)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Thnx if some1 could help me... btw scanner sample work perfect :(
 

killiak

Member
Licensed User
Longtime User
i reply myself... Copying all the res in the folder and change to read only all works well... don't know why, but i was working at debug (with bridge) change to Release, problem fix, then switch back to Debug mode and works again.. weird...i was even closing and opening the proyect about 2 days and still receiving the problem, switch and switch back and fixed!.

thanx (to read even it was solved)
 

killiak

Member
Licensed User
Longtime User
Ok New problem now, i have the problem that the program start...i have my adquire button, when i click in the middle of the screen appear a Black frame with those little red lines (same as example) but nothing.....comera doesn't show... Someone has this problem? I will attach the sample but is just exactly as the original source.

B4X:
#Region  Project Attributes
    #ApplicationLabel: Registra Producto
    #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 scan As zxScannerLiveView
    Private Butadquiri As Button
    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("AdquiereDatos")

    scan.Initialize("scan")
    Activity.AddView(scan, 50%x - 45%y, 5%y, 90%y, 90%y)
    scan.HudVisible=True
    scan.PlaySound=True
    scan.Visible=False

End Sub

Sub Activity_Resume

End Sub

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


Sub ButAdquiri_Click
    scan.Visible=True
    scan.startScanner
End Sub

Sub scan_scanresult
    Label1.Text = scan.ScanResult
   
End Sub


Sub scan_scanner_started
   
    Log("Inicia Escaner")
   
End Sub

Sub scan_scan_error
   
    Log("Errore en el Escaneo")
   
End Sub

Sub scan_scanner_stopped
   
    Log("Escaner Finalizado")
   
End Sub
 

Johan Schoeman

Expert
Licensed User
Longtime User
Ok New problem now, i have the problem that the program start...i have my adquire button, when i click in the middle of the screen appear a Black frame with those little red lines (same as example) but nothing.....comera doesn't show... Someone has this problem? I will attach the sample but is just exactly as the original source.

B4X:
#Region  Project Attributes
    #ApplicationLabel: Registra Producto
    #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 scan As zxScannerLiveView
    Private Butadquiri As Button
    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("AdquiereDatos")

    scan.Initialize("scan")
    Activity.AddView(scan, 50%x - 45%y, 5%y, 90%y, 90%y)
    scan.HudVisible=True
    scan.PlaySound=True
    scan.Visible=False

End Sub

Sub Activity_Resume

End Sub

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


Sub ButAdquiri_Click
    scan.Visible=True
    scan.startScanner
End Sub

Sub scan_scanresult
    Label1.Text = scan.ScanResult
  
End Sub


Sub scan_scanner_started
  
    Log("Inicia Escaner")
  
End Sub

Sub scan_scan_error
  
    Log("Errore en el Escaneo")
  
End Sub

Sub scan_scanner_stopped
  
    Log("Escaner Finalizado")
  
End Sub
See my answer to your question here
https://b4x.com/android/forum/threads/barcode-library-problem.62624/#post-395387

Please post your questions in the applicable thread else we have no idea what project you are referring to. No need to duplicate your question in the Questions forum.
 

Johan Schoeman

Expert
Licensed User
Longtime User

Johan Schoeman

Expert
Licensed User
Longtime User
He want the picture to be mirrored (horizontal or vertical i dont know; probably horizontal)
Instead of normal he want the picture "up-side down"
So the periscope lens is then flipping the image to be a mirror image and he wants to correct it?
 

Johan Schoeman

Expert
Licensed User
Longtime User
That´s how i interpreted/understand it, yes
Two periscope lenses in series should do it....:) Lib can unfortunately not do it as it stands at present.
 
Top