B4A Library QREader - Scan 1D and 2D barcodes (QR Code, PDF 417, Aztec, etc)

This is a wrapper for this Github project. It is similar to @DonManfred 's posting here in the sense that it also makes use of the Google Mobile Vision API. @DonManfred 's version is however far more advanced. For me this was a learning exercise and I will therefore try and explain in detail how you should go about to set this up:

1. You will need the following libraries in your additional library folder (recent versions):
android-support-v4.jar
android-support-v7-appcompat.jar
google-play-services.jar​
2. Download and extract b4AQREader.zip (it is the B4A project)
3. Download and extract resource.zip. The folder in this zip file should be copied to the B4A project so that it is on the same folder level as the /Files and /Objects folders of the B4A project.
4. Download and extract QREaderLibFiles.zip and copy the jar and xml to your additional library folder
5. Download and extract resourceAdditionalLibs.zip. It contains a folder that is also named resource. Copy this folder into your additional library folder
6. Set up the correct paths in the B4A project to the following:
B4X:
#AdditionalRes: C:\Users\----------2\Documents\Basic 4 Android\JOHAN APPS\JHS LIBS\resource\b4a_appcompat, de.amberhome.objects.appcompat
#AdditionalRes: C:\ANDRIOD_SDK_TOOLS\extras\android\support\v7\appcompat\res, android.support.v7.appcompat
#AdditionalRes: C:\ANDRIOD_SDK_TOOLS\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms

Compile the B4A project and you should get this:
1.png


The library raises an event in the B4A project and passes on the decoded string to the B4A project (i.e the "message" in the barcode).

I am also posting the Java code for whoever would like to build on the project. in order to compile the Java code you need to create a libs folder on the same folder level as the src folder and copy the 3 jar files mentioned in (1) above to this folder.



Sample code:
B4X:
#Region  Project Attributes
    #ApplicationLabel: QREader
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#AdditionalRes: ..\resource

#AdditionalRes: C:\Users\----------2\Documents\Basic 4 Android\JOHAN APPS\JHS LIBS\resource\b4a_appcompat, de.amberhome.objects.appcompat
#AdditionalRes: C:\ANDRIOD_SDK_TOOLS\extras\android\support\v7\appcompat\res, android.support.v7.appcompat
#AdditionalRes: C:\ANDRIOD_SDK_TOOLS\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms

#ExcludeClasses: .games, .drive, .ads, .fitness, .wearable, .measurement, .cast, .auth, .nearby
#ExcludeClasses: .tagmanager, .analytics, .wallet, .plus, .gcm, .maps, .panorama

'#Extends: android.support.v7.app.ActionBarActivity
#Extends: android.support.v7.app.AppCompatActivity


#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.

    Dim myscanner As QREader

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")
    myscanner.Initialize("myscanner")

    myscanner.InitialScannerText = "Scan a 1D or 2D Barcode"

End Sub

Sub Activity_Resume

    myscanner.StartScan

End Sub

Sub Activity_Pause (UserClosed As Boolean)

    myscanner.StopScan

End Sub

Sub myscanner_scan_result(result As String)

    Log("result = " & result)

End Sub

Library as it stands at present:
QREader
Author:
Github: Nishant Srivastava, Wrapped by: Johan Schoeman
Version: 1

  • Fields:
    • camera_view As Int
    • code_info As Int

  • Fields:
    • activity_main As Int
  • QREader
    Events:
    • scan_result (result As String)
    Methods:
    • Initialize (paramString As String)
    • IsInitialized As Boolean
    • StartScan
    • StopScan
    Properties:
    • InitialScannerText As String [write only]
Important: take note of the various files in the /Objects/res/bla bla folders of the B4A project and make sure that they are set to READ ONLY before you compile the B4A project.

You should also take note of the B4A Manifest should you start a new project.
 

Attachments

  • b4aQREader.zip
    10 KB · Views: 342
  • QREaderLibFiles.zip
    9.2 KB · Views: 334
  • TheJavaCode.zip
    38.1 KB · Views: 277
  • resource.zip
    423 bytes · Views: 329
  • resourceAdditionalLibs.zip
    775 bytes · Views: 322
Last edited:

DonManfred

Expert
Licensed User
Longtime User
#AdditionalRes: C:\Users\----------2\Documents\Basic 4 Android\JOHAN APPS\JHS LIBS\resource\b4a_appcompat, de.amberhome.objects.appcompat
#AdditionalRes: C:\ANDRIOD_SDK_TOOLS\extras\android\support\v7\appcompat\res, android.support.v7.appcompat
#AdditionalRes: C:\ANDRIOD_SDK_TOOLS\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms
i suggest to change them to

B4X:
#AdditionalRes: $AdditionalLibs$\b4a_appcompat, de.amberhome.objects.appcompat
#AdditionalRes: $AndroidSDK$\extras\android\support\v7\appcompat\res, android.support.v7.appcompat
#AdditionalRes: $AndroidSDK$\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms
And place the folder b4a_appcompat in you additional libs folder.

This makes it easier for others to adapt the paths
 

Johan Schoeman

Expert
Licensed User
Longtime User
Only an update to allow for making use of either the front or the back camera. Read post #1 and follow all instructions (but now with the new B4A library files and the new B4A project).

The icons at the top right from left-to-right:
1. Start the scanner
2. Stop the scanner
3. Use the back facing camera (click on this before you click on the start scan icon or the start scan button
4. Use the front facing camera (click on this before you click on the start scan icon or the start scan button

Use...
B4X:
Dim backCamId As Int = myscanner.BackFacingCamera                  'is there a back facing camera? Read the popup help
Dim frontCamId As Int = myscanner.FrontFacingCamera                'is there a front facing camera? Read the popup help
...to determine the availability of front and back cameras. A short explanation:
myscanner.BackFacingCamera will be zero (0) if the device has a back camera only, a front camera only, or back and front facing cameras
myscanner.FrontFacingCamera will be one (1) if the device has a back and a front facing camera

Posting the updated B4A project, the Java Code as it stands at present, and the new B4A library files (again, follow post #1 and replace the B4A project and the B4A ,library files with the attached in this post)

You can set the text color and text size with...
B4X:
myscanner.ScannerTextColor = Colors.Red
myscanner.ScannerTextSize = 25

You can also use the "back button" of your device to stop the scanner and (again) to stop the app.

Click on the BIG "START SCAN" button or on the first icon from the left (top right) to start the scanner
1.png


QR Code scanned (it will also scan other 2D as well as 1D barcodes)...
2.png



Clicked the "back button" of the device....(click the "back button" again to quite the app - via a dialog)
3.png


If you want to change the size (width / height) of the scanner then do so by editing activity_main.xml in the /Object/res/layout folder of the B4A project. You must disable the READ ONLY property of this xml file, do the changes, save the xml file, and then set the property back to READ ONLY.

Library at present:

QREader
Author:
Github: Nishant Srivastava, Wrapped by: Johan Schoeman
Version: 1
  • QREader
    Events:
    • scan_result (result 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
    Methods:
    • Initialize (paramString As String)
    • IsInitialized As Boolean
    • StartScan
      Start the barcode scanner
    • StopScan
      Stop the barcode scanner
    • isScannerActive As Boolean
    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
    • CameraToUse As Int [write only]
      Set the camera to be use for recording
      For a device with front and back cameras ---> front = 1 and back = 0
      for a device with a front camera only ---> front = 0
      for a device with a back facing camera only ---> back = 0
      Use avc1.BackFacingCamera and avc1.FrontFacingCamera to get the camera ID's
    • 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
    • InitialScannerText As String [write only]
      Set the initial scanner text to be displayed (i.e before a barcode is scannned)
    • ScannerTextColor As Int [write only]
      Set the scanner text color
    • ScannerTextSize As Int [write only]
      Set the scanner text size

    Also note that in the Java Code I have replaced class CameraSource.java in the google-play-services.jar with a new / updated class (see the Java code)
Just my personal opinion of how I would rate the decoding capability of the various barcode decoding engines (highest to lowest):

1. Google Mobile Vision API
2. ZBAR
3. ZXing
 

Attachments

  • TheJavaCode.zip
    51.8 KB · Views: 209
  • QREaderLibFiles.zip
    26.7 KB · Views: 229
  • b4aQREader.zip
    131.4 KB · Views: 229
Last edited:

Erick Kalugdan

Member
Licensed User
How do I do this step? Help please as I cannot find these jar files in my android sdk folder :-(

1. You will need the following libraries in your additional library folder (recent versions):
android-support-v4.jar
android-support-v7-appcompat.jar
google-play-services.jar
 

Johan Schoeman

Expert
Licensed User
Longtime User

Erick Kalugdan

Member
Licensed User
I am getting the error:
ERROR: resource directory 'c:\andriod_sdk_tools\extras\google\google_play_services\libproject\google-play-services_lib\res' does not exist

And when I look at my folder, I only see
C:\Android\android-sdk\extras\google\google_play_services\docs
C:\Android\android-sdk\extras\google\google_play_services\samples

There is no libproject folder.

Here's my SDK Manager:
 

Attachments

  • Screen Shot 2016-05-28 at 6.15.33 PM.png
    Screen Shot 2016-05-28 at 6.15.33 PM.png
    94.7 KB · Views: 161

Johan Schoeman

Expert
Licensed User
Longtime User
I am getting the error:
ERROR: resource directory 'c:\andriod_sdk_tools\extras\google\google_play_services\libproject\google-play-services_lib\res' does not exist

And when I look at my folder, I only see
C:\Android\android-sdk\extras\google\google_play_services\docs
C:\Android\android-sdk\extras\google\google_play_services\samples

There is no libproject folder.

Here's my SDK Manager:
You need to set the following paths according to your own setup (the below is the paths to the setup in my computer). You can't copy mine - do you also have a folder called andriod and not android like I do? Very much doubt it.

#AdditionalRes: C:\Users\----------2\Documents\Basic 4 Android\JOHAN APPS\JHS LIBS\resource\b4a_appcompat, de.amberhome.objects.appcompat
#AdditionalRes: C:\ANDRIOD_SDK_TOOLS\extras\android\support\v7\appcompat\res, android.support.v7.appcompat
#AdditionalRes: C:\ANDRIOD_SDK_TOOLS\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms

The first line is the path to "my B4A additional library folder"\resource\b4a_appcompat - now set it up for yours

For the other two lines to take effect your Android SDK seems not to be up to date...You need to update your SDK. Browse the forum to find out how to do it and what you should thick to install when you start the SDK Manager (SDK Manager.exe)
 

akb

Member
Licensed User
Longtime User
I am struggling to get this working. I have installed Android Studio and what I think are the appropriate SDK's.
I would really like to test the Google Mobile Vision API for reading QR Codes to see how it performs against some other readers.
Is there any chance someone could post the APK for this project so I can test it, please?
 

Johan Schoeman

Expert
Licensed User
Longtime User
I am struggling to get this working. I have installed Android Studio and what I think are the appropriate SDK's.
I would really like to test the Google Mobile Vision API for reading QR Codes to see how it performs against some other readers.
Is there any chance someone could post the APK for this project so I can test it, please?
Will send you a link to the APK. Need to revisit this project to do so. Almost 21 months since I posted this project....
 

Johan Schoeman

Expert
Licensed User
Longtime User

Johan Schoeman

Expert
Licensed User
Longtime User
I come from China,becouse the firewall,I can't get the file from this URL,can you download it and send to me? My Email:guojs@yeah.net. Thank you!
Try with the attached - it works on B4A V8.30 and targetSDK set to 26. Not other downloads required except for the attached B4A project and lib files. The library should show as V1.01 in the B4A IDE

Extract resource.zip and copy the folder and its contents to be on the same folder level as that of the /Files and /Objects folders of the B4A project
 

Attachments

  • QREaderLibFilesV1.01.zip
    27.4 KB · Views: 216
  • b4aQREader.zip
    131.8 KB · Views: 194
  • resource.zip
    423 bytes · Views: 195
Last edited:

bjfhs

Active Member
Licensed User
Try with the attached - it works on B4A V8.30 and targetSDK set to 26. Not other downloads required except for the attached B4A project and lib files. The library should show as V1.01 in the B4A IDE
Thank you very much,I will test it.
 
Top