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: 2,684
  • b4aZXscannerLiveView.zip
    19 KB · Views: 2,673
Last edited:

krebsi83

Member
Licensed User
Yes exactly, I want to reflect the image back. (Horizontal or vertical). The periscope lens is for better handling... because I want read a lot barcodes and do some interaction on screen after every read.... It's for an electronic ordering system without a laser or ccd barcode scanner.... sorry for my bad english
 

Johan Schoeman

Expert
Licensed User
Longtime User
Yes exactly, I want to reflect the image back. (Horizontal or vertical). The periscope lens is for better handling... because I want read a lot barcodes and do some interaction on screen after every read.... It's for an electronic ordering system without a laser or ccd barcode scanner.... sorry for my bad english
Sorry, but library as it is at present does not support this option - was never intended to support such an option.
 

Johan Schoeman

Expert
Licensed User
Longtime User
I have updated the B4A library based on recent updates to the original Github project. Posting the new B4A library files. It adds no additional functionality to the B4A code - it is purely an internal library upgrade. It fixes problems that existed when using this library with API 9 devices.

You still need core-3.1.0.jar and android-support-v4.jar to be in your additional library folder. They have been zipped together - you can download them from HERE. Extract them from the zipped file and copy them to your additional library folder.
 

Attachments

  • zxScannerLiveViewB4ALibFiles.zip
    49.4 KB · Views: 587

bocker77

Active Member
Licensed User
Longtime User
Johan, I am getting this error when stopping the scan. I am using your latest and greatest. It does not cause any issues so far though. Also this is in a new project.

Code at line 231:
Sub zxslv_scanner_stopped
scanStarted = False
Log("Scanner stopped")
End Sub

** Activity (gameplay) Pause, UserClosed = false **
Error occurred on line: 231 (GamePlay)
java.lang.NullPointerException
at com.KangaRooSoftware.gameplay._zxslv_scanner_stopped(gameplay.java:764)
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:702)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:336)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:246)
at anywheresoftware.b4a.shell.Shell$2.run(Shell.java:310)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5377)
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:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
** Service (starter) Destroy **
 

Johan Schoeman

Expert
Licensed User
Longtime User
Johan, I am getting this error when stopping the scan. I am using your latest and greatest. It does not cause any issues so far though. Also this is in a new project.

Code at line 231:
Sub zxslv_scanner_stopped
scanStarted = False
Log("Scanner stopped")
End Sub

** Activity (gameplay) Pause, UserClosed = false **
Error occurred on line: 231 (GamePlay)
java.lang.NullPointerException
at com.KangaRooSoftware.gameplay._zxslv_scanner_stopped(gameplay.java:764)
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:702)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:336)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:246)
at anywheresoftware.b4a.shell.Shell$2.run(Shell.java:310)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5377)
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:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
** Service (starter) Destroy **
It could be that the scanner was already stopped and then asked to stop again. Can you perhaps upload your project? Will be easier to catch the bug
 
D

Deleted member 103

Guest
Hi Johan Schoeman,

I have a question specifically what the camera would have a resolution of at least?
With my Tablet Medion LifeTab 7" (camera resolution 2MB) the bar code is not clear, and therefore it is not recognized.
 

Johan Schoeman

Expert
Licensed User
Longtime User

Gerrard

Member
Licensed User
Longtime User
Hi Johan
I am trying zxScannerLiveView and get the following run time error:
B4X:
Installing file.
PackageAdded: package:JHS.zxScannerLiveView
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
java.lang.RuntimeException: startPreview failed
    at android.hardware.Camera.startPreview(Native Method)
    at eu.livotov.labs.android.camview.camera.v1.DefaultCameraV1Controller.startPreview(DefaultCameraV1Controller.java:141)
    at eu.livotov.labs.android.camview.CameraLiveView.setCamera(CameraLiveView.java:188)
    at eu.livotov.labs.android.camview.CameraLiveView$1.onOperationCompleted(CameraLiveView.java:117)
    at eu.livotov.labs.android.camview.camera.v1.DefaultCameraV1Controller$5.onPostExecute(DefaultCameraV1Controller.java:328)
    at eu.livotov.labs.android.camview.camera.CAMViewAsyncTask$Handler.handleMessage(CAMViewAsyncTask.java:231)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:3687)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
    at dalvik.system.NativeStart.main(Native Method)

What do you suggest to fix the problem.
Regards
Gerrard
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi Johan
I am trying zxScannerLiveView and get the following run time error:
B4X:
Installing file.
PackageAdded: package:JHS.zxScannerLiveView
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
java.lang.RuntimeException: startPreview failed
    at android.hardware.Camera.startPreview(Native Method)
    at eu.livotov.labs.android.camview.camera.v1.DefaultCameraV1Controller.startPreview(DefaultCameraV1Controller.java:141)
    at eu.livotov.labs.android.camview.CameraLiveView.setCamera(CameraLiveView.java:188)
    at eu.livotov.labs.android.camview.CameraLiveView$1.onOperationCompleted(CameraLiveView.java:117)
    at eu.livotov.labs.android.camview.camera.v1.DefaultCameraV1Controller$5.onPostExecute(DefaultCameraV1Controller.java:328)
    at eu.livotov.labs.android.camview.camera.CAMViewAsyncTask$Handler.handleMessage(CAMViewAsyncTask.java:231)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:3687)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
    at dalvik.system.NativeStart.main(Native Method)

What do you suggest to fix the problem.
Regards
Gerrard
Gerrard, there are numerous version of the project in this thread each with new library files that adds some additional functionality to the library in post #1. What post # is the project and library that you are trying to test? Or did you start your own project?
 

CaStar

Member
Licensed User
Longtime User
Thanks Johan!
Unfortunately it does not work, I think that the camera is simply too weak (2MB pixels), the bar code is not sharp enough. :(
The same problem here on my Samsung J1. Seems not to be a camera problem but a problem of focusing. ABZxing works without problems...
 

Johan Schoeman

Expert
Licensed User
Longtime User
The same problem here on my Samsung J1. Seems not to be a camera problem but a problem of focusing. ABZxing works without problems...
There are now a number of barcode scanning libraries available on the forum some of which are here (@DonManfred has also posted some):

Barcodes and Barcode Scanners
7. ZXingLib(integration version) by icefairy333 - modified by Johan Schoeman (QR Codes and Barcodes) - follow the thread
21. QR Code Library - follow the thread (it also creates other 2D and 1D barcodes - no internet connection required)
23. QRCP3 & QRCP4 - create colored QR codes and scan 1D & 2D codes
24. B4A code for creating QR Codes (version 1 to 40), Aztec Codes (layer 1 to 11), PDF417, 2 of 5 Interleaved, EAN-13, EAN-8, Code 128, Code 39, Code 93 and Code 11 - no internet connection required
51. Barcode Scanner - 100% embedded within B4A
57. QRCodeReaderView - QR Code Scanner that is also 100% embedded in B4A
78. 1D and 2D Barcode Scanner with ZXING - another Barcode Scanner that is 100% embedded in B4A
79. 1D and 2D Barcode Scanner with ZBAR - another Barcode Scanner that is 100% embedded in B4A
92. QREader - scan 1D and 2D barcodes (QR Code, PDF 417, Aztec, etc) making use of Google Mobile Vision API
93. Android Vision Barcode Reader (QR Codes and other 1D & 2D barcodes)
105. android-quick-response-code - Another 1D & 2D barcode scanner
117. ZXING - A wrap for the Zxing official project as it is on 10 July 2016

Use whatever works for you. Some are 100% embedded in B4A while others depend on starting an external activity via B4A.

And if you want, I will post the Java code as it stands at present and you can change it to do whatever you would like it to do.
 
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User

Gerrard

Member
Licensed User
Longtime User
Johan, In my reply #111, I used the b4aZXScannerLiveView.zip code file, and the ZXScannerLiveViewLibFiles.zip library files from your post #1.
I have now used the zxScannerLiveViewB4ALibFiles.zip from your post #105. With this I get the following compile time error:

adb.exe - Entry Point Not Found
The procedure entry point WSAPoll could not be located in the dynamic link library WS2_32.dll.

Hope you can suggest a solution. Regards Gerrard
 

Johan Schoeman

Expert
Licensed User
Longtime User
Johan, In my reply #111, I used the b4aZXScannerLiveView.zip code file, and the ZXScannerLiveViewLibFiles.zip library files from your post #1.
I have now used the zxScannerLiveViewB4ALibFiles.zip from your post #105. With this I get the following compile time error:

adb.exe - Entry Point Not Found
The procedure entry point WSAPoll could not be located in the dynamic link library WS2_32.dll.

Hope you can suggest a solution. Regards Gerrard
Did you download core-3.1.0.jar and android-support-v4.jar from the link provided in post #1 and copied them to your additional library folder?
 

Gerrard

Member
Licensed User
Longtime User
Yes I did download and copy core-3.1.0.jar and android-support-v4.jar to the additional libraries folder
 

Johan Schoeman

Expert
Licensed User
Longtime User
Johan, In my reply #111, I used the b4aZXScannerLiveView.zip code file, and the ZXScannerLiveViewLibFiles.zip library files from your post #1.
I have now used the zxScannerLiveViewB4ALibFiles.zip from your post #105. With this I get the following compile time error:

adb.exe - Entry Point Not Found
The procedure entry point WSAPoll could not be located in the dynamic link library WS2_32.dll.

Hope you can suggest a solution. Regards Gerrard
What device are you running this on? I have no idea what those errors are...
 
Top