Android Question help take picture on B4XPage using Camera2 Lib and CamEx2 v. 1.30

omarruben

Active Member
Licensed User
Longtime User
Hi I wanted to take a picture on this page (B4XPage) sendpicture(most copied from Erel Sample and try to simplify), I have a layout with a panel and button , but I don't get anything , any help is welcome

B4X:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
    Private btnTakePicture As Button
    Private cam As CamEx2
    Private panelCamera As Panel
    Private rp As RuntimePermissions
    Private MyTaskIndex As Int
    Private VideoFileDir As String
End Sub

'You can add more parameters here.
Public Sub Initialize As Object
    Return Me
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
    Root.LoadLayout("sendpicture")
    B4XPages.SetTitle(Me, "Take a Clear Picture")
    OpenCamera(True)
   
End Sub

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

Private Sub btnTakePicture_Click
    VideoFileDir = rp.GetSafeDirDefaultExternal("")
    cam.Initialize(panelCamera)
    Log(cam.SupportedHardwareLevel)

    TakePicture
End Sub

Sub OpenCamera (front As Boolean)
   
    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
   
    Wait For (cam.OpenCamera(front)) Complete (TaskIndex As Int)
    If TaskIndex > 0 Then
        MyTaskIndex = TaskIndex 'hold this index. It will be required in later calls.
        Wait For(PrepareSurface) Complete (Success As Boolean)
    End If
    Log("Start success: " & Success)
    If Success = False Then
        ToastMessageShow("Failed to open camera", True)
    End If
End Sub

Sub PrepareSurface As ResumableSub
    Log("prepare surface ok")
    cam.PreviewSize.Initialize(1920, 1080)
    Wait For (cam.PrepareSurface(MyTaskIndex)) Complete (Success As Boolean)

    If Success Then cam.StartPreview(MyTaskIndex, False) ' false mean picture mode no video
   
    Return Success
End Sub

Sub TakePicture
    Try
        Wait For(cam.FocusAndTakePicture(MyTaskIndex)) Complete (Data() As Byte)

        cam.DataToFile(Data, VideoFileDir, "1.jpg")
        Dim bmp As Bitmap = cam.DataToBitmap(Data)
        Log("Picture taken: " & bmp) 'ignore
        panelCamera.SetBackgroundImage(bmp.Resize(panelCamera.Width, panelCamera.Height, True)).Gravity = Gravity.CENTER
        Sleep(4000)
       
    Catch
        Log("error not picture taken")
    End Try
   
End Sub
 
Last edited:

mangojack

Well-Known Member
Licensed User
Longtime User
Try this ... the example is stripped to match your above code. (with slight changes)
The taken picture is displayed in an ImageView on your Panel.
 

Attachments

  • Barebones B4XPages Camera2 Ex.zip
    15.4 KB · Views: 224
Last edited:
Upvote 0

omarruben

Active Member
Licensed User
Longtime User
mangojack
thank you it works, the main replacement is :

B4X:
Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
for
B4X:
Wait For B4xPage_PermissionResult (Permission As String, Result As Boolean)

for whoever needs it, you guys are awesome !!!!
 
Last edited:
Upvote 0
Top