B4A Library ZXing-Standalone

A wrap for this Github project. Posting the following:
1. b4aZXingStandAlone.zip - the B4A project (see notes further down to make this project complete)
2. html-fr.zip - copy this zip files into the /Files folder of the B4A project and then extract it so that the files and folders in this zip is in the root of the B4A project's /Files folder
3. ZxingRes.zip - create a folder called ZXingRes on the same folder level as that of the B4A project's /Files and /Objects folder level. Then extract this zip file and copy the extracted contents into the ZXingRes folder that you have created.
4. core-3.2.1.zip - extract it and copy the jar to your additional library folder
5. ZXingStandAloneLibFiles.zip - extract it and copy the jar and xml to your additional library folder.
6. The Java code - in order to compile it you will need to create a libs folder on the same folder level as the src folder and copy core-3.2.1.jar into this folder

Notes:
a. Take note of the B4A project's Manifest file in case you start a new project from scratch
b. Use your device's volume up/down buttons to switch on/off the torch
c. Take note of this in the B4A code to reference the additional resources in point (3) above
B4X:
#AdditionalRes: ..\ZXingRes

Library:
ZXingStandAlone
Author:
Github: Tarun Mudgal; Wrapped by: Johan Schoeman
Version: 1
  • ZXingStandAlone
    Events:
    • result (barcodetype as String As , barcodetext as String As , image as Object As )
    Fields:
    • laserAlpha As Int[]
    • laserColor As Int
    • maskColor As Int
    • resultColor As Int
    • resultPointColor As Int
    Methods:
    • BeginScan (EventName As String)
    Permissions:
    • android.hardware.camera
    • android.hardware.camera.autofocus
    • android.permission.ACCESS_WIFI_STATE
    • android.permission.CAMERA
    • android.permission.CHANGE_WIFI_STATE
    • android.permission.FLASHLIGHT
    • android.permission.INTERNET
    • android.permission.READ_CONTACTS
    • android.permission.VIBRATE
    • android.permission.WRITE_EXTERNAL_STORAGE
    • com.android.browser.permission.READ_HISTORY_BOOKMARKS

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

#AdditionalRes: ..\ZXingRes

#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 Button1 As Button
    Dim zxalone As ZXingStandAlone

    Private ImageView1 As ImageView
    Private Label1 As Label
    Private Label2 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")

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub Button1_Click

    Dim laserAlpha() As Int = Array As Int (0, 64, 128, 192, 255, 192, 128, 64)
    zxalone.laserAlpha = laserAlpha
    zxalone.laserColor = Colors.Magenta
    zxalone.maskColor = Colors.ARGB(100,0,0,200)
    zxalone.resultColor = Colors.ARGB(100,100,0,100)
    zxalone.resultPointColor = Colors.white
    zxalone.BeginScan("zxalone")

End Sub

Sub zxalone_result(barcodetype As String, barcodetext As String, image As Object)

    ImageView1.Gravity = Gravity.FILL
    ImageView1.Visible = True
    ImageView1.BringToFront
    ImageView1.Color = Colors.Yellow
    Label1.Text = "Type: " & barcodetype
    Label2.Text = "Context: " & barcodetext

    ImageView1.Bitmap = image

End Sub

1.png


2.png


3.png


4.png
 

Attachments

  • b4aZXingStandAlone.zip
    10.1 KB · Views: 564
  • core-3.2.1.zip
    483.9 KB · Views: 565
  • html-fr.zip
    136.8 KB · Views: 463
  • ZxingRes.zip
    155.9 KB · Views: 526
  • ZXingStandAloneLibFiles.zip
    171.6 KB · Views: 564
  • TheJavaCode.zip
    138.8 KB · Views: 468
Last edited:

johndb

Active Member
Licensed User
Longtime User
This is indeed a very useful library ... fully integrated within the app. Well done @Johan Schoeman :)

Just one comment though. Although it is basic knowledge that you should include the statement "#AdditionalRes: ..\ZXingRes" in a new app, it may be helpful if you add that to your "Notes" comments in post #1. ;)

John
 

Johan Schoeman

Expert
Licensed User
Longtime User
This is indeed a very useful library ... fully integrated within the app. Well done @Johan Schoeman :)

Just one comment though. Although it is basic knowledge that you should include the statement "#AdditionalRes: ..\ZXingRes" in a new app, it may be helpful if you add that to your "Notes" comments in post #1. ;)

John
Thanks John - have added an additional comment in post #1 wrt "#AdditionalRes: ..\ZXingRes"
 

Johan Schoeman

Expert
Licensed User
Longtime User
This is an update for the library and B4A project. You can:
1. Move the laser up/down while scanning is active.
2. Set the result points to be either circles or squares
3. Set the prompt message, prompt message text color, and prompt message text size
4. Set the number of barcodes to scan before automatically returning to B4A

When the B4A activity resumes the barcodes scanned will be displayed in ImageView 1 to 5 (adapt for your purposes)

Library:
ZXingStandAlone
Author:
Github: Tarun Mudgal; Wrapped by: Johan Schoeman
Version: 1.03
  • ZXingStandAlone
    Events:
    • scan_result (barcodetype as String As , barcodetext as String As , barcode as Object As )
    Fields:
    • BACK_CAMERA As Int
    • FRONT_CAMERA As Int
    • laserAlpha As Int[]
    • laserColor As Int
    • maskColor As Int
    • resultColor As Int
    • resultPointColor As Int
    • resultpointshape_circle As Int
    • resultpointshape_square As Int
    Methods:
    • BeginScan (EventName As String)
    Permissions:
    • android.hardware.camera
    • android.hardware.camera.autofocus
    • android.permission.ACCESS_WIFI_STATE
    • android.permission.CAMERA
    • android.permission.CHANGE_WIFI_STATE
    • android.permission.FLASHLIGHT
    • android.permission.INTERNET
    • android.permission.READ_CONTACTS
    • android.permission.VIBRATE
    • android.permission.WRITE_EXTERNAL_STORAGE
    • com.android.browser.permission.READ_HISTORY_BOOKMARKS
    Properties:
    • CameraToUse As Int [write only]
    • CustomPromptMessage As String [write only]
    • CustomPromptMessageTextColor As Int [write only]
    • CustomPromptMessageTextSize As Int [write only]
    • MoveLaserUpDown As Boolean [write only]
    • NumberOfScans As Int [write only]
    • ResultPointShape As Int [write only]

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

#AdditionalRes: ..\ZXingRes

#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 Button1 As Button
    Dim zxalone As ZXingStandAlone

    Private ImageView1 As ImageView
    Private Label1 As Label
    Private Label2 As Label
    Private Spinner1 As Spinner
    Private ImageView2 As ImageView
    Private ImageView3 As ImageView
    Private ImageView4 As ImageView
    Private ImageView5 As ImageView


    Dim cntr As Int

    Private cb1 As CheckBox
    Private cb2 As CheckBox
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")
    Spinner1.DropdownBackgroundColor = Colors.Blue
    Spinner1.DropdownTextColor = Colors.red
    Spinner1.AddAll(Array As String("BACK", "FRONT"))


End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)


End Sub


Sub Button1_Click

    Dim laserAlpha() As Int = Array As Int (0, 64, 128, 192, 255, 192, 128, 64)
    zxalone.laserAlpha = laserAlpha
    zxalone.laserColor = Colors.Magenta
    zxalone.maskColor = Colors.ARGB(100,0,0,200)
    zxalone.resultColor = Colors.ARGB(100,100,0,100)
    zxalone.resultPointColor = Colors.Green
    zxalone.CustomPromptMessage = "Posizionare il codice a barre all'interno della vista dello scanner"
    zxalone.CustomPromptMessageTextColor = Colors.Yellow
    zxalone.CustomPromptMessageTextSize = 20


    cntr = 1                                      'cntr is used to decide what imagview should display the consecutive barcode scans
    zxalone.NumberOfScans = 3                     'set the number of barcodes to scan before returning to B4A
    ImageView1.Bitmap = Null
    ImageView2.Bitmap = Null
    ImageView3.Bitmap = Null
    ImageView4.Bitmap = Null
    ImageView5.Bitmap = Null

    zxalone.BeginScan("zxalone")

End Sub

Sub Spinner1_ItemClick (Position As Int, Value As Object)

    If Spinner1.SelectedIndex = 0 Then zxalone.CameraToUse = zxalone.BACK_CAMERA
    If Spinner1.SelectedIndex = 1 Then zxalone.CameraToUse = zxalone.FRONT_CAMERA

End Sub

Sub zxalone_scan_result(barcodetype As String, barcodetext As String, image As Object)

    Log("barcode type = " & barcodetype)
    Log("barcodetext = " & barcodetext)

    Log("cntr = " & cntr)
    ImageView1.Gravity = Gravity.FILL
    ImageView2.Gravity = Gravity.FILL
    ImageView3.Gravity = Gravity.FILL
    ImageView4.Gravity = Gravity.FILL
    ImageView5.Gravity = Gravity.FILL


    ImageView1.Visible = True
    ImageView2.Visible = True
    ImageView3.Visible = True
    ImageView4.Visible = True
    ImageView5.Visible = True

    ImageView1.BringToFront
    ImageView2.BringToFront
    ImageView3.BringToFront
    ImageView4.BringToFront
    ImageView5.BringToFront

    Label1.Text = "Last Type Scanned: " & barcodetype
    Label2.Text = "Context of last scan: " & barcodetext


    If cntr = 1 Then
        ImageView1.Bitmap = image
        cntr = cntr + 1
    else if cntr = 2 Then
        ImageView2.Bitmap = image
        cntr = cntr + 1
    else if cntr = 3 Then
        ImageView3.Bitmap = image
        cntr = cntr + 1
    else if cntr = 4 Then
        ImageView4.Bitmap = image
        cntr = cntr + 1
    else if cntr = 5 Then
        ImageView5.Bitmap = image
        cntr = cntr + 1
    End If

    If cntr = 6 Then cntr = 1

End Sub

Sub cb1_CheckedChange(Checked As Boolean)

    If Checked = True Then
        zxalone.MoveLaserUpDown = True
    Else
        zxalone.MoveLaserUpDown = False
    End If

End Sub

Sub cb2_CheckedChange(Checked As Boolean)

    If Checked = True Then
        zxalone.ResultPointShape = zxalone.resultpointshape_circle    'can also be zxalone.resultpointshape_circle
    Else
        zxalone.ResultPointShape = zxalone.resultpointshape_square    'can also be zxalone.resultpointshape_square
    End If


End Sub

Designer Code:
B4X:
'All variants script
AutoScaleAll
Button1.Left = 2%x
Button1.Top = 2%y
Button1.Width = 25%x
Button1.Height = 20%y

Label1.left = 2%x
Label1.Top = Button1.Bottom + 11%y
Label1.Width = 96%x
Label1.Height = 15%y

Label2.left = 2%x
Label2.Top = Label1.Bottom + 2%y
Label2.Width = 96%x
Label2.Height = 15%y

ImageView1.Height = 18%x
ImageView1.Width = 18%x
ImageView1.Left = 2%x
ImageView1.Top = 100%y - ImageView1.Height

ImageView2.Height = 18%x
ImageView2.Width = 18%x
ImageView2.Left = ImageView1.Right + 2%x
ImageView2.Top = 100%y - ImageView1.Height

ImageView3.Height = 18%x
ImageView3.Width = 18%x
ImageView3.Left = ImageView2.Right + 2%x
ImageView3.Top = 100%y - ImageView1.Height

ImageView4.Height = 18%x
ImageView4.Width = 18%x
ImageView4.Left = ImageView3.Right + 2%x
ImageView4.Top = 100%y - ImageView1.Height

ImageView5.Height = 18%x
ImageView5.Width = 18%x
ImageView5.Left = ImageView4.Right + 2%x
ImageView5.Top = 100%y - ImageView1.Height


Spinner1.Left = Button1.Right + 5%x
Spinner1.Top = Button1.Top
Spinner1.Width = 20%x
Spinner1.Height = 12%y

cb1.Left = Spinner1.Right + 2%x
cb1.Top = Spinner1.top
cb1.Width = Spinner1.Width
cb1.Height = 15%y

cb2.Left = cb1.Right + 2%x
cb2.Top = cb1.top
cb2.Width = cb1.Width
cb2.Height = 15%y
Extract core-3.2.1.zip and copy the jar to your additional library folder
Extract ZXingStandAlone.zip and copy the jar and xml to your additional library folder
Extract ZxingRes.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.

B4A manifest file:
B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="22"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")

AddApplicationText(<activity android:name="com.google.zxing.client.android.CaptureActivity"
              android:screenOrientation="sensorLandscape"
              android:clearTaskOnLaunch="true"
              android:stateNotNeeded="true"
              android:theme="@style/CaptureTheme"
              android:windowSoftInputMode="stateAlwaysHidden">

      <intent-filter>
        <action android:name="com.google.zxing.client.android.SCAN"/>
        <category android:name="android.intent.category.DEFAULT"/>
      </intent-filter>
      <!-- Allow web apps to launch Barcode Scanner by linking to http://zxing.appspot.com/scan. -->
      <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <data android:scheme="http" android:host="zxing.appspot.com" android:path="/scan"/>
      </intent-filter>
      <!-- We also support a Google Product Search URL. -->
      <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <data android:scheme="http" android:host="www.google.com" android:path="/m/products/scan"/>
      </intent-filter>
      <!-- And the UK version. -->
      <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <data android:scheme="http" android:host="www.google.co.uk" android:path="/m/products/scan"/>
      </intent-filter>
      <!-- Support zxing://scan/?... like iPhone app -->
      <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <data android:scheme="zxing" android:host="scan" android:path="/"/>
      </intent-filter>
    </activity>
    <activity android:name="com.google.zxing.client.android.PreferencesActivity"
              android:label="@string/preferences_name"
              android:stateNotNeeded="true"/>
    <activity android:name="com.google.zxing.client.android.encode.EncodeActivity"
              android:stateNotNeeded="true">
      <intent-filter>
        <action android:name="com.google.zxing.client.android.ENCODE"/>
        <category android:name="android.intent.category.DEFAULT"/>
      </intent-filter>
      <!-- This allows us to handle the Share button in Contacts. -->
      <intent-filter>
        <action android:name="android.intent.action.SEND"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="text/x-vcard"/>
      </intent-filter>
      <!-- This allows us to handle sharing any plain text . -->
      <intent-filter>
        <action android:name="android.intent.action.SEND"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="text/plain"/>
      </intent-filter>
    </activity>
    <activity android:name="com.google.zxing.client.android.book.SearchBookContentsActivity"
              android:label="@string/sbc_name"
              android:stateNotNeeded="true"
              android:screenOrientation="sensorLandscape">
      <intent-filter>
        <action android:name="com.google.zxing.client.android.SEARCH_BOOK_CONTENTS"/>
        <category android:name="android.intent.category.DEFAULT"/>
      </intent-filter>
    </activity>
    <activity android:name="com.google.zxing.client.android.share.ShareActivity"
              android:stateNotNeeded="true"
              android:screenOrientation="user">
      <intent-filter>
        <action android:name="com.google.zxing.client.android.SHARE"/>
        <category android:name="android.intent.category.DEFAULT"/>
      </intent-filter>
    </activity>
    <activity android:name="com.google.zxing.client.android.history.HistoryActivity"
              android:label="@string/history_title"
              android:stateNotNeeded="true"/>
    <activity android:name="com.google.zxing.client.android.share.BookmarkPickerActivity"
              android:label="@string/bookmark_picker_name"
              android:stateNotNeeded="true"/>
    <activity android:name="com.google.zxing.client.android.share.AppPickerActivity"
              android:label="@string/app_picker_name"
              android:stateNotNeeded="true"/>
    <activity android:name="com.google.zxing.client.android.HelpActivity"
              android:label="@string/menu_help"
              android:screenOrientation="user"
              android:stateNotNeeded="true"/>)
'End of default text.

1.png



2.png
 

Attachments

  • b4aZXingStandAlone.zip
    11.7 KB · Views: 431
  • ZXingStandAlone.zip
    170.7 KB · Views: 424
  • ZxingRes.zip
    155.9 KB · Views: 410
  • core-3.2.1.zip
    483.9 KB · Views: 408
Last edited:

BarryW

Active Member
Licensed User
Longtime User
how to make scanner automatic close after scanning the barcode. Also how to remove web search, share via email and otherboptions inside the scanner
 

Ydm

Active Member
Licensed User
Longtime User
I also checked the Github project. However, I could not find any information about portrait scanning. Is this possible with this plugin? When I write the code below in the manifest file, the barcode reader opens vertically. However, it only reads when I flip the screen horizontally. Cannot read vertically.

XML:
<activity android:name="com.google.zxing.client.android.CaptureActivity"
              android:screenOrientation="sensorPortrait"
              android:clearTaskOnLaunch="true"
              android:stateNotNeeded="true"
              android:theme="@style/CaptureTheme"
              android:windowSoftInputMode="stateAlwaysHidden">
 

Johan Schoeman

Expert
Licensed User
Longtime User
I also checked the Github project. However, I could not find any information about portrait scanning. Is this possible with this plugin? When I write the code below in the manifest file, the barcode reader opens vertically. However, it only reads when I flip the screen horizontally. Cannot read vertically.

XML:
<activity android:name="com.google.zxing.client.android.CaptureActivity"
              android:screenOrientation="sensorPortrait"
              android:clearTaskOnLaunch="true"
              android:stateNotNeeded="true"
              android:theme="@style/CaptureTheme"
              android:windowSoftInputMode="stateAlwaysHidden">
With the change that you have made to the manifest - change the following in the B4A project:

#SupportedOrientations: portrait:
#Region  Project Attributes
    #ApplicationLabel: ZXingStandAlone
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait

It will then scan in portrait mode. Or try this one:

 
  • Like
Reactions: Ydm

Ydm

Active Member
Licensed User
Longtime User
My B4A project is already in portrait mode. So the way you say. The barcode scanner opens vertically. But if I turn the scan sideways, it can.
 
Top