diff --git a/src/Functions/SantaTalk.Functions/SantaTalk.Functions.sln b/src/Functions/SantaTalk.Functions/SantaTalk.Functions.sln
new file mode 100644
index 0000000..c5d6d67
--- /dev/null
+++ b/src/Functions/SantaTalk.Functions/SantaTalk.Functions.sln
@@ -0,0 +1,17 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio 15
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SantaTalk.Functions", "SantaTalk.Functions.csproj", "{F98A568D-8F95-48FE-B1DC-7F8EF9F3431F}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Any CPU = Debug|Any CPU
+ Release|Any CPU = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {F98A568D-8F95-48FE-B1DC-7F8EF9F3431F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {F98A568D-8F95-48FE-B1DC-7F8EF9F3431F}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {F98A568D-8F95-48FE-B1DC-7F8EF9F3431F}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {F98A568D-8F95-48FE-B1DC-7F8EF9F3431F}.Release|Any CPU.Build.0 = Release|Any CPU
+ EndGlobalSection
+EndGlobal
diff --git a/src/SantaTalk.Android/MainActivity.cs b/src/SantaTalk.Android/MainActivity.cs
index 15fa73e..5ac0eb2 100644
--- a/src/SantaTalk.Android/MainActivity.cs
+++ b/src/SantaTalk.Android/MainActivity.cs
@@ -6,6 +6,7 @@
using Android.Views;
using Android.Widget;
using Android.OS;
+using Plugin.CurrentActivity;
namespace SantaTalk.Droid
{
@@ -19,6 +20,8 @@ protected override void OnCreate(Bundle savedInstanceState)
base.OnCreate(savedInstanceState);
+ CrossCurrentActivity.Current.Init(this, savedInstanceState);
+
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
FFImageLoading.Forms.Platform.CachedImageRenderer.Init(true);
@@ -27,8 +30,10 @@ protected override void OnCreate(Bundle savedInstanceState)
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
+ Plugin.Permissions.PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
+
}
}
\ No newline at end of file
diff --git a/src/SantaTalk.Android/MainApplication.cs b/src/SantaTalk.Android/MainApplication.cs
new file mode 100644
index 0000000..9334ef5
--- /dev/null
+++ b/src/SantaTalk.Android/MainApplication.cs
@@ -0,0 +1,23 @@
+using System;
+using Android.App;
+using Android.Runtime;
+using Plugin.CurrentActivity;
+
+#if DEBUG
+[Application(Debuggable = true)]
+#else
+ [Application(Debuggable = false)]
+#endif
+public class MainApplication : Application
+{
+ public MainApplication(IntPtr handle, JniHandleOwnership transer)
+ : base(handle, transer)
+ {
+ }
+
+ public override void OnCreate()
+ {
+ base.OnCreate();
+ CrossCurrentActivity.Current.Init(this);
+ }
+}
\ No newline at end of file
diff --git a/src/SantaTalk.Android/Properties/AndroidManifest.xml b/src/SantaTalk.Android/Properties/AndroidManifest.xml
index ba3f8c2..657a8cb 100644
--- a/src/SantaTalk.Android/Properties/AndroidManifest.xml
+++ b/src/SantaTalk.Android/Properties/AndroidManifest.xml
@@ -1,6 +1,12 @@
-
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/SantaTalk.Android/Properties/AssemblyInfo.cs b/src/SantaTalk.Android/Properties/AssemblyInfo.cs
index 82620f6..57c29c0 100644
--- a/src/SantaTalk.Android/Properties/AssemblyInfo.cs
+++ b/src/SantaTalk.Android/Properties/AssemblyInfo.cs
@@ -32,3 +32,6 @@
// Add some common permissions, these can be removed if not needed
[assembly: UsesPermission(Android.Manifest.Permission.Internet)]
[assembly: UsesPermission(Android.Manifest.Permission.WriteExternalStorage)]
+
+[assembly: UsesFeature("android.hardware.camera", Required = true)]
+[assembly: UsesFeature("android.hardware.camera.autofocus", Required = true)]
diff --git a/src/SantaTalk.Android/Resources/Resource.designer.cs b/src/SantaTalk.Android/Resources/Resource.designer.cs
index 1d944e7..0d3d6e9 100644
--- a/src/SantaTalk.Android/Resources/Resource.designer.cs
+++ b/src/SantaTalk.Android/Resources/Resource.designer.cs
@@ -11424,10 +11424,13 @@ public partial class Xml
{
// aapt resource value: 0x7F100000
- public const int network_security_config = 2131755008;
+ public const int file_paths = 2131755008;
// aapt resource value: 0x7F100001
- public const int xamarin_essentials_fileprovider_file_paths = 2131755009;
+ public const int network_security_config = 2131755009;
+
+ // aapt resource value: 0x7F100002
+ public const int xamarin_essentials_fileprovider_file_paths = 2131755010;
static Xml()
{
diff --git a/src/SantaTalk.Android/Resources/xml/file_paths.xml b/src/SantaTalk.Android/Resources/xml/file_paths.xml
new file mode 100644
index 0000000..7d5b0f1
--- /dev/null
+++ b/src/SantaTalk.Android/Resources/xml/file_paths.xml
@@ -0,0 +1,5 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/src/SantaTalk.Android/SantaTalk.Android.csproj b/src/SantaTalk.Android/SantaTalk.Android.csproj
index ffcd81d..f9d20a4 100644
--- a/src/SantaTalk.Android/SantaTalk.Android.csproj
+++ b/src/SantaTalk.Android/SantaTalk.Android.csproj
@@ -69,11 +69,18 @@
12.0.3
+
+ 4.0.1.5
+
+
+ 2.1.0.4
+
+
@@ -125,6 +132,10 @@
+
+
+
+
diff --git a/src/SantaTalk.iOS/Info.plist b/src/SantaTalk.iOS/Info.plist
index bdf5fdd..940ff2a 100644
--- a/src/SantaTalk.iOS/Info.plist
+++ b/src/SantaTalk.iOS/Info.plist
@@ -39,6 +39,14 @@
NSAllowLocalNetworking
+ NSCameraUsageDescription
+This app needs access to the camera to take photos.
+NSPhotoLibraryUsageDescription
+This app needs access to photos.
+NSMicrophoneUsageDescription
+This app needs access to microphone.
+NSPhotoLibraryAddUsageDescription
+This app needs access to the photo gallery.
UIAppFonts
futura_book.ttf
diff --git a/src/SantaTalk.iOS/SantaTalk.iOS.csproj b/src/SantaTalk.iOS/SantaTalk.iOS.csproj
index 19ef860..3a7fede 100644
--- a/src/SantaTalk.iOS/SantaTalk.iOS.csproj
+++ b/src/SantaTalk.iOS/SantaTalk.iOS.csproj
@@ -139,6 +139,9 @@
12.0.3
+
+ 4.0.1.5
+
diff --git a/src/SantaTalk/MainPage.xaml b/src/SantaTalk/MainPage.xaml
index 7263026..0fc3449 100644
--- a/src/SantaTalk/MainPage.xaml
+++ b/src/SantaTalk/MainPage.xaml
@@ -1,49 +1,59 @@
-
-
+
+
-
+
-
+
-
-
-
+
+
+
-
+
-
-
+
+
+
+
-
+
-
-
\ No newline at end of file
diff --git a/src/SantaTalk/Models/Face.cs b/src/SantaTalk/Models/Face.cs
new file mode 100644
index 0000000..6c1fecf
--- /dev/null
+++ b/src/SantaTalk/Models/Face.cs
@@ -0,0 +1,28 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Newtonsoft.Json;
+
+namespace SantaTalk.Models
+{
+ public class Face
+ {
+ [JsonProperty("smile")]
+ public double Smile { get; set; }
+
+ [JsonProperty("gender")]
+ public string Gender { get; set; }
+
+ [JsonProperty("age")]
+ public double Age { get; set; }
+
+ [JsonProperty("emotion")]
+ public Dictionary Emotions;
+
+ public string CurrentEmotion()
+ {
+ var emotions = Emotions.OrderByDescending(x => x.Value);
+ return emotions.FirstOrDefault().Key;
+ }
+ }
+}
diff --git a/src/SantaTalk/ResultsPage.xaml b/src/SantaTalk/ResultsPage.xaml
index a31892f..2e5b1dc 100644
--- a/src/SantaTalk/ResultsPage.xaml
+++ b/src/SantaTalk/ResultsPage.xaml
@@ -60,6 +60,10 @@
+
+
+
+
@@ -25,5 +26,6 @@
+
\ No newline at end of file
diff --git a/src/SantaTalk/Services/FaceResponse.cs b/src/SantaTalk/Services/FaceResponse.cs
new file mode 100644
index 0000000..b2e459f
--- /dev/null
+++ b/src/SantaTalk/Services/FaceResponse.cs
@@ -0,0 +1,9 @@
+using SantaTalk.Models;
+
+namespace SantaTalk.Services
+{
+ public class FaceResponse
+ {
+ public Face FaceAttributes { get; set; }
+ }
+}
diff --git a/src/SantaTalk/Services/PhotoDeliveryService.cs b/src/SantaTalk/Services/PhotoDeliveryService.cs
new file mode 100644
index 0000000..77c1463
--- /dev/null
+++ b/src/SantaTalk/Services/PhotoDeliveryService.cs
@@ -0,0 +1,56 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Net.Http;
+using System.Net.Http.Headers;
+using System.Threading.Tasks;
+using Newtonsoft.Json;
+
+namespace SantaTalk.Services
+{
+ public class PhotoDeliveryService
+ {
+ const string subscriptionKey = "4c8b5bdac07d45e58a64734a1a10eff6";
+ const string uriBase = "https://brazilsouth.api.cognitive.microsoft.com/face/v1.0/detect";
+
+ public async Task> MakeAnalysisRequest(string imageFilePath)
+ {
+ var httpClient = new HttpClient();
+
+ httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
+
+ string requestParameters = "returnFaceId=true&returnFaceLandmarks=false" +
+ "&returnFaceAttributes=age,gender,headPose,smile,facialHair,glasses," +
+ "emotion,hair,makeup,occlusion,accessories,blur,exposure,noise";
+
+ string uri = uriBase + "?" + requestParameters;
+
+ byte[] byteData = GetImageAsByteArray(imageFilePath);
+
+ List result;
+
+ using (ByteArrayContent content = new ByteArrayContent(byteData))
+ {
+ content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
+
+ var response = await httpClient.PostAsync(uri, content);
+
+ string contentString = await response.Content.ReadAsStringAsync();
+
+ result = JsonConvert.DeserializeObject>(contentString);
+ }
+
+ return result;
+ }
+
+ private byte[] GetImageAsByteArray(string imageFilePath)
+ {
+ using (FileStream fileStream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read))
+ {
+ BinaryReader binaryReader = new BinaryReader(fileStream);
+ return binaryReader.ReadBytes((int)fileStream.Length);
+ }
+ }
+ }
+}
+
diff --git a/src/SantaTalk/ViewModels/MainPageViewModel.cs b/src/SantaTalk/ViewModels/MainPageViewModel.cs
index 2663946..e8cb65c 100644
--- a/src/SantaTalk/ViewModels/MainPageViewModel.cs
+++ b/src/SantaTalk/ViewModels/MainPageViewModel.cs
@@ -1,18 +1,20 @@
using System;
using System.Windows.Input;
using MvvmHelpers;
+using Plugin.Media;
using Xamarin.Forms;
namespace SantaTalk
{
public class MainPageViewModel : BaseViewModel
{
- public MainPageViewModel()
+ public string FilePath { get; set; }
+
+ ImageSource photo;
+ public ImageSource Photo
{
- SendLetterCommand = new Command(async () =>
- {
- await Application.Current.MainPage.Navigation.PushAsync(new ResultsPage(KidsName, LetterText));
- });
+ get => photo;
+ set => SetProperty(ref photo, value);
}
string kidsName;
@@ -29,6 +31,54 @@ public string LetterText
set => SetProperty(ref letterText, value);
}
+ bool readyToSend;
+ public bool ReadyToSend
+ {
+ get => readyToSend;
+ set => SetProperty(ref readyToSend, value);
+ }
+
public ICommand SendLetterCommand { get; }
+
+ public ICommand TakePictureCommand { get; }
+
+ public ICommand SendPhotoCommand { get; set; }
+
+ public MainPageViewModel()
+ {
+ SendLetterCommand = new Command(async () =>
+ {
+ await Application.Current.MainPage.Navigation.PushAsync(new ResultsPage(KidsName, LetterText, FilePath));
+ });
+
+ TakePictureCommand = new Command(async () =>
+ {
+ await CrossMedia.Current.Initialize();
+
+ if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
+ {
+ Console.Write(@"No Camera :( No camera available");
+ return;
+ }
+
+ var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
+ {
+ Directory = "Sample",
+ Name = "test.jpg"
+ });
+
+ if (file == null)
+ return;
+
+ FilePath = file.Path;
+
+ Photo = ImageSource.FromStream(() =>
+ {
+ var stream = file.GetStream();
+ file.Dispose();
+ return stream;
+ });
+ });
+ }
}
}
diff --git a/src/SantaTalk/ViewModels/ResultsPageViewModel.cs b/src/SantaTalk/ViewModels/ResultsPageViewModel.cs
index 696a611..0498f65 100644
--- a/src/SantaTalk/ViewModels/ResultsPageViewModel.cs
+++ b/src/SantaTalk/ViewModels/ResultsPageViewModel.cs
@@ -1,13 +1,36 @@
using System;
+using System.Linq;
using System.Threading.Tasks;
using MvvmHelpers;
using SantaTalk.Models;
+using SantaTalk.Services;
using Xamarin.Forms.StateSquid;
namespace SantaTalk
{
public class ResultsPageViewModel : BaseViewModel
{
+ string filePath;
+ public string FilePath
+ {
+ get => filePath;
+ set => SetProperty(ref filePath, value);
+ }
+
+ bool theresFoto;
+ public bool TheresFoto
+ {
+ get => theresFoto;
+ set => SetProperty(ref theresFoto, value);
+ }
+
+ string emotionPhoto;
+ public string EmotionPhoto
+ {
+ get => emotionPhoto;
+ set => SetProperty(ref emotionPhoto, value);
+ }
+
string kidsName;
public string KidsName
{
@@ -50,10 +73,20 @@ public string GiftDecision
set => SetProperty(ref giftDecision, value);
}
+
public async Task SendLetterToSanta()
{
CurrentState = State.Loading;
+ if (!string.IsNullOrEmpty(FilePath))
+ {
+ TheresFoto = true;
+ var emotion = await new PhotoDeliveryService().MakeAnalysisRequest(FilePath);
+ var currentEmotion = emotion.FirstOrDefault().FaceAttributes.CurrentEmotion();
+ EmotionPhoto = currentEmotion;
+ CurrentState = State.Success;
+ }
+
var letter = new SantaLetter
{
KidName = KidsName,
@@ -65,8 +98,11 @@ public async Task SendLetterToSanta()
if (results.SentimentScore == -1)
{
- CurrentState = State.Error;
- return;
+ if (string.IsNullOrEmpty(FilePath))
+ {
+ CurrentState = State.Error;
+ return;
+ }
}
var commentsService = new SantasCommentsService();