B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=Activity
Version=6.8
@EndOfDesignText@
#Region  Activity Attributes 
	#FullScreen: False
	#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.
    Dim ruta As String
    Dim g As GPS
	Private frontCamera As Boolean = False
	Dim quien_llamo As String
	Dim foto_g () As Byte
	Dim foto_g1 () As Byte
	Dim foto_g2 () As Byte
	Dim foto_g3 () As Byte
	Dim foto_g4 () As Byte
End Sub

Sub Globals
	Dim btnTakePicture As Button
	Dim Panel1 As Panel
'    Dim skmt As SQL
	Dim c As Cursor
	Dim cuenta As String
	Dim lat_gps As String
	Dim lon_gps As String
    Private camEx As CameraExClass
	Private i_foto As ImageView
	Private b_guardar As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
	Activity.LoadLayout("foto")
	Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_CAMERA)
	Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
	ruta = File.DirInternal
End Sub

Sub Camera1_Ready (Success As Boolean)
'	Log("Camara lista")
	If Success Then
		Dim ps As CameraSize
		ps.Width =640'480 '1280
		ps.Height =480'360 '960
		camEx.SetPictureSize(ps.Width, ps.Height)
		camEx.CommitParameters
		Try
			camEx.StartPreview
		Catch
			camEx.Release
			B4XPages.ShowPage("CheckList")
		End Try
	Else
		B4XPages.ShowPage("CheckList")
		'ToastMessageShow("Cannot open camera.", True)
	End If
End Sub

Sub Activity_Resume
	i_foto.Visible = False
	InitializeCamera
	btnTakePicture.Enabled = True
	b_guardar.Enabled = False
End Sub

Sub Activity_Pause (UserClosed As Boolean)
	camEx.Release
End Sub

Sub GPS_LocationChanged (Location1 As Location)
'	lat_gps=Location1.ConvertToSeconds(Location1.Latitude)
'	lon_gps=Location1.ConvertToSeconds(Location1.Longitude)
'	'btnTakePicture.Enabled = True
'	's.ExecNonQuery2("INSERT INTO  HIST_GPS (HGDATE,HGLAT, HGLON) VALUES(?,?,?) ", Array As Object (sDate & sTime, lat_gps, lon_gps))
End Sub

Private Sub InitializeCamera
	camEx.Initialize(Panel1, frontCamera, Me, "Camera1")
	frontCamera = camEx.Front
End Sub

Sub Camera1_PictureTaken (Data() As Byte)
'	Log("foto tomada")
	btnTakePicture.Enabled = True
	Dim filename As String = "2.jpg"
	Dim dir As String = File.DirInternal
	camEx.SavePictureToFile(Data, dir, filename)
	camEx.StartPreview 'restart preview
	i_foto.Visible = True
	i_foto.Bitmap = LoadBitmap(File.DirInternal, "2.jpg")
	If B4XPages.MainPage.cliente.TOMAR_FOTO = 1 Then
		Dim InputStream1 As InputStream
		InputStream1 = File.OpenInput(File.DirInternal, "2.jpg")
		Dim OutputStream1 As OutputStream
		OutputStream1.InitializeToBytesArray(500)
		File.Copy2(InputStream1, OutputStream1)
		foto_g = OutputStream1.ToBytesArray
	End If
	b_guardar.Enabled = True
	camEx.Release
	InitializeCamera
	btnTakePicture.Enabled = True
	'CallSubDelayed(seleccion,"seleccion")
'	StartActivity(fila)
End Sub

Sub Activity_KeyPress (key As Int) As Boolean
    ' BACK key pressed
    If key=KeyCodes.KEYCODE_BACK Then
		' I want to capture the key here so I return True
		Subs.iniciaActividad("Principal")
'		B4XPages.ShowPage("Principal")
		'Return True
    End If
    ' Returning False signals the system to handle the key
    Return False
End Sub

Sub b_guardar_Click
	camEx.Release
'	Log("liberamos camara")
	'If quien_llamo = "1" Then
	Try
		If quien_llamo = "1" Then
			File.Copy(File.DirInternal, "2.jpg",File.DirInternal, "FOTO1.jpg")
			Activity.Finish
			B4XPages.ShowPage("CheckList")
		else if quien_llamo = "2" Then
			File.Copy(File.DirInternal, "2.jpg",File.DirInternal, "FOTO2.jpg")
			Activity.Finish
			B4XPages.ShowPage("CheckList")
		else if quien_llamo = "3" Then
			File.Copy(File.DirInternal, "2.jpg",File.DirInternal, "FOTO3.jpg")
			Activity.Finish
			B4XPages.ShowPage("CheckList")
		else if quien_llamo = "4" Then
			File.Copy(File.DirInternal, "2.jpg",File.DirInternal, "FOTO4.jpg")
			Activity.Finish
			B4XPages.ShowPage("CheckList")
		End If
		If B4XPages.MainPage.cliente.TOMAR_FOTO = 1 Then
			c=Starter.skmt.ExecQuery("select cuenta from cuentaa")
			c.Position = 0
			cuenta = c.GetString("CUENTA")
			c.Close
			Starter.skmt.ExecNonQuery2("INSERT INTO  HIST_FOTOS (HF_FOTO, HF_CUENTA, HF_IDENCUESTA) VALUES(?,?,?) ", Array As Object (foto_g,cuenta,B4XPages.MainPage.cliente.id_encuesta ))
			Subs.iniciaActividad("Cliente")
		End If
	Catch
		Msgbox("ERROR EN FOTO","") 'ignore
		If B4XPages.MainPage.cliente.TOMAR_FOTO = 1 Then
			Subs.iniciaActividad("Cliente")
		Else
			B4XPages.ShowPage("CheckList")
		End If
	End Try
End Sub

Sub btnTakePicture_Click
	btnTakePicture.Enabled = False
	Dim ps As CameraSize
	ps.Width =640'480 '1280
	ps.Height =480'360 '960
	camEx.SetPictureSize(ps.Width, ps.Height)
	camEx.CommitParameters
	camEx.TakePicture
End Sub