B4A Library Another B4A Barcode Scanner - based on Android Vision

Sure you will figure it out - check the B4A log when scanning a barcode

Copy the .jar and .xml files to your B4A additional libs folder.

B4A code:

B4X:
#Region  Project Attributes
    #ApplicationLabel: jhsScanOnly
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
'    #MultiDex: True

#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 bs1 As BillScanner
    Private Button1 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")
    
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
    
      bs1.stopScanning
      
End Sub

Sub bs1_scan_value(val As String, rawbyte() As Byte, barcodetype As Int)
    
    Log("Value = " & val)
    Log("type = " & barcodetype)
    Log(" ")
    
End Sub

Sub Button1_Click
    
    bs1.startScanning
    
End Sub

I guess you know how to make this "RuntimePermissions" compliant.

Take note of this in the B4A Manifest file:

B4X:
AddApplicationText(<meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.gms.vision.DEPENDENCIES"
            android:value="barcode" />)

This is the scanner that I am using here.....


Zxing does not work - this scanner (based on Android Vision) does the job.

Enjoy.....;)
 

Attachments

  • BillScanner.xml
    9.3 KB · Views: 764
  • BillScanner.jar
    4.4 KB · Views: 793
  • jhsScanOnly.zip
    9.9 KB · Views: 861

Zlgo

Member
Hi Johan
I just try your demo but in compiling receive massage : " Maven artifact not found: androidx.annotation/annotation-experimental". B4A version 10.2 ,Java version 8.
 

Zlgo

Member
Thanks, this problem is solved, but now compiler report long list of errors like:

Java Version: 8
Parsing code. (0.00s)
Building folders structure. (0.01s)
Compiling code. (0.02s)
Compiling layouts code. (0.00s)
Organizing libraries. (4.60s)
(AndroidX SDK)
Generating R file. (2.02s)
Compiling debugger engine code. (0.81s)
Compiling generated Java code. (2.13s)
Convert byte code - optimized dex. Error
trouble writing output: Too many field references to fit in one dex file: 74723; max is 65536.
You may try using multi-dex. If multi-dex is enabled then the list of classes for the main dex list is too large.
References by package:
2 android.accounts
27 android.app
1 android.content
33 android.content.pm
17 android.content.res
3 android.database
111 android.graphics
4 android.hardware
2 android.media
1 android.media.browse
2 android.net
21 android.os
3 android.print
6 android.support.v4.app
210 android.support.v4.media
380 android.support.v4.media.session
12 android.support.v4.os
1 android.system
16 android.text
1 android.text.util
2 android.transition
15 android.util
29 android.view
15 android.view.accessibility
3 android.view.inputmethod
1 android.view.textclassifier
19 android.widget
29 androidx.activity
22 androidx.annotation
3 androidx.annotation.experimental
9 androidx.annotation.experimental.lint
3682 androidx.appcompat
392 androidx.appcompat.app
 

Johan Schoeman

Expert
Licensed User
Longtime User
Thanks, this problem is solved, but now compiler report long list of errors like:

Java Version: 8
Parsing code. (0.00s)
Building folders structure. (0.01s)
Compiling code. (0.02s)
Compiling layouts code. (0.00s)
Organizing libraries. (4.60s)
(AndroidX SDK)
Generating R file. (2.02s)
Compiling debugger engine code. (0.81s)
Compiling generated Java code. (2.13s)
Convert byte code - optimized dex. Error
trouble writing output: Too many field references to fit in one dex file: 74723; max is 65536.
You may try using multi-dex. If multi-dex is enabled then the list of classes for the main dex list is too large.
References by package:
2 android.accounts
27 android.app
1 android.content
33 android.content.pm
17 android.content.res
3 android.database
111 android.graphics
4 android.hardware
2 android.media
1 android.media.browse
2 android.net
21 android.os
3 android.print
6 android.support.v4.app
210 android.support.v4.media
380 android.support.v4.media.session
12 android.support.v4.os
1 android.system
16 android.text
1 android.text.util
2 android.transition
15 android.util
29 android.view
15 android.view.accessibility
3 android.view.inputmethod
1 android.view.textclassifier
19 android.widget
29 androidx.activity
22 androidx.annotation
3 androidx.annotation.experimental
9 androidx.annotation.experimental.lint
3682 androidx.appcompat
392 androidx.appcompat.app
Look for:
# Mulitdex: True
 

Zlgo

Member
Look for:
# Mulitdex: True
Bingo, this option was marked ('), but also need this Runtimepermision to add in code (otherwise app crash)
B4X:
rp.CheckAndRequest(rp.PERMISSION_CAMERA)
    Wait For Activity_PermissionResult (Permission As String, ResultPermission As Boolean)
Now it works pretty good, but is there option to choose wich barcode only to read?
 

Johan Schoeman

Expert
Licensed User
Longtime User
Try with the attached library files and sample project:

1602165059201.png
 

Attachments

  • BillScanner.xml
    11.8 KB · Views: 426
  • BillScanner.jar
    4.8 KB · Views: 431
  • jhsScanOnly.zip
    9.9 KB · Views: 420

Xfood

Expert
Licensed User
good afternoon,
I'm trying this fantastic bookcase,
I ran that I read the reading twice, why?

B4X:
Sub Globals
    Private bs1 As BillScanner
    Private Button1 As Button
    Private letture As Int    
End Sub
Sub bs1_scan_value(val As String, rawbyte() As Byte, barcodetype As Int)
    bs1.stopScanning
    bs1.Enabled=False
    bs1.Visible=False
    letture=letture+1
    ToastMessageShow("letture := " & letture,False)  '2'
    

    Log("Value = " & val)
    Log("type = " & barcodetype)
    Log(" ")
    
    ToastMessageShow("Value = " & val,False)
    ToastMessageShow("type = " & barcodetype,False)

End Sub

Sub Button1_Click
    letture = 0
    bs1.Enabled=True
    bs1.Visible=True
    bs1.startScanning
End Sub
 

Johan Schoeman

Expert
Licensed User
Longtime User
good afternoon,
I'm trying this fantastic bookcase,
I ran that I read the reading twice, why?

Not understanding what you are trying to say....? What is the problem that you are experiencing?
 

Johan Schoeman

Expert
Licensed User
Longtime User
the code is read twice, even if the result stops reading, why?
Add some code in your B4A project (bs1_scan_value(val As String, barcodetype As Int)) to check if the new value is the same as the previous value and if so then decide what you want to do with the new value (keep it or discard it).

I don't understand what you mean by "even if the result stops reading". If the scanner does not see a barcode it will stop scanning and nothing will be logged in the IDE Logs
 

Xfood

Expert
Licensed User
sorry, maybe I can't explain well. 1) I press the scan button, read the code and stop reading all the functions contained in
bs1_scan_value(val As String, barcodetype As Int)
are executed twice, as if you had read the code twice.
 

Johan Schoeman

Expert
Licensed User
Longtime User
sorry, maybe I can't explain well. 1) I press the scan button, read the code and stop reading all the functions contained in
bs1_scan_value(val As String, barcodetype As Int)
are executed twice, as if you had read the code twice.
B4X:
#Region  Project Attributes
    #ApplicationLabel: jhsScanOnly
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
    #MultiDex: True

#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 bs1 As BillScanner
    Private Button1 As Button
    
    Dim prevVal As String = ""

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")
'    bs1.ScanMode = bs1.FORMAT_ONE_D
    
    
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
    
      bs1.stopScanning
      
End Sub

Sub bs1_scan_value(val As String, barcodetype As Int)
    
    If val <> prevVal Then
        Log("Value = " & val)
        Log("type = " & barcodetype)
        Log(" ")
        prevVal = val
    End If   
    
End Sub

Sub Button1_Click
    
    bs1.startScanning
    
End Sub

If the previous scan is not the same as the new scan then the scan will be accepted else it will be discarded. If more than one barcode is "visible" it might scan the barcodes repetitively. Handle it in you B4A project in Sub bs1_scan_value(val As String, barcodetype As Int)
 

Johan Schoeman

Expert
Licensed User
Longtime User
should perform this sub, 1 time only
No, the library will return all scans for as long as what the scanner is active. Handle the scan result in your B4A project. Really very simple to do.
 

Xfood

Expert
Licensed User
i don't think it's okay, if i read code 123, and then 555 ok, but if i read 123 and after again 123 it doesn't execute the function. It is strange that the function does not exit after reading. thanks anyway
 

Johan Schoeman

Expert
Licensed User
Longtime User
i don't think it's okay, if i read code 123, and then 555 ok, but if i read 123 and after again 123 it doesn't execute the function. It is strange that the function does not exit after reading. thanks anyway
Call bs1.stopScanning in Sub bs1_scan_value(val As String, barcodetype As Int) when you got a result and you want the scanner to stop scanning.
 
Top