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

The attached project wraps the ZXING part of this Github project. I have not tested all the barcode formats but it should scan the following types:

BarcodeFormat.UPC_A
BarcodeFormat.UPC_E
BarcodeFormat.EAN_13
BarcodeFormat.EAN_8
BarcodeFormat.RSS_14
BarcodeFormat.CODE_39
BarcodeFormat.CODE_93
BarcodeFormat.CODE_128
BarcodeFormat.ITF
BarcodeFormat.CODABAR
BarcodeFormat.QR_CODE
BarcodeFormat.DATA_MATRIX
BarcodeFormat.PDF_417

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
2. B4A library files - copy them to your additional library folder
3. You also need android-support.v4.jar and core-3.2.1.jar to be in your additional library folder. I have zipped them together and you can download them from this link:
https://www.dropbox.com/s/h9ts6cjfoo5et6h/core-3.2.1.zip?dl=0

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

B4X:
zx1.Left = 2%x
zx1.Top = 5%y
zx1.Width = 96%x
zx1.Height = 80%y

b1.Left = 2%x
b1.Top = zx1.Bottom + 2%y
b1.Width = 15%x
b1.Height = 10%y

b2.Left = 45%x
b2.Top = zx1.Bottom + 2%y
b2.Width = 15%x
b2.Height = 10%y

b3.Left = 83%x
b3.Top = zx1.Bottom + 2%y
b3.Width = 15%x
b3.Height = 10%y

1.png



2.png

Sample code:
B4X:
#Region  Project Attributes
    #ApplicationLabel: ZxingBarcodeScanner
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #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 zx1 As ZxingBarcodeScanner
    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")

    zx1.LaserColor = Colors.Yellow
    zx1.MaskColor = Colors.ARGB(150, 0, 0, 200)
    zx1.BorderColor = Colors.Magenta
    zx1.BorderStrokeWidth = 5
    zx1.BorderLineLength = 40
    zx1.Visible = False

End Sub

Sub Activity_Resume


End Sub

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

End Sub


Sub b1_Click

    zx1.toggleFlash

End Sub

Sub b2_Click

    zx1.Visible = True
    zx1.startScanner

End Sub

Sub b3_Click

    zx1.Visible = False
    zx1.stopScanner

End Sub

Sub zx1_scan_result (scantext As String, scanformat As String)

    Log ("B4A scan text = " & scantext)
    Log ("B4A scan format = " & scanformat)


End Sub

The library:

ZxingBarcodeScanner
Author:
Github: Dushyanth Maguluru, Wrapped by: Johan Schoeman
Version: 1
ZxingBarcodeScanner
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
 

Attachments

  • ZxingBarcodeScannerLibFiles.zip
    22.5 KB · Views: 2,670
  • b4aZxingBarcodeScanner.zip
    9.9 KB · Views: 2,534
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
This is an update for this library. It will allow you to automatically scan normal color barcodes (ie white background with black modules) or inverted color barcodes (ie black background with white modules). All done within the library so no additional code required in the B4A project.

See post #1 (point 3) of this thread for the other library files that are required.

You can download and test it but if you want to use it you need to

3.png


4.png
 

Attachments

  • b4aZxingBarcodeScanner07052016.zip
    11.3 KB · Views: 658
  • b4aZxingBarcodeScannerLibFiles.zip
    29.7 KB · Views: 716

nhz

Member
Licensed User
Longtime User
Hi, any way to trigger the sound effect for successful scan?
 

iStef

Member
Licensed User
Hi, I am having problems with this library and Android 7. Everything works fine on Android 5 and 6, but on Android 7 my app can't detect any barcode. The image looks bad (probably it's in low resolution or something similar) and barcode is not read. Any hint? Thanks in advance.
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi Johan, that library works fine on Android 7. Since I'd prefer to use this one (I mean, the one you posted in this thread), do you think we have a chance to make it working on Android 7 too? Thanks for your help.

I will look into it and try to establish how the two wraps differ in terms of setting the focus. I do know that each of them has their own CameraManager code and I will have to investigate. If I do find a solution I will post it - but will need some time. We have had a massive rainstorm in Durban earlier today and my factory is severely impacted due to wind and rain damage. So it gets highest priority at present....
 

iStef

Member
Licensed User
I will look into it and try to establish how the two wraps differ in terms of setting the focus. I do know that each of them has their own CameraManager code and I will have to investigate. If I do find a solution I will post it - but will need some time. We have had a massive rainstorm in Durban earlier today and my factory is severely impacted due to wind and rain damage. So it gets highest priority at present....

Take your time Johan. I hope everything will be fine in Durban soon. Please let me know when you find a solution or even if you don't/can't find it. I appreciate your help, thank you.
 

hongbii khaw

Member
Licensed User
Longtime User
Hi,
What can i do if i get this error?

B4X:
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Connected
LAN
android.content.res.Resources$NotFoundException: Resource ID #0x0
    at android.content.res.Resources.getValue(Resources.java:1369)
    at android.content.res.MiuiResources.getValue(MiuiResources.java:145)
    at android.content.res.Resources.getColor(Resources.java:981)
    at android.content.res.Resources.getColor(Resources.java:954)
    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:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5438)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi,
What can i do if i get this error?

B4X:
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Connected
LAN
android.content.res.Resources$NotFoundException: Resource ID #0x0
    at android.content.res.Resources.getValue(Resources.java:1369)
    at android.content.res.MiuiResources.getValue(MiuiResources.java:145)
    at android.content.res.Resources.getColor(Resources.java:981)
    at android.content.res.Resources.getColor(Resources.java:954)
    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:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5438)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
Take note of what I have posted in post #1 of this thread....the files need to be set to READ ONLY else they will be deleted when you compile your B4A project.
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.
 

iStef

Member
Licensed User
Take note of what I have posted in post #1 of this thread....the files need to be set to READ ONLY else they will be deleted when you compile your B4A project.
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.

Hi Johan, any news about compatibility to Android 7?
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi Johan, any news about compatibility to Android 7?
There are 5 focus modes in the library:
FOCUS_MODE_AUTO
FOCUS_MODE_CONTINUOUS_PICTURE
FOCUS_MODE_CONTINUOUS_VIDEO
FOCUS_MODE_MACRO
FOCUS_MODE_INFINITY

I have just tested it on an Android 7 device with.....
B4X:
zx1.AutoFocusInterval = 2000
zx1.FocusMode = zx1.FOCUS_MODE_AUTO
....and it seems to be working fine.
 

iStef

Member
Licensed User
I have just tested it on an Android 7 device with.....
B4X:
zx1.AutoFocusInterval = 2000
zx1.FocusMode = zx1.FOCUS_MODE_AUTO
....and it seems to be working fine.

Hi Johan,

I was already using FOCUS_MODE_AUTO. Now I also added AutoFocusInterval = 2000, but it still does not work (see attached image). Tested on two different Android 7 devices (Honor 8 and Huawei P9 Lite).

The same app works just fine on Android 6. Also, the other library works fine, but I don't like it very much and I'd prefer to use yours.

Do you have any other suggestion or test I could do to understand what the problem is?

Thank you for your help.
 

Attachments

  • NoFocus.jpg
    NoFocus.jpg
    39.2 KB · Views: 344

iStef

Member
Licensed User
Hi Johan, after further investigation and tests with several devices, it turned out that the problem is not Android 7 itself, but Android 7 running on Huawei or Honor devices. We haven't tested with any possible device of course, but so far the library worked fine on any Android 7 device except for Huawei or Honor (which, as you know, share the same interface, called EMUI). Notice the library worked fine on Huawei running Android 6.

Does this help you to better understand what could be the issue? Thank you.
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi Johan, after further investigation and tests with several devices, it turned out that the problem is not Android 7 itself, but Android 7 running on Huawei or Honor devices. We haven't tested with any possible device of course, but so far the library worked fine on any Android 7 device except for Huawei or Honor (which, as you know, share the same interface, called EMUI). Notice the library worked fine on Huawei running Android 6.

Does this help you to better understand what could be the issue? Thank you.
I will have to read up (Google) on the focus issues of those devices.
 

jaraiza

Active Member
Licensed User
Longtime User
Hi,

I've downloaded the different versions published in this post, and I've an issue...

If I use version 1.00 I have an annoying default sound when the image has been successfully scanned (there's no option to turn it off)
If I user version 1.04 the sound is removed, but I lose LowerLuxLimit, UpperLuxLimit, AutoTorchMode and AmbientLightLux.

Is there any version with the latest properties available, but no sound? (Or an option to disable it)

I have problems with some images without light management properties :(

Thanks!
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi,

I've downloaded the different versions published in this post, and I've an issue...

If I use version 1.00 I have an annoying default sound when the image has been successfully scanned (there's no option to turn it off)
If I user version 1.04 the sound is removed, but I lose LowerLuxLimit, UpperLuxLimit, AutoTorchMode and AmbientLightLux.

Is there any version with the latest properties available, but no sound? (Or an option to disable it)

I have problems with some images without light management properties :(

Thanks!
Last update of the lib and sample project is in post #41.
 
Top