B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=Class
Version=12.5
@EndOfDesignText@
Sub Class_Globals
	Private Root As B4XView 'ignore
	Private xui As XUI 'ignore
	Private p_check As Panel
	Private sv_datos As ScrollView
	
	Private camEx As CameraExClass
	Dim frontCamera As Boolean = False
	Private p_cam As Panel
	Dim  nombrefoto As String = "0"
	Dim  nombrefoto1 As String = "0"
	Dim  nombrefoto2 As String = "0"
	Dim  nombrefoto3 As String = "0"
	Private p_camara As Panel
	Private teclado As IME
	Dim fototomada As String
	Private ImageView1 As ImageView
	Private ImageView3 As ImageView
	Private ImageView2 As ImageView
	Private ImageView4 As ImageView
	Private b_foto As Button
	Dim device As Phone
	Dim MES1 As ManageExternalStorage
	Dim x As Int = 0
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
	Root.LoadLayout("checklist")
	
	sv_datos.Height = Root.Height
	sv_datos.Width = Root.Width
	sv_datos.Panel.LoadLayout("check")
	sv_datos.Panel.Height = p_check.Height
	p_check.Width = Root.Width
	p_camara.Width = Root.Width
	p_camara.Height = Root.Height
End Sub

Private Sub B4XPage_Appear
	MES1.Initialize(Me, "MES1")
	
	' get the device SDK version
	Dim SdkVersion As Int = device.SdkVersion
	
	' Choose which permission to request in order to access external storgage
	If SdkVersion < 30 Then
		Log("SDK = " & SdkVersion & " : Requesting WRITE_EXTERNAL_STORAGE permission")
		Dim rp As RuntimePermissions
		rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE) ' Implicit read capability if granted
		Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
		Log($"PERMISSION_WRITE_EXTERNAL_STORAGE = ${Result}"$)
	Else
		Log("SDK = " & SdkVersion & " : Requesting MANAGE_EXTERNAL_STORAGE permission")
		Log("On Entry MANAGE_EXTERNAL_STORAGE = " & MES1.HasPermission)
		If Not(MES1.HasPermission) Then
			MsgboxAsync("This app requires access to all files, please enable the option", "Manage All Files")
			Wait For Msgbox_Result(Res As Int)
			Log("Getting permission")
			MES1.GetPermission
			Wait For MES_StorageAvailable
		End If
	End If
	
	p_camara.Visible = False
End Sub

Private Sub InitializeCamera2
	Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_CAMERA)
	Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
	If Result Then
		camEx.Initialize(p_cam, frontCamera, Me, "Camera1")
		frontCamera = camEx.Front
		Log("inicializamos Camara")
	Else
		ToastMessageShow("No permission!!!", True)
	End If
	Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
	Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
	If Result Then
		Log("conpermisos para escritura")
	Else
		ToastMessageShow("No permission!!!", True)
	End If
	
	Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_READ_EXTERNAL_STORAGE)
	Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
	If Result Then
		Log("conpermisos para escritura")
	Else
		ToastMessageShow("No permission!!!", True)
	End If
End Sub

Sub Camera1_Ready (Success As Boolean)
	Log("Camara ready")
	If Success Then
		camEx.SetJpegQuality(90)
		camEx.SetContinuousAutoFocus
		camEx.CommitParameters
		camEx.StartPreview
		Log(camEx.GetPreviewSize)
	Else
		ToastMessageShow("Cannot open camera.", True)
		Log("Cannot open camera")
	End If
End Sub

Sub Camera1_PictureTaken (Data()As Byte)
	If x = 1 Then
		DateTime.DateFormat="ddMMyyyyHHmmss"
		nombrefoto = DateTime.Now & "_FOTO1.jpg"
		teclado.HideKeyboard
		fototomada = nombrefoto
		
	Else If x = 2 Then
		DateTime.DateFormat="ddMMyyyyHHmmss"
		nombrefoto1 = DateTime.Now & "_FOTO2.jpg"
		teclado.HideKeyboard
		fototomada = nombrefoto1
		
	Else If x = 3 Then
		DateTime.DateFormat="ddMMyyyyHHmmss"
		nombrefoto2 = DateTime.Now & "_FOTO3.jpg"
		teclado.HideKeyboard
		fototomada = nombrefoto2
		
	Else If x = 4 Then
		DateTime.DateFormat="ddMMyyyyHHmmss"
		nombrefoto3 = DateTime.Now & "_FOTO4.jpg"
		teclado.HideKeyboard
		fototomada = nombrefoto3
		
	End If
	Log("tome foto")
	Dim filename As String = fototomada
	Dim Dirp As String = File.DirRootExternal
	Dim Dir As String
	Dim Dir2 As String
	Try
		File.MakeDir(Dirp,"/md")
		Dir = "/md"
		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
	
	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)
	p_camara.Visible = False
	
	Log(nombrefoto)
	Log(nombrefoto1)
	Log(nombrefoto2)
	Log(nombrefoto3)
	
	If nombrefoto <> "0" Then
		Dim img As B4XBitmap = xui.LoadBitmapResize(File.DirRootExternal & Dir, filename, 300, 450, True)
		Dim out As OutputStream = File.OpenOutput(File.DirRootExternal & Dir & Dir2, filename, True)
		img.WriteToStream(out, 100, "PNG")
		out.Close
		ImageView1.Bitmap = LoadBitmap(File.DirRootExternal & Dir & Dir2,nombrefoto)
	End If
	
	If nombrefoto1 <> "0" Then
		Dim img As B4XBitmap = xui.LoadBitmapResize(File.DirRootExternal & Dir, filename, 300, 450, True)
		Dim out As OutputStream = File.OpenOutput(File.DirRootExternal & Dir & Dir2, filename, False)
		img.WriteToStream(out, 100, "PNG")
		out.Close
		ImageView2.Bitmap = LoadBitmap(File.DirRootExternal & Dir& Dir2,nombrefoto1)
	End If
	
	If nombrefoto2 <> "0" Then
		Dim img As B4XBitmap = xui.LoadBitmapResize(File.DirRootExternal & Dir, filename, 300, 450, True)
		Dim out As OutputStream = File.OpenOutput(File.DirRootExternal & Dir & Dir2, filename, False)
		img.WriteToStream(out, 100, "PNG")
		out.Close
		ImageView3.Bitmap = LoadBitmap(File.DirRootExternal & Dir& Dir2,nombrefoto2)
	End If
	
	If nombrefoto3 <> "0" Then
		Dim img As B4XBitmap = xui.LoadBitmapResize(File.DirRootExternal & Dir, filename, 300, 450, True)
		Dim out As OutputStream = File.OpenOutput(File.DirRootExternal & Dir & Dir2, filename, False)
		img.WriteToStream(out, 100, "PNG")
		out.Close
		ImageView4.Bitmap = LoadBitmap(File.DirRootExternal & Dir & Dir2,nombrefoto3)
	End If
	
	StopCamera2
	
End Sub

Private Sub p_camara_Click
	
End Sub

Private Sub b_foto_Click
	camEx.TakePicture
	p_camara.Visible = False
'	StopCamera2
End Sub

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

Private Sub ImageView1_Click

	x = 1
	InitializeCamera2
	p_camara.Visible = True

End Sub

Private Sub ImageView2_Click

	x = 2
	InitializeCamera2
	p_camara.Visible = True

End Sub

Private Sub ImageView3_Click

	x = 3
	InitializeCamera2
	p_camara.Visible = True

End Sub

Private Sub ImageView4_Click

	x = 4
	InitializeCamera2
	p_camara.Visible = True

End Sub

Private Sub b_guardar_Click
	B4XPages.ShowPage("Principal")
End Sub