Android Question MobileVision Camera Problem

potman100

Active Member
Licensed User
Longtime User
Hi

I am using the following code to display and capture barcodes with mobile vision, all works fine until I load another activity like preferences, on closing the preferences screen the cam view has gone, I have had it working were it appears, but then it has no autofocus enabled, trying to restart autofocus does nothing !

B4X:
#Region  Project Attributes
    #ApplicationLabel: Camera Test
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region
#AdditionalJar: com.google.android.gms:play-services-vision
#BridgeLogger: true

#Region  Activity Attributes
    #FullScreen: True
    #IncludeTitle: False
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private frontCamera As Boolean = False
    Private detector As JavaObject
    Private SearchForBarcodes As Boolean
    Private LastPreview As Long
    Private IntervalBetweenPreviewsMs As Int = 500
    Private LastBarcode As String
    Dim manager As PreferenceManager
    Dim screen As PreferenceScreen
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 cvs As B4XCanvas
    Dim cs As CameraSize
    Private camEx As CameraExClass

    
    Private pnlScanner As Panel
    Private pnlDrawing As Panel
    Private cmdOptions As Button
    Private lblStatus As Label
    Private Panel6 As Panel
    Private ScrollView2 As ScrollView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    If FirstTime Then
        Activity.LoadLayout("main")
        cvs.Initialize(pnlDrawing)
        CreateDetector (Array("ALL_FORMATS"))
        CreatePreferenceScreen
    End If
End Sub

Sub Activity_Resume
    Try

        InitializeCamera
        pnlScanner.Visible=True
        
        camEx.StartPreview
        pnlScanner.Invalidate
    Catch
    End Try
End Sub

Sub Activity_Pause (UserClosed As Boolean)

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)
    Log("Is detector operational: " & operational)
'    SearchForBarcodes = operational
    
End Sub
Sub Camera1_Preview (data() As Byte)
'    Log("In Preview")
'    If SearchForBarcodes Then
        If DateTime.Now > LastPreview + IntervalBetweenPreviewsMs Then
'            If BypassScan = False Then
                'Dim n As Long = DateTime.Now
                cvs.ClearRect(cvs.TargetRect)
                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)
                If cs.Width = 0 Then
                    cs = camEx.GetPreviewSize
                End If
                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
            
                For i = 0 To SparseArray.RunMethod("size", Null) - 1
                    Dim barcode As JavaObject = SparseArray.RunMethod("valueAt", Array(i))
                    Dim raw As String = barcode.GetField("rawValue")
                    Log(raw)
                
                Next
            End If
'        End If
'    End If
End Sub

Private Sub InitializeCamera
    Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_CAMERA)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result Then
        camEx.Initialize(pnlScanner, frontCamera, Me, "Camera1")
        frontCamera = camEx.Front
    End If
End Sub

Sub Camera1_Ready (Success As Boolean)
    If Success Then
        camEx.SetJpegQuality(90)
        camEx.SetContinuousAutoFocus
        camEx.CommitParameters
        camEx.StartPreview
        Log(camEx.GetPreviewSize)
    Else
        ToastMessageShow("Cannot open camera.", True)
    End If
End Sub

    
End Sub
Sub cmdOptions_Click
    StartActivity(screen.CreateIntent) ' Start the Pref Screen
End Sub

Sub CreatePreferenceScreen
    screen.Initialize("Settings", "")
    Dim cat1 As PreferenceCategory
    cat1.Initialize("Test")
    cat1.AddCheckBox("T1", "Test 1", "", True)
    cat1.AddCheckBox("T2", "Test 2", "", True)
    screen.AddPreferenceCategory(cat1)
End Sub

I have had it doing different things, but can't get it to work properly.
 

potman100

Active Member
Licensed User
Longtime User
I knew is was some thing simple, brain freeze, I just added

B4X:
Sub Activity_Resume
    Try
        
        InitializeCamera
        Dim operational As Boolean = detector.RunMethod("isOperational", Null)
        pnlScanner.Visible=True
        camEx.StopPreview
        camEx.StartPreview
        pnlScanner.Invalidate
    Catch
    End Try
End Sub
 
Upvote 0
Top