B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=Class
Version=12.8
@EndOfDesignText@
Sub Class_Globals
	Private Root As B4XView 'ignore
	Private xui As XUI 'ignore
	Private camEx As CameraExClass
	Private frontCamera As Boolean = False
	Dim p_camara0 As Panel
	Dim p_camara As Panel
	Private p_cam As Panel
	Private b_foto_inci As Button
	Private b_cam As Button
	Dim fototomada As String = "0"
	Dim sDate As String
	Dim sTime 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("camara")
End Sub

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

Sub InitializeCamera
	Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_CAMERA)
	Wait For B4XPage_PermissionResult (Permission As String, resulta As Boolean)
	If resulta Then
		camEx.Initialize(p_cam, frontCamera, Me, "Camera1")
		frontCamera = camEx.Front
'		p_camara0.Initialize("p_camara0")
'		p_camara0.LoadLayout("camara")
	Else
		ToastMessageShow("!!!Sin permisos de camara!!!", True)
	End If
End Sub

Sub camaraVisible
	Log("CAMARA")
	p_camara.Top = 0
	p_camara.Left = 0
	p_camara.Visible = True
	p_camara.BringToFront
End Sub

Sub Camera1_Ready (Success As Boolean)
	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 Camera1_PictureTaken (Data() As Byte)
	If b_foto_inci.Text = "Foto tienda" Then
		DateTime.DateFormat="ddMMyyyyHHmmss"
		fototomada = DateTime.Now & "_cliente.jpg"
		Dim filename As String = fototomada
		Dim dir As String = File.DirInternal
		camEx.SavePictureToFile(Data, dir, filename)
		camEx.StartPreview 'restart preview
		ToastMessageShow("Picture saved." & CRLF  & "File size: " & File.Size(dir, filename), True)
		Dim Dirp As String = File.DirInternal
		Dim dir As String
		Dim Dir2 As String
		Try
			File.MakeDir(Dirp,"/adm")
			dir = "/adm"
			Log("creado en promotoria " & Dirp & dir)
		Catch
			dir = ""
			Log("creado en raiz")
		End Try
		Try
			File.MakeDir(Dirp & dir,"/reduccion")
			Dir2 = "/reduccion"
			Log("creado en promotoria " & Dirp & dir & Dir2)
		Catch
			dir = ""
			Log("creado en raiz")
		End Try
		DateTime.DateFormat = "MM/dd/yyyy"
		DateTime.TimeFormat = "HH:mm:ss"
		sDate = DateTime.Date(DateTime.Now)
		sTime = DateTime.Time(DateTime.Now)
		camEx.SavePictureToFile(Data, Dirp&dir, filename)
		camEx.StartPreview 'restart preview
		ToastMessageShow("Picture saved." & CRLF  & "File size: " & File.Size(dir, filename) & dir &"," & filename, True)
		Log("Picture saved." & CRLF  & "File size: " & File.Size(dir, filename) & dir &"," & filename)
		
		Dim img As B4XBitmap = xui.LoadBitmapResize(File.DirInternal & dir, filename, 450, 600, True)
		Dim out As OutputStream = File.OpenOutput(File.DirInternal & dir & Dir2, filename, False)
		img.WriteToStream(out, 100, "PNG")
		out.Close
	
		Starter.skmt.ExecNonQuery2("INSERT INTO FOTOINICIAL (RUTAA, ALMACEN, LATITUD, LONGITUD, FECHAHORA, FOTO, USUARIO) VALUES (?,?,?,?,?,?,?)",Array As Object(Subs.traeRuta, Subs.traeAlmacen, B4XPages.MainPage.lat_gps, B4XPages.MainPage.lon_gps, sDate & " " & sTime, File.ReadBytes(File.DirInternal & dir & Dir2, fototomada), Subs.traeUsuarioDeBD))
		b_foto_inci.Text = "Foto usuario"
		ChangeCamera_Click
	Else If b_foto_inci.Text = "Foto usuario" Then
		DateTime.DateFormat="ddMMyyyyHHmmss"
		fototomada = DateTime.Now & "_cliente.jpg"
		Dim filename As String = fototomada
		Dim dir As String = File.DirInternal
		camEx.SavePictureToFile(Data, dir, filename)
		camEx.StartPreview 'restart preview
		ToastMessageShow("Picture saved." & CRLF  & "File size: " & File.Size(dir, filename), True)
		Dim Dirp As String = File.DirInternal
		Dim dir As String
		Dim Dir2 As String
		Try
			File.MakeDir(Dirp,"/adm")
			dir = "/adm"
			Log("creado en promotoria " & Dirp & dir)
		Catch
			dir = ""
			Log("creado en raiz")
		End Try
		Try
			File.MakeDir(Dirp & dir,"/reduccion")
			Dir2 = "/reduccion"
			Log("creado en promotoria " & Dirp & dir & Dir2)
		Catch
			dir = ""
			Log("creado en raiz")
		End Try
		DateTime.DateFormat = "MM/dd/yyyy"
		DateTime.TimeFormat = "HH:mm:ss"
		sDate = DateTime.Date(DateTime.Now)
		sTime = DateTime.Time(DateTime.Now)
		camEx.SavePictureToFile(Data, Dirp&dir, filename)
		camEx.StartPreview 'restart preview
		ToastMessageShow("Picture saved." & CRLF  & "File size: " & File.Size(dir, filename) & dir &"," & filename, True)
		Log("Picture saved." & CRLF  & "File size: " & File.Size(dir, filename) & dir &"," & filename)
		Dim img As B4XBitmap = xui.LoadBitmapResize(File.DirInternal & dir, filename, 450, 600, True)
		Dim out As OutputStream = File.OpenOutput(File.DirInternal & dir & Dir2, filename, False)
		img.WriteToStream(out, 100, "PNG")
		out.Close
		B4XPages.MainPage.skmt.ExecNonQuery2("UPDATE FOTOINICIAL set FOTO2 = ? where USUARIO = ? and RUTAA = ? and ALMACEN = ? ", Array As Object (File.ReadBytes(File.DirInternal & dir & Dir2, fototomada), Subs.traeUsuarioDeBD,Subs.traeruta, Subs.traeAlmacen ))
		b_foto_inci.Text = "Foto tienda"
		ChangeCamera_Click
		p_camara.Visible = False
		StopCamera
		If Subs.traeCliente = "0" Then
			B4XPages.MainPage.tipo_venta = "ABORDO"
		Else If Subs.traeCliente = "1" Then
			B4XPages.MainPage.tipo_venta = "RECARGA"
		Else
			B4XPages.MainPage.tipo_venta = "PREVENTA"
		End If
'		B4XPages.ShowPage("productos")
'		p_transparentePidePeso.Visible = True
	End If
End Sub

Sub ChangeCamera_Click
	camEx.Release
	frontCamera = Not(frontCamera)
	InitializeCamera
End Sub

Private Sub StopCamera
'	Capturing = False
	If camEx.IsInitialized Then
		camEx.Release
	End If
End Sub

Private Sub b_foto_inci_Click
	
End Sub

Private Sub p_camara_Click
	
End Sub