Android Question Error using ZXing Scanner with BAXpage

Situ LLC

Active Member
Licensed User
I am trying to use the ZXing Scanner library with B4X Page and it generates the following error:

B4X:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
    
    Private zx1 As BarcodeScanner
    
    Private pmain As Panel
    Private b1 As Button
    Private b2 As Button
    Private b3 As Button
    Private b4 As Button
    Private b5 As Button
    Private lResult As Label
    Private lAuto As Label
    Private l3 As Label
    
    Private view As Panel
    
    Private flag As Int = 0
    Private runningavg As Int = 127
    
    Private scanStarted As Boolean = False
    
End Sub

'You can add more parameters here.
Public Sub Initialize
    
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    'load the layout to Root
    
End Sub

Private Sub B4XPage_Appear
    
    Root.LoadLayout("scanner")
    
    lResult.Text = "Result"
    lAuto.Text = ""
    
    b4.Text = "+"
    b4.TextSize = 20
    
    b5.Text = "-"
    b5.TextSize = 20
    
    CreateScanner
    
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub CreateScanner
    Dim rp As RuntimePermissions
    
    Dim Result As Boolean = True
    If Not(rp.Check(rp.PERMISSION_CAMERA)) Then
        rp.CheckAndRequest(rp.PERMISSION_CAMERA)
        Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    End If
    If Result Then
    
        Dim x, y, w, h As Int
        x = 20
        y = 220
        w = 640
        h = 420
    
        view.Initialize("")
        Root.Color = Colors.White
        pmain.AddView(view, x, y, w, h)

        zx1.Initialize("zx1")  <----------------------------------------------HERE ERROR
        zx1.LaserColor     = Colors.Green
        zx1.MaskColor     = Colors.ARGB(150, 0, 0, 200)
        zx1.BorderColor = Colors.Magenta
        zx1.BorderStrokeWidth = 5
        zx1.BorderLineLength = 40
        zx1.Beep     = True
        zx1.Vibrate = True
        zx1.AutoFocusInterval = 500
        zx1.AutoFocus     = True
        zx1.FocusMode     = zx1.FOCUS_MODE_AUTO
        zx1.AddPanel(view)
                
        l3.Text = zx1.Width & "/" & zx1.Left
        
        Log($"Camera is : ${zx1.checkCameraHardware} "$)
        
    Else
    
        ToastMessageShow("Permission not granted", True)
    
    End If
End Sub

Sub b1_Click
    
    zx1.ToggleFlash
    
End Sub

Sub b2_Click
   
    zx1.Visible = True
    zx1.startScanner
    '    zx1.TurnFlashOn
    '    Log(zx1.isFlashOn)

    scanStarted = True
    b2.Enabled = False
    b1.Enabled = True
   
End Sub

Sub b3_Click
    
    zx1.Visible = False
    zx1.stopScanner
    lResult.Text = ""
    lAuto.Text = ""
    l3.Text = ""
    
    scanStarted = False
    b2.Enabled = True
    b1.Enabled = False
    
End Sub

Sub b4_Click
    
    Log("Zoom b4")
    Dim zoomInValue As Int = 5                     'positive values -> zoon in
    If scanStarted Then
        If zx1.CurrentZoomValue <= (zx1.MaxZoomValue - zoomInValue) Then
            zx1.zoomIn(zoomInValue)
        Else
            zx1.zoomIn(zx1.MaxZoomValue - zx1.CurrentZoomValue)
        End If
        Log("Zoom +")
    End If
    
End Sub

Sub b5_Click
    Log("Zoom b5")
    Dim zoomInValue As Int = -5                     'negative values -> zoom out
    If scanStarted Then
        If zx1.CurrentZoomValue > (-1* zoomInValue) Then
            zx1.zoomIn(zoomInValue)
        Else
            zx1.zoomIn(0 - zx1.CurrentZoomValue)
        End If
        Log("Zoom -")
    End If
    
End Sub

Sub zx1_scan_result (scantext As String, scanformat As String)
    
'    Dim tim As Long = 100
    Log ("B4A scan text = " & scantext)
    Log ("B4A scan format = " & scanformat)
'    Log("isFlashOn = " & zx1.isFlashOn)
    lResult.Text = "Text : " & scantext & CRLF & " Format : " & scanformat
    
    ToastMessageShow("Text : " & scantext & CRLF & " Format : " & scanformat, True)
    
    zx1.Visible = False
    zx1.stopScanner
    
    scanStarted = False
    
End Sub

Sub zx1_brightness_changed (bright As Int )                        'ADDED 6 MARCH 2016
'    Log("bright = " & bright)                                      'this is the average brightness of the frame that was captured for possible barcode extraction
    flag = flag + 1
    lAuto.Text = "Brigtness = " & bright
    If flag = 30 Then                                              'I only want to use the average brightness of every 30th frame to calculate a running brigtness average
        flag = 0                                                   'reset the count to zero for the next 30 frames
        If bright < 150 Then                                       'discard excessive high brigness values
            runningavg = (runningavg + bright) / 2
'            Log("runningavg brightness = " & runningavg)
            If runningavg < 70 Then
                If zx1.isFlashOn = False Then zx1.TurnFlashOn
            Else if runningavg > 115 Then
                If zx1.isFlashOn = True Then zx1.TurnFlashOff
            End If
        End If
    End If
'    l3.Text = "isFlashOn = " & zx1.isFlashOn
    
End Sub


Sub B4XPage_Foreground
    If zx1.Autofocus = True Then
        lAuto.Text = "AUTO : ON"
    Else
        lAuto.Text = "AUTO : OFF"
    End If         
    zx1.Visible = True
    zx1.Autofocus = True
    zx1.startScanner
End Sub

Sub B4XPage_Background 
    zx1.Visible = False
    zx1.stopScanner
End Sub

The error code
B4X:
Copying updated assets files (1)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
Error occurred on line: 77 (B4XPage2)
android.content.res.Resources$NotFoundException: Resource ID #0x0
    at android.content.res.Resources.getValue(Resources.java:1266)
    at android.content.res.Resources.getColor(Resources.java:920)
    at com.scanner.core.ViewFinderView.<init>(ViewFinderView.java:38)
    at com.scanner.core.BarcodeScannerView.createViewFinderView(BarcodeScannerView.java:129)
    at com.scanner.core.BarcodeScannerView.<init>(BarcodeScannerView.java:29)
    at com.scanner.zxing.ZXingScannerView.<init>(ZXingScannerView.java:70)
    at com.scanner.ScannerWrapper._initialize(ScannerWrapper.java:82)
    at com.scanner.ScannerWrapper.Initialize(ScannerWrapper.java:71)
    at b4a.example.b4xpage2$ResumableSub_CreateScanner.resume(b4xpage2.java:330)
    at b4a.example.b4xpage2._createscanner(b4xpage2.java:220)
    at b4a.example.b4xpage2._b4xpage_appear(b4xpage2.java:210)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA$2.run(BA.java:387)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
** Activity (main) Pause event (activity is not paused). **

Important !! If I use the library in a project that does not use B4XPage it works correctly
 
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
Sure you have the resources in your project?

B4X:
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.
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
I am trying to use the ZXing Scanner library with B4X Page and it generates the following error:

Important !! If I use the library in a project that does not use B4XPage it works correctly
Try with the attached - it is using B4XPages (the jar and xml should be the same as what you already have - I guess....)
 

Attachments

  • ZxingBarcodeScanner.jar
    31.8 KB · Views: 394
  • ZxingBarcodeScanner.xml
    13.7 KB · Views: 392
  • b4aZxingScannerPages.zip
    14.6 KB · Views: 380
Upvote 0
Top