Android Question ZXingBarcodeScanner hangs

hung

Active Member
Licensed User
Longtime User
I need help from this group. I am using ZxingBarcodeScanner v1.00 library to create a camera scanner to scan QR codes.

I follow the original sample to create the barcode camera. All ran well but after SDK v28 (or earlier) the app hung as below
- scan barcode ok for a while
- suddenly hang during scanning
- when restart app and scan again, camera show black screen, same when start Android camera
- only solved by wait for long time or reboot Android phone

That happened in Samsung, Huawei and Xiaomi (9T pro). Attached is the latest hang screen.
WhatsApp Image 2022-06-07 at 7.48.02 PM.jpeg

Part of the code as following

B4X:
Sub Globals
..
    Private zx1_cont As ZxingBarcodeScanner
...
Sub Activity_Create(FirstTime As Boolean)
   ...
        scanbarcode_init_cont
...
Sub Activity_Resume
    If pnl_scan.Visible Then
        zx1_cont.Visible = True
        zx1_cont.startScanner
    End If
 ...
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If pnl_scan.Visible Then 
        zx1_cont.Visible = False
        zx1_cont.stopScanner
    End If
...
End Sub


Sub scanbarcode_init_cont
    zx1_cont.LaserColor = Colors.Red
    zx1_cont.MaskColor = Colors.ARGB(100, 0, 0, 100)
    zx1_cont.BorderColor = Colors.Blue
    zx1_cont.BorderStrokeWidth = 5
    zx1_cont.BorderLineLength = 40
    zx1_cont.Visible = False
End Sub

Sub scanbarcode_pause_cont
    ' causing hang ?
    'zx1_cont.Visible = False
    'zx1_cont.stopScanner
End Sub

Sub scanbarcode_start_cont
    pnl_scan.Visible = True
    pnl_scan.BringToFront
    chk_cont.Checked = gScanCont
    zx1_cont.Visible = True
    zx1_cont.startScanner
End Sub

Sub scanbarcode_stop_cont
    zx1_cont.Visible = False
    zx1_cont.stopScanner
    pnl_scan.Visible = False
    lv_barcode_cont.Clear
    lbl_cnt_cont.Text = "0"
    btn_getbarcode_cont.Text = "Get Barcode"
End Sub

Sub scanbarcode_flash_cont
    zx1_cont.toggleFlash
End Sub

Sub zx1_cont_scan_result (scantext As String, scanformat As String)
    
    If gScanCont = False Then
        btn_getbarcode_cont.Text = scantext
        btn_getbarcode_cont_Click
    Else
        Dim lcnt As Int
        Dim fdup As Boolean
        lcnt = 0
        fdup = False
        Do While lcnt < lv_barcode_cont.Size
            If lv_barcode_cont.GetItem(lcnt) = scantext Then
                fdup = True
                Exit
            End If
            lcnt = lcnt + 1
        Loop
        If fdup = False Then
            lv_barcode_cont.AddSingleLine(scantext)
            lv_barcode_cont.SetSelection(lv_barcode_cont.Size)
            lbl_cnt_cont.Text = lv_barcode_cont.Size
            btn_getbarcode_cont.TextColor = Colors.Black
            btn_getbarcode_cont.Text = scantext
        Else
            btn_getbarcode_cont.TextColor = Colors.Red
        End If
    End If

End Sub


The manifest as below

B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="30"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
AddManifestText(<uses-feature android:name="android.hardware.telephony" android:required="true" />)
AddManifestText(<uses-feature android:name="android.hardware.camera" android:required="false" />) ' to avoid xiaomi cam crash
AddManifestText(<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />)
AddManifestText(<uses-feature android:name="android.hardware.camera.flash" android:required="false" />)
 
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
I need help from this group. I am using ZxingBarcodeScanner v1.00 library to create a camera scanner to scan QR codes.

I follow the original sample to create the barcode camera. All ran well but after SDK v28 (or earlier) the app hung as below
- scan barcode ok for a while
- suddenly hang during scanning
- when restart app and scan again, camera show black screen, same when start Android camera
- only solved by wait for long time or reboot Android phone

That happened in Samsung, Huawei and Xiaomi (9T pro). Attached is the latest hang screen.
View attachment 130079
Part of the code as following

B4X:
Sub Globals
..
    Private zx1_cont As ZxingBarcodeScanner
...
Sub Activity_Create(FirstTime As Boolean)
   ...
        scanbarcode_init_cont
...
Sub Activity_Resume
    If pnl_scan.Visible Then
        zx1_cont.Visible = True
        zx1_cont.startScanner
    End If
 ...
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If pnl_scan.Visible Then
        zx1_cont.Visible = False
        zx1_cont.stopScanner
    End If
...
End Sub


Sub scanbarcode_init_cont
    zx1_cont.LaserColor = Colors.Red
    zx1_cont.MaskColor = Colors.ARGB(100, 0, 0, 100)
    zx1_cont.BorderColor = Colors.Blue
    zx1_cont.BorderStrokeWidth = 5
    zx1_cont.BorderLineLength = 40
    zx1_cont.Visible = False
End Sub

Sub scanbarcode_pause_cont
    ' causing hang ?
    'zx1_cont.Visible = False
    'zx1_cont.stopScanner
End Sub

Sub scanbarcode_start_cont
    pnl_scan.Visible = True
    pnl_scan.BringToFront
    chk_cont.Checked = gScanCont
    zx1_cont.Visible = True
    zx1_cont.startScanner
End Sub

Sub scanbarcode_stop_cont
    zx1_cont.Visible = False
    zx1_cont.stopScanner
    pnl_scan.Visible = False
    lv_barcode_cont.Clear
    lbl_cnt_cont.Text = "0"
    btn_getbarcode_cont.Text = "Get Barcode"
End Sub

Sub scanbarcode_flash_cont
    zx1_cont.toggleFlash
End Sub

Sub zx1_cont_scan_result (scantext As String, scanformat As String)
  
    If gScanCont = False Then
        btn_getbarcode_cont.Text = scantext
        btn_getbarcode_cont_Click
    Else
        Dim lcnt As Int
        Dim fdup As Boolean
        lcnt = 0
        fdup = False
        Do While lcnt < lv_barcode_cont.Size
            If lv_barcode_cont.GetItem(lcnt) = scantext Then
                fdup = True
                Exit
            End If
            lcnt = lcnt + 1
        Loop
        If fdup = False Then
            lv_barcode_cont.AddSingleLine(scantext)
            lv_barcode_cont.SetSelection(lv_barcode_cont.Size)
            lbl_cnt_cont.Text = lv_barcode_cont.Size
            btn_getbarcode_cont.TextColor = Colors.Black
            btn_getbarcode_cont.Text = scantext
        Else
            btn_getbarcode_cont.TextColor = Colors.Red
        End If
    End If

End Sub


The manifest as below

B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="30"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
AddManifestText(<uses-feature android:name="android.hardware.telephony" android:required="true" />)
AddManifestText(<uses-feature android:name="android.hardware.camera" android:required="false" />) ' to avoid xiaomi cam crash
AddManifestText(<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />)
AddManifestText(<uses-feature android:name="android.hardware.camera.flash" android:required="false" />)
You need to add RuntimePermission in the B4A project for the camera if SDK is >= 26
 
Last edited:
Upvote 0

hung

Active Member
Licensed User
Longtime User
You need to add RuntimePermission in the B4A project for the camera if SDK is >= 26
@Johan Schoeman yes I did so the first 5 to 10 barcodes could be scanned. Then hang on camera screen / frozen until I reboot Android.

Would that be related to camera Auto Focus? I tried to set autofocus as off but seems not directly related.

I am using latest b4a and java sdk.


This is the Runtime Permission requested:
'AddPermission("android.permission.ACCESS_COARSE_LOCATION")
AddPermission("android.permission.INTERNET")
AddPermission("android.permission.ACCESS_FINE_LOCATION")
AddPermission("android.permission.WAKE_LOCK")
AddPermission("android.permission.DEVICE_POWER")
'AddPermission("android.permission.ACCESS_COARSE_UPDATES")
AddPermission("android.permission.READ_PHONE_STATE")
AddPermission("android.permission.VIBRATE")
AddPermission("android.permission.CAMERA")
AddPermission("android.permission.FLASHLIGHT")
'AddPermission("android.hardware.camera")
 
Last edited:
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
@Johan Schoeman yes I did so the first 5 to 10 barcodes could be scanned. Then hang on camera screen / frozen until I reboot Android.

Would that be related to camera Auto Focus? I tried to set autofocus as off but seems not directly related.

I am using latest b4a and java sdk.


This is the Runtime Permission requested:
'AddPermission("android.permission.ACCESS_COARSE_LOCATION")
AddPermission("android.permission.INTERNET")
AddPermission("android.permission.ACCESS_FINE_LOCATION")
AddPermission("android.permission.WAKE_LOCK")
AddPermission("android.permission.DEVICE_POWER")
'AddPermission("android.permission.ACCESS_COARSE_UPDATES")
AddPermission("android.permission.READ_PHONE_STATE")
AddPermission("android.permission.VIBRATE")
AddPermission("android.permission.CAMERA")
AddPermission("android.permission.FLASHLIGHT")
'AddPermission("android.hardware.camera")
See RuntimePermission here that you need to add to your B4A project. See how the permission is requested in the sample code in the below post

 
Upvote 0
Top