Android Question Can NOT initialize camera.

Baris Karadeniz

Active Member
Licensed User
I want to take a picture when the PanelCamera is visible. But I can not initialize camera. When I press on the BtnCamera, program crashes. How can I sole this problem? My codes are given below;

B4X:
Sub BtnCamera_Click
    PanelMain.Visible = False
    PanelCamera.Visible = True
    btnTakePicture.Enabled = False
    camera.Initialize(PanelCamera, "Camera")
End Sub

Sub Camera_Ready (Success As Boolean)
    If Success Then
        camera.StartPreview
        btnTakePicture.Enabled = True
    Else
        ToastMessageShow("Cannot open camera.", True)
    End If
End Sub

Sub Camera_PictureTaken (Data() As Byte)
    camera.StartPreview
    Dim out As OutputStream
    out = File.OpenOutput(File.DirInternal, "picture.jpg", False)
    out.WriteBytes(Data, 0, Data.Length)
    out.Close
    ToastMessageShow("Image saved: " & File.Combine(File.DirInternal, "picture.jpg"), True)
    PhotoUpload
    btnTakePicture.Enabled = True
End Sub

Sub btnTakePicture_Click
    btnTakePicture.Enabled = False
    camera.TakePicture
End Sub
 

Baris Karadeniz

Active Member
Licensed User
Thanks. CameraEx is more complicated. The library below is enough for me. When the app is debug mode and I press on BtnCamera to go to a view of camera, program corrupts suddenly and out from debug. There is not any log. I think it is because of "camera.Initialize(PanelCamera, "Camera")" line. Because if I delete this line, view opens but of course there is no camera. So I think that "camera.Initialize(PanelCamera, "Camera")" line should be at another line but where?
 
Upvote 0

strat

Active Member
Licensed User
Longtime User
Use this. It uses CameraEx classs.


B4X:
#Region Module Attributes
    #FullScreen: True
    #IncludeTitle: False
    #ApplicationLabel: Camex
    #VersionCode: 1
    #VersionName: 1.0
    #SupportedOrientations:portrait
#End Region

Sub Process_Globals
    Dim timer1 As Timer
End Sub

Sub Globals
    Private Panel1 As Panel
    Private camEx As CameraExClass
    Private Change_Camera As Button
    Private btnTakePicture As Button
    Private api As Int
    Private FocusSupported As Boolean
    Private PnlFocus As Panel
    Dim cvsDrawing As Canvas
    Private frontCamera As Boolean = False
    Private zaman As Int
    Private r As Reflector
End Sub


Private Sub InitializeCamera
    camEx.Initialize(Panel1, frontCamera, Me, "Camera1", PnlFocus, cvsDrawing)
    frontCamera = camEx.Front
    zaman=0
    timer1.Enabled=True
End Sub

Sub timer1_Tick
    zaman=zaman+1
    If zaman>10 Then
        timer1.Enabled=False
        resimboyutuayarla
    End If
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Panel1.Initialize("Panel1")
    Activity.AddView(Panel1,0,0,100%x,90%y)
   
    PnlFocus.Initialize("PnlFocus")
    Activity.AddView(PnlFocus,0,0,10%x,10%y)
   
    Change_Camera.Initialize("Change_Camera")
    Activity.AddView(Change_Camera,10%x,90%y,30%x,8%y)
    Change_Camera.Text="Change Camera"
   
    btnTakePicture.Initialize("btnTakePicture")
    Activity.AddView(btnTakePicture,60%x,90%y,30%x,8%y)
    btnTakePicture.Text="Take Picture"
   
    api = r.GetStaticField("android.os.Build$VERSION", "SDK_INT")
    timer1.Initialize("timer1",100)
    timer1.Enabled=False
    InitializeCamera
   
End Sub

Sub Camera1_Ready (Success As Boolean)
    Dim previewSizes() As CameraSize = camEx.GetSupportedPreviewSizes
    Dim current As CameraSize = camEx.GetPreviewSize
    For i = 0 To previewSizes.Length - 1
        If previewSizes(i).Width = current.Width And previewSizes(i).Height = current.Height Then Exit
    Next
    Dim ps As CameraSize = previewSizes((i + 1) Mod previewSizes.Length)
    Log(ps.Width&"   "&ps.Height)
    camEx.SetpreviewSize(ps.Width, ps.Height)
    camEx.CommitParameters       
    If Success Then
        camEx.JpegQuality=90
        camEx.CommitParameters
        camEx.StartPreview
        FocusSupported = False
        If api >= 14 Then
            If camEx.GetMaxNumFocusAreas <> 0 Then FocusSupported = True
        End If
    Else
        ToastMessageShow("Cannot open camera.", True)
    End If
End Sub

Sub Camera1_PictureTaken (Data() As Byte)
    Dim filename As String = "Temp1.jpg"
    Dim dir As String = File.DirRootExternal
    camEx.SavePictureToFile(Data, dir, filename)
    camEx.StartPreview 'restart preview
End Sub

Sub btnTakePicture_Click
    camEx.TakePicture
End Sub

Sub resimboyutuayarla
    Dim pictureSizes() As CameraSize = camEx.GetSupportedPicturesSizes
    Dim sayi1,sayi2,adim As Int
    Dim ilk As Boolean
    Dim no,ilkdeger,sondeger As Int
    ilk=True
    ilkdeger=pictureSizes(0).Width
    sondeger=pictureSizes(pictureSizes.Length - 1).Width
    If ilkdeger<sondeger Then
        sayi2=0
        sayi1=pictureSizes.Length - 1
        adim=-1
    Else
        sayi1=0
        sayi2=pictureSizes.Length - 1
        adim=1
    End If
    For i = sayi1 To sayi2 Step adim
        If ilk=True Then
            If (pictureSizes(i).Width<=1200) And (pictureSizes(i).Height<=1500) Then
                no=i
                ilk=False
            End If
        End If
    Next
    Dim ps As CameraSize = pictureSizes(no)
    camEx.SetPictureSize(ps.Width, ps.Height)
    camEx.CommitParameters       
End Sub

Sub Panel1_Touch (Action As Int, X As Float, Y As Float) As Boolean
    If FocusSupported Then
        Dim rect1 As Rect
        Dim myleft As Int
        Dim mytop As Int
        myleft = Min(Max((X/Panel1.width)* 2000 -1000 -100, -1000),800)
        mytop = Min(Max((Y/Panel1.Height)*2000 - 1000 -100, -1000),800)
        If Action = Activity.ACTION_UP Then
            rect1.Initialize(myleft,mytop,myleft + 200 , mytop + 200)
            camEx.SetFocusArea(rect1, 1000 )
            camEx.setFocusMode("auto")
            camEx.commitparameters
            camEx.focus
        End If
    End If
    Return True
End Sub

Sub Change_Camera_Click
    camEx.Release
    frontCamera = Not(frontCamera)
    InitializeCamera
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…