B4A Library QRCodeReaderView - QR Code Scanner that is also 100% embedded in B4A (new B4A lib files in post #8)

Here is another scanner that is 100% embedded in B4A. It wraps this Github project and will only scan QR Codes (it uses the ZXING engine for decoding scanned QR Codes). It will keep on reading QR codes for as long as what it finds a valid QR code. It is quite a "speedy" scanner. It does not require any other barcode scanner app to be installed on your device - it is 100% standalone. You can customize your B4A activity / UI by adding buttons, labels, panels, images, etc etc and change colors to your liking - 100% B4A customizable.

Note that a panel is the parent of the custom view but seeing that the original project extends Surface View it will punch a hole in the panel when the scanner starts.

Also note the permissions in the B4A project's manifest file (I know majority are redundant for this project).

You will have to handle consecutive / continuous scans of the same QR code within your B4A code.

Posting the following:
1. B4A project
2. B4A library files (3 x jar and 1 x xml) - copy them to your additional library folder.

EDIT: UPDATED LIBRARY FILES IN POST #6 THAT WILL NOT RAISE AN EVENT IN B4A IF TWO CONSECUTIVE SCANS OF THE SAME QR CODE OCCURS

1.png



Scanner.png



2.png


Some sample code:

B4X:
Region  Project Attributes
    #ApplicationLabel: QRCodeReaderView
    #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 qrcrv As QRCodeReaderView

    Private b1 As Button
    Private b2 As Button
    Private p1 As Panel
    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")

    qrcrv.Visible = False


End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub b1_Click


    qrcrv.Visible = True
    DoEvents
    qrcrv.startScan

End Sub
Sub b2_Click

    qrcrv.stopScan
    DoEvents
    qrcrv.Visible = False

End Sub

Sub qrcrv_result_found(retval As String)

    Log("B4A: " & retval)
    l1.Text = retval

End Sub


The library:

QRCodeReaderView
Author:
Github: David L\u00e1zaro, Wrapped by: Johan Schoeman
Version: 1
QRCodeReaderView
Events:

  • result_found (retval 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)
  • startScan
  • stopScan
Properties:
  • Background As Drawable
  • Color As Int [write only]
  • Enabled As Boolean
  • Height As Int
  • Left As Int
  • 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

  • QRCodeReaderViewLibFiles.zip
    498.1 KB · Views: 3,533
  • b4aQRCodeReaderView.zip
    8.9 KB · Views: 3,129
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
On request - posting only the updated B4A library files (QRCodeReaderView.jar and QRCodeReaderView.xml). It will generate a beep sound when a successful scan occurred (although it sometime sounds like a machine gun due to the rate of successful scans taking place).
 

Attachments

  • QRCodeReaderViewLibFilesSoundAdded.zip
    35 KB · Views: 1,122
Last edited:

Mahares

Expert
Licensed User
Longtime User
This is another amazing library courtesy JHS. It scans QRCodes very fast. In fact, I installed the sample on a tablet with poor camera focus and it read the QRCode with ease.
 

Johan Schoeman

Expert
Licensed User
Longtime User
Change the B4A code to the code below if you want to "eliminate" consecutive scan results of the same QR Code:

B4X:
#Region  Project Attributes
    #ApplicationLabel: QRCodeReaderView
    #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 qrcrv As QRCodeReaderView
 
    Private b1 As Button
    Private b2 As Button
    Private p1 As Panel
    Private l1 As Label
    Private oldString As String = ""
 
    Dim bm As Bitmap
 
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")
 
    qrcrv.Visible = False
    bm.Initialize(File.DirAssets, "red_line.png")
    qrcrv.SetBackgroundImage(bm)
 
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub b1_Click
 
    qrcrv.Visible = True
    DoEvents
    qrcrv.startScan
 
End Sub
Sub b2_Click
 
    qrcrv.stopScan
    DoEvents
    qrcrv.Visible = False

End Sub

Sub qrcrv_result_found(retval As String)
    If oldString <> retval Then
      oldString = retval
      Log("B4A: " & oldString)
      l1.Text = oldString
    End If
 
End Sub

it keeps track of the previous scan result.
 

Johan Schoeman

Expert
Licensed User
Longtime User
On request - the attached library will not raise an event in B4A if two consecutive scans of (a) QR code(s) have the same scan value. Thus, if you scan a QR code you cannot scan it immediately as the next scan - you need to first scan a different QR code. Beep sound also adjusted accordingly so that it will only beep if the next code that you scan is different from the previous one that you scanned. No changes required to the B4A code (all internal to the library).

Only posting the B4A library files (the other two jar files are still as is - they are included in the zip file in post #1 i.e jhscore.jar and android-support-annotations.jar)
 

Attachments

  • QRCodeReaderViewLibFiles.zip
    35.1 KB · Views: 958
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
Posting new library files. Only the B4A lib files. The other two (jhscore.jar and android-support-annotations.jar) are still as per the lib files zipped in post #1. The new lib files will allow you to scan the same QR Code if you have started the scanner, scanned a code, stopped the scanner, and then restart the scanner. It was not the case before. It still limits you from scanning the same QR Code consecutively without scanning a different QR Code in between while the scanner is active (as per amendments in post #6 above).

I needed the change for the Protected QR Code example that I have posted in post #2 of this thread (CLICK HERE).
 

Attachments

  • QRCodeReaderViewLibFiles.zip
    35.1 KB · Views: 922

JTmartins

Active Member
Licensed User
Longtime User
Argggg...No front Cam...

Love this lib, but I always need the front cam in my project...chuif.

If you have some extra time Master Johan...

JM
 

Johan Schoeman

Expert
Licensed User
Longtime User
Argggg...No front Cam...

Love this lib, but I always need the front cam in my project...chuif.

If you have some extra time Master Johan...

JM
Will look into it JT. Original project chooses back camera automatically if only the back or a front and back camera are present. It only chooses the front camera if the front camera is the only camera on a device. Will add some code to the wrapper and original project to allow the option of selecting the camera to use.
 

JTmartins

Active Member
Licensed User
Longtime User
Many thanks. That would make this a complete library.

We can include QRcodes inside a B4A activity and choose whatever camera the project needs.

+10
 

Johan Schoeman

Expert
Licensed User
Longtime User
Many thanks. That would make this a complete library.

We can include QRcodes inside a B4A activity and choose whatever camera the project needs.

+10
Added the option to use either the front or the back camera. Posting a sample project and updated library files. Copy the library files to your additional library folder. See comments inside the code about the help on the methods and variables that have been added (popup help added when entering methods is B4A project).

Sample code:

B4X:
#Region  Project Attributes
    #ApplicationLabel: QRCodeReaderView
    #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 qrcrv As QRCodeReaderView
   
    Private b1 As Button
    Private b2 As Button
    Private p1 As Panel
    Private l1 As Label
    Private oldString 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")
   
    qrcrv.Visible = False

    'Keep the order of the next 4 lines. CAMERA_BACK and CAMERA_FRONT will only contain correct values once qrcrv.BackFacingCamera and qrcrv.FrontFacingCamera has
    'been executed else they will have default values of -1
    Log(qrcrv.BackFacingCamera)    'see the popup help when entering this method - check if there is a back facing camera and return the ID of the camera
    Log(qrcrv.FrontFacingCamera)   'see the popup help when entering this method - check if there is a front facing camera and retutn the ID of the camera
   
    Log(qrcrv.CAMERA_BACK)         'see the popup help when entering this method
    Log(qrcrv.CAMERA_FRONT)           'see the popup help when entering this method
   
    'now you can use
    qrcrv.CameraId = qrcrv.CAMERA_BACK   'or qrcrv.CameraId = qrcrv.CAMERA_BACK
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub b1_Click
   
    qrcrv.Visible = True
    DoEvents
    qrcrv.startScan
   
End Sub
Sub b2_Click
   
    qrcrv.stopScan
    DoEvents
    qrcrv.Visible = False

End Sub

Sub qrcrv_result_found(retval As String)
    If oldString <> retval Then
      oldString = retval
      Log("B4A: " & oldString)
      l1.Text = oldString
    End If
   
End Sub


Library:
QRCodeReaderView
Author:
Github: David Lazaro, Wrapped by: Johan Schoeman
Version: 1
  • QRCodeReaderView
    Events:
    • result_found (retval As String)
    Fields:
    • CAMERA_BACK As Int
      The ID of the back facing camera
      If the ID is -1 after initialization then the device does not support a back facing camera
      If the device has front and back cameras then the ID's will be BACK = 0 and FRONT = 1 after initialization
      If the device has a back camera only then the ID's will be BACK = 0 and FRONT = -1 after initialization
      If the device has a front camera only then the ID's will be BACK = -1 and FRONT = 0 after initialization
    • CAMERA_FRONT As Int
      The ID of the front facing camera
      If the ID is -1 after initialization then the device does not support a front facing camera
      If the device has front and back cameras then the ID's will be BACK = 0 and FRONT = 1 after initialization
      If the device has a back camera only then the ID's will be BACK = 0 and FRONT = -1 after initialization
      If the device has a front camera only then the ID's will be BACK = -1 and FRONT = 0 after initialization
    • 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)
    • startScan
    • stopScan
    Properties:
    • BackFacingCamera As Int [read only]
      Get the ID of the back facing camera
      If the device has a front and back camera then the ID = 0
      If the device has a back facing camera only then the ID = 0
      It will return the ID as -1 if there is no back facing camera
    • Background As Drawable
    • CameraId As Int [write only]
    • Color As Int [write only]
    • Enabled As Boolean
    • FrontFacingCamera As Int [read only]
      Get the ID of the front facing camera
      If the device has a front and back camera then the ID = 1
      If the device has a front facing camera only then the ID = 0
      It will return the ID as -1 if there is no front facing camera
    • Height As Int
    • Left As Int
    • Tag As Object
    • Top As Int
    • Visible As Boolean
    • Width As Int
 

Attachments

  • b4aQRCodeReaderViewFrontBack.zip
    9.6 KB · Views: 763
  • QRCodeReaderViewNewLibFilesFrontBack.zip
    494.6 KB · Views: 883

JTmartins

Active Member
Licensed User
Longtime User
gonna play with it.
your work in behalf of the B4A community is amazing.

***UPDATE

Tested..Top notch
 
Last edited:

purzelonline

New Member
Licensed User
Longtime User
HI Johan !

Thks again for this library - perfect ! Donate is done :)
I tried to combine with photo light in lib fiddlearound - but problem with camera module connection. Any idea or library workaround to be able
to switch camera light ? My use of QR reader is to setup lights and when one broke and is to be changed the room can be a little dark
during install of a new ...

thks in advance
Rainer
 

Johan Schoeman

Expert
Licensed User
Longtime User
HI Johan !

Thks again for this library - perfect ! Donate is done :)
I tried to combine with photo light in lib fiddlearound - but problem with camera module connection. Any idea or library workaround to be able
to switch camera light ? My use of QR reader is to setup lights and when one broke and is to be changed the room can be a little dark
during install of a new ...

thks in advance
Rainer
I will see if I can add a torch to it and then post updated library files. Thanks for the donation. Much appreciated. ;)

If you follow these threads - the torch is already implemented in these postings

https://www.b4x.com/android/forum/t...de-scanner-that-is-100-embedded-in-b4a.63764/

and

https://www.b4x.com/android/forum/t...de-scanner-that-is-100-embedded-in-b4a.63794/
 

Johan Schoeman

Expert
Licensed User
Longtime User
HI Johan !

Thks again for this library - perfect ! Donate is done :)
I tried to combine with photo light in lib fiddlearound - but problem with camera module connection. Any idea or library workaround to be able
to switch camera light ? My use of QR reader is to setup lights and when one broke and is to be changed the room can be a little dark
during install of a new ...

thks in advance
Rainer
The attached B4A library files should handle the torch. Only posting the new B4A library files and a B4A project for demonstration purposes (jhscore.jar and android-support-annotations.jar remain unchanged - they should already be in your additional library folder from the lib files posted in post #1 of this thread)

Click on the "Flash" button when the scanner is active to toggle the flash/torch between On/Off

3.png


Sample code:

B4X:
#Region  Project Attributes
    #ApplicationLabel: QRCodeReaderView
    #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 qrcrv As QRCodeReaderView
   
    Private b1 As Button
    Private b2 As Button
    Private p1 As Panel
    Private l1 As Label
    Private oldString As String = ""
   
    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")
   
    qrcrv.Visible = False

    'Keep the order of the next 4 lines. CAMERA_BACK and CAMERA_FRONT will only contain correct values once qrcrv.BackFacingCamera and qrcrv.FrontFacingCamera has
    'been executed else they will have default values of -1
    Log(qrcrv.BackFacingCamera)    'see the popup help when entering this method - check if there is a back facing camera and return the ID of the camera
    Log(qrcrv.FrontFacingCamera)   'see the popup help when entering this method - check if there is a front facing camera and retutn the ID of the camera
   
    Log(qrcrv.CAMERA_BACK)         'see the popup help when entering this method
    Log(qrcrv.CAMERA_FRONT)           'see the popup help when entering this method
   
    'now you can use
    qrcrv.CameraId = qrcrv.CAMERA_BACK   'or qrcrv.CameraId = qrcrv.CAMERA_FRONT
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub b1_Click
   
    qrcrv.Visible = True
    DoEvents
    qrcrv.startScan
   
End Sub
Sub b2_Click
   
    qrcrv.stopScan
    DoEvents
    qrcrv.Visible = False

End Sub

Sub qrcrv_result_found(retval As String)
    If oldString <> retval Then
      oldString = retval
      Log("B4A: " & oldString)
      l1.Text = oldString
    End If
   
End Sub

Sub b3_Click
   
    If (qrcrv.isFlashOn = True) Then
      qrcrv.setFlashOff
    Else
      qrcrv.setFlashOn
    End If 
   
End Sub
 

Attachments

  • QRCodeReaderViewTorchLibFiles.zip
    32.1 KB · Views: 968
  • b4aQRCodeReaderViewTorch.zip
    9.7 KB · Views: 848
Top