Sub Class_Globals
Dim cam As CamEx2
Dim MyTaskIndex As Int
Dim pnlCamera As Panel
Dim Dir As String
Dim fn As String
Dim Confirm As Boolean
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(p As Panel,D As String,f As String, c As Boolean)
pnlCamera = p
Dir = D
fn = f
Confirm = c
End Sub
Sub TakePicture(front As Boolean) As ResumableSub
Dim rp As RuntimePermissions
pnlCamera.RemoveAllViews
cam.Initialize(pnlCamera)
Log("before rp.CheckAndRequest(rp.PERMISSION_CAMERA")
rp.CheckAndRequest(rp.PERMISSION_CAMERA)
Log("after rp.CheckAndRequest(rp.PERMISSION_CAMERA")
Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
If Result = False Then
ToastMessageShow("No permission!", True)
Return Null
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.
cam.PreviewSize.Initialize(1920, 1080) 'sizes can be modified here
Wait For (cam.PrepareSurface(MyTaskIndex)) Complete (Success As Boolean)
If Success = False Then
ToastMessageShow("Failed to open camera", True)
Log(LastException)
Return Null
End If
cam.StartPreview(MyTaskIndex,False)
Dim lbl As Label
lbl.Initialize("lblTakePic")
lbl.Typeface = Typeface.FONTAWESOME
lbl.TextSize = 100
lbl.TextColor = Colors.White
lbl.Text = Chr(0xF030)
pnlCamera.AddView(lbl,(pnlCamera.Width/2)-50dip,pnlCamera.Height-150dip,250dip,250dip)
Wait For lblTakePic_click
If front Then
Wait For(cam.TakePictureNow(MyTaskIndex)) Complete (Data() As Byte)
Else
Wait For(cam.FocusAndTakePicture(MyTaskIndex)) Complete (Data() As Byte)
End If
cam.stop
pnlCamera.RemoveAllViews
Dim bmp As Bitmap = cam.DataToBitmap(Data)
If front Then
bmp = bmp.Rotate(-90)
Else
bmp = bmp.Rotate(90)
End If
Dim save As Boolean = True
If Confirm Then
pnlCamera.SetBackgroundImage(bmp).Gravity = Gravity.FILL
Dim lbl As Label
lbl.Initialize("lblokRetry")
lbl.Typeface = Typeface.MATERIALICONS
lbl.TextSize = 50
lbl.TextColor = Colors.White
lbl.Gravity = Gravity.top
lbl.Text = Chr(0xE876)
pnlCamera.AddView(lbl,pnlCamera.Width - 150dip,pnlCamera.Height-75dip,50dip,50dip)
Dim lbl As Label
lbl.Initialize("lblokRetry")
lbl.Typeface = Typeface.MATERIALICONS
lbl.TextSize = 50
lbl.TextColor = Colors.White
lbl.Text = Chr(0xE5C9)
pnlCamera.AddView(lbl,50dip,pnlCamera.Height-75dip,50dip,50dip)
Wait For lblokRetry_click
Dim lbl As Label = Sender
If lbl.Text = "Retry" Then
Wait For (CallSub(Me,"TakePicture")) complete (r As Object)
Else
save = True
End If
End If
If save Then
Dim out As OutputStream = File.OpenOutput(Dir, fn, False)
bmp.WriteToStream(out, 100, "JPEG")
out.Close
End If
End If
Return Null
End Sub