Android Question (Solved) [B4XPages] Barcode Reader Camera not Show

Ajws

Member
Licensed User
hai please help, i get error :
error:
rp.CheckAndRequest(rp.PERMISSION_CAMERA)
    Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
    If Result = False Then
        ToastMessageShow("No permission!",True)
        Return
    End If
    StartCameraShared

source project from : https://www.b4x.com/android/forum/threads/b4x-b4xpages-barcode-reader.120417/
my camera is not show and permission error : Error evaluating expression

Capture.JPG
 

Ajws

Member
Licensed User
code:
Sub Activity_PermissionResult (Permission As String, Result As Boolean)
    B4XPages.Delegate.Activity_PermissionResult(Permission, Result)
End Sub

PERMISSION_CAMERA --> result : True

But Why My camera not show on my project ?

but if I run from the project that I downloaded, there is no problem, maybe there is a script that I forgot to copy or what? I don't understand.

this is my code :

code:
#Region Shared Files
#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\Shared Files" "..\Files"
'Ctrl + click to sync files: ide://run?file=%WINDIR%\System32\Robocopy.exe&args=..\..\Shared+Files&args=..\Files&FilesSync=True
#End Region

'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=Project.zip

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 xui As XUI
    Private pnlPreview As B4XView
    Private btnStartStop As B4XView
    #if B4A
    Private rp As RuntimePermissions
    Private detector As JavaObject
    Private camEx As CameraExClass
    Private LastPreview As Long
    Private IntervalBetweenPreviewsMs As Int = 100
    #else if B4i
    Private scanner As BarcodeScanner
    #End If
        Private lblResult As B4XView
    Private Capturing As Boolean
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("Layout1")
 
    Activity.LoadLayout("FScan")
    
    StopCamera
    'B4XPages.SetTitle(Me, "Barcode Example")
    #if B4A
    CreateDetector (Array("CODE_128", "CODE_93", "QR_CODE"))
    #Else if B4i
    scanner.Initialize2("scanner", pnlPreview, Array(scanner.TYPE_93, scanner.TYPE_128, scanner.TYPE_QR))
    Wait For Scanner_Ready (Success As Boolean)
    If Success = False Then
        btnStartStop.Enabled = False
        toast.Show("Failed to initialize the scanner.")
    End If
    #end if
End Sub

Private Sub B4XPage_Disappear
    StopCamera
End Sub


Sub btnStartStop_Click
    If Capturing = False Then
        StartCamera
    Else
        StopCamera
    End If
End Sub

Private Sub StopCamera
    Capturing = False
    btnStartStop.Text = "Start"
    pnlPreview.Visible = False
    #if B4A
    If camEx.IsInitialized Then
        camEx.Release
    End If
    #Else If B4i
    scanner.Stop
    #end if
End Sub

Private Sub StartCameraShared
    btnStartStop.Text = "Stop"
    pnlPreview.Visible = True
    Capturing = True
End Sub

Private Sub FoundBarcode (msg As String)
    lblResult.Text = msg
    ToastMessageShow($"Found [Color=Blue][b][plain]${msg}[/plain][/b][/Color]"$,True)
End Sub

 

#if B4A
Private Sub StartCamera
    Try
        rp.CheckAndRequest(rp.PERMISSION_CAMERA)
        Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
        If Result = False Then
            ToastMessageShow("No permission!",True)
            Return
        End If
        StartCameraShared
        camEx.Initialize(pnlPreview, False, Me, "Camera1")
        Wait For Camera1_Ready (Success As Boolean)
        If Success Then
            camEx.SetContinuousAutoFocus
            camEx.CommitParameters
            camEx.StartPreview
        Else
            ToastMessageShow("Error opening camera",True)
            StopCamera
        End If
Catch
    Log(LastException)
End Try
    
End Sub

Sub Activity_PermissionResult (Permission As String, Result As Boolean)
    B4XPages.Delegate.Activity_PermissionResult(Permission, Result)
End Sub

Private Sub CreateDetector (Codes As List)
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    Dim builder As JavaObject
    builder.InitializeNewInstance("com/google/android/gms/vision/barcode/BarcodeDetector.Builder".Replace("/", "."), Array(ctxt))
    Dim barcodeClass As String = "com/google/android/gms/vision/barcode/Barcode".Replace("/", ".")
    Dim barcodeStatic As JavaObject
    barcodeStatic.InitializeStatic(barcodeClass)
    Dim format As Int
    For Each formatName As String In Codes
        format = Bit.Or(format, barcodeStatic.GetField(formatName))
    Next
    builder.RunMethod("setBarcodeFormats", Array(format))
    detector = builder.RunMethod("build", Null)
    Dim operational As Boolean = detector.RunMethod("isOperational", Null)
    If operational = False Then
        ToastMessageShow("Failed to create detector",True)
    End If
    btnStartStop.Enabled = operational
End Sub

Private Sub Camera1_Preview (data() As Byte)
    If DateTime.Now > LastPreview + IntervalBetweenPreviewsMs Then
        'Dim n As Long = DateTime.Now
        Dim frameBuilder As JavaObject
        Dim bb As JavaObject
        bb = bb.InitializeStatic("java.nio.ByteBuffer").RunMethod("wrap", Array(data))
        frameBuilder.InitializeNewInstance("com/google/android/gms/vision/Frame.Builder".Replace("/", "."), Null)
        Dim cs As CameraSize = camEx.GetPreviewSize
        frameBuilder.RunMethod("setImageData", Array(bb, cs.Width, cs.Height,  842094169))
        Dim frame As JavaObject = frameBuilder.RunMethod("build", Null)
        Dim SparseArray As JavaObject = detector.RunMethod("detect", Array(frame))
        LastPreview = DateTime.Now
        Dim Matches As Int = SparseArray.RunMethod("size", Null)
        If Matches > 0 Then
            Dim barcode As JavaObject = SparseArray.RunMethod("valueAt", Array(0))
            Dim raw As String = barcode.GetField("rawValue")
            FoundBarcode(raw)
        End If
    End If
End Sub

#Else if B4I
Sub B4XPage_Resize (Width As Float, Height As Float)
    scanner.Resize
End Sub

Private Sub StartCamera
    scanner.Start
    StartCameraShared
End Sub

Sub scanner_Detected (Codes As List)
    If Codes.Size > 0 Then
        Dim code As BarcodeCode = Codes.Get(0)
        FoundBarcode(code.Value)
    End If
End Sub
#End If


Sub Activity_Resume
    'B4XPages.Delegate.Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub




please help.
 
Last edited:
Upvote 0

josejad

Expert
Licensed User
Longtime User
but if I run from the project that I downloaded, there is no problem, maybe there is a script that I forgot to copy or what?
Have you check the manifest of the working example?

Have you added the lines:
B4X:
AddApplicationText(<meta-data
    android:name="com.google.android.gms.vision.DEPENDENCIES"
    android:value="barcode,,face" />
)
CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase)
 
Upvote 0

Ajws

Member
Licensed User
Have you check the manifest of the working example?

Have you added the lines:
B4X:
AddApplicationText(<meta-data
    android:name="com.google.android.gms.vision.DEPENDENCIES"
    android:value="barcode,,face" />
)
CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase)
manifest:
'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="5" android:targetSdkVersion="26"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>
   
<meta-data
    android:name="com.google.android.gms.vision.DEPENDENCIES"
    android:value="barcode" />  
    )
   
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.LightTheme)
'End of default text.
AddPermission(android.permission.WRITE_EXTERNAL_STORAGE)
AddManifestText(<uses-feature android:name="android.hardware.location.gps"/>)
AddApplicationText( <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />)



 AddApplicationText(<meta-data
    android:name="com.google.android.gms.vision.DEPENDENCIES"
    android:value="barcode,,face" />
)

 CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase)


this is my manifest, but still cannot show.
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
Upload your project and it will be easier someone can help you.
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
Please, tell us where the problem was, in order it can help other members.
 
Upvote 0

Ajws

Member
Licensed User
Please, tell us where the problem was, in order it can help other members.
Function:
Sub Activity_PermissionResult (Permission As String, Result As Boolean)
    B4XPages.Delegate.Activity_PermissionResult(Permission, Result)
End Sub

i just copy without see name :
Error Code:
rp.CheckAndRequest(rp.PERMISSION_CAMERA)
    Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
    If Result = False Then
        ToastMessageShow("No permission!",True)
        Return
    End If
    StartCameraShared

Correct Code:
rp.CheckAndRequest(rp.PERMISSION_CAMERA)
    Wait For Activity_PermissionResult(Permission As String, Result As Boolean)
    If Result = False Then
        ToastMessageShow("No permission!",True)
        Return
    End If
    StartCameraShared

sorry I'm not focused
 
Upvote 0
Top