Android Question CameraEx Problem

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi
this is probably not a request for help, rather a request of clarification, or possibly a rectification of my conclusions. The problem is resumed in the following:
1) I have a layout with a Panel "Main" containing some EditTexts and another Panel "pnlA".
2) Inside Main, I have a panel "pnlA" which is used by CameraExClass to take photos. pnlA has Parent "Main"
3) If I put over "pnlA" a button "Foto" , having pnlA as parent, the App crashes (when I initialize the CameraEx) (pnlA is the white Panel)
4) If same Button "Foto", besides being displayed over pnlA, has parent "Main" everything works.
The crash happens even without Clicking the Button, only initializing the Camera, as shown in the log.

Camera is initialized with:
B4X:
CamEx.Initialize(pnlA, frontCamera, Me, "Camera1")

My conclusion is that i cannot put the Button on the Panel "pnlA" connected to CameraEx, having pnlA as parent.
The problem is apparently emphirically resolved..

Error is reported in the attached log and occurs at:

B4X:
Public Sub Initialize (Panel1 As Panel, frontCamera As Boolean, TargetModule As Object, EventName As String)
    target = TargetModule
    event = EventName
    Front = frontCamera
    Dim id As Int
    id = FindCamera(Front).id
    If id = -1 Then
        Front = Not(Front) 'try different camera
        id = FindCamera(Front).id
        If id = -1 Then
            Log("No camera found.")
            Return
        End If
    End If
    Log("Panel1 " & Panel1) ' Panel1 is pnlA ..
    cam.Initialize2(Panel1, "camera", id) ' <<<<<<<<<<<error here (line 34)
End Sub
 

Attachments

  • i1.png
    13.1 KB · Views: 70
  • log.txt
    1.4 KB · Views: 60

walterf25

Expert
Licensed User
Longtime User
What is the error you are receiving exactly, how are you adding the pnlA panel to your project, are you using the Visual Designer or are you adding it through code, if so are you initializing the pnlA and adding it to your activity before using it?

Walter
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Thanks for your reply. Anyway, I add the Panel with Designer. The error is reported in the log file that I attached.
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Thanks for your reply. Anyway, I add the Panel with Designer. The error is reported in the log file that I attached.
The error is very clear
java.lang.ClassCastException: android.widget.Button cannot be cast to android.view.SurfaceView
Seems like you added a panel view instead of a button, can you upload a small example that shows this problem?

Walter
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
The error is very clear

Seems like you added a panel view instead of a button, can you upload a small example that shows this problem?

Walter
Hi. Thanks for your interest, but I cannot spend time for building a test example now. Things are as I described. You wrote "Seems that you added a panel view instead of a button.". No, I added a button. The Attached "bal" file shows my situation. I Have a first Panel "pnlNewName" which contains everything. Then there some edit controls, and a panel where the CameraEx operates. Its name is PannelloCamera. Finally we have btnTakePicture which is the problem: If I put btnTakePicture parent as PannelloCamera, App crashes, if I put btnTakePicture parent as pnlNewName, no problems.
Initially the Layout is loaded by sub "AskPinName". CameraEx is not yet initialized. Related code is:
B4X:
private Sub show_PinData As ResumableSub
    Private pnl As B4XView = xui.CreatePanel("")
    Private inputDialog As B4XDialog
  
    pnl.SetLayoutAnimated(0dip, 5%x,10%y, 90%x, 95%y)
  
    pnl.LoadLayout("AskPinData")    

    inputDialog.Initialize(Root)

    inputDialog.PutAtTop = True
    inputDialog.BackgroundColor = Colors.White
    
    PannelloCamera.Color=Colors.Black
    
    edtNewName.Color=Colors.White
    edtPinDesc.Color=Colors.White
    
    btnTakePicture.Visible=True
    btnTakePicture.Enabled=True
    
    Dim rsub1 As ResumableSub =  inputDialog.ShowCustom(pnl, "Yes", "No", "")

    Wait For (rsub1) Complete (Result As Int)
    
    Return Result
    
End Sub

private Sub AskPinName As ResumableSub
    
    wait for (show_PinData) complete (res As Int)

    Return res
End Sub
Then, when PannelloCamera (which is attached to CameraEx) is clicked, Initially btnTakePicture is not visible. It becomes visible after CamEx is initialized. CameraEx object is initialized by:
B4X:
Private Sub PannelloCamera_Click
    If Not(btnTakePicture.Visible) Then
        If Not(CamEx.IsInitialized) Then
            wait for (InitializeCamera) complete (Resu As Boolean)
            Sleep(100)
            If Resu Then 
                CamEx.StartPreview
            End If
        Else
            CamEx.StartPreview
        End If
        btnTakePicture.Enabled=True
        btnTakePicture.Visible=True
    Else
        btnTakePicture.Visible=False
        btnTakePicture.Enabled=False
        If CamEx.IsInitialized Then CamEx.StopPreview
    End If
End Sub
Private Sub InitializeCamera As ResumableSub
    Log("InitializeCamera " & PannelloCamera.IsInitialized)
    If PannelloCamera.IsInitialized Then
        Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_CAMERA)
        
        Wait For b4xpage_PermissionResult (Permission As String, Result As Boolean)
        Log("Permission " & Result)
        If Result Then
            CamEx.Initialize(PannelloCamera, frontCamera, Me, "Camera1")
            frontCamera = CamEx.Front
            Log("Camera initialized")
        Else
            Log("No permission!!!")
        End If
    End If
    Return Result
End Sub

Sub Camera1_Ready (Success As Boolean)
    Log ("Camera1_Ready")
    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


Sub btnTakePicture_Click
    If CamEx.IsInitialized Then
        CamEx.FocusAndTakePicture
    Else
        Log("Camera not initialized!!")
    End If
End Sub
Fnally I attach CameraExClass that I am using. Thanks for your patience.
 

Attachments

  • askpindata.bal
    4.9 KB · Views: 52
  • CameraExClass.bas
    11 KB · Views: 53
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi. BTW I have reproduced the problem with the CameraExClass code got from this site. I just changed the Existing button btnTakePicture parent, from Activity to Panel1: the problem is the same. I attach the project with only this change, to avoid further doubts.
 

Attachments

  • CameraExProblem.zip
    13.4 KB · Views: 55
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Ah I see what you're trying to do, you actually can not and should not add any views to the Preview panel, take a look at this thread
https://www.b4x.com/android/forum/t...amera-library-functionality.23801/post-281365 and the one below that is Erel's suggestion to not use or add any views to the Preview Panel.

You can add another Panel view on top and set it's background color to transparent and add your views to that Panel instead.

Check out the uploaded modified example, or you can just simply leave the button in the Main Activity as it's parent.

Walter
 

Attachments

  • Camera_Modified.zip
    13.5 KB · Views: 61
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Thanks. I understood on my skin that it is so. As a matter of fact my initial post was not claiming for a bug. I solved the issue as I said. So, in practice, it must not be an "advice", not to put any views on preview Panel. It is simply forbidden. After this, the ways to circumvent the problem are various. Thanks again
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…