private string handleVisitorResult(VisitorResultObject result) { string playContent = null; if (result.status == "Invalid") { playContent = "Invalid"; } else { playContent = "Welcome"; foreach (var visitor in result.visitorNames) { playContent += " " + visitor; } if (result.strangerNum == 0) { playContent += " to my home"; } else if (result.totalNum == result.strangerNum) { playContent += " " + result.strangerNum.ToString() + " friends to my home"; } else { playContent += " and other " + result.strangerNum.ToString() + " friends to my home"; } } return(playContent); }
/// <summary> /// This function is for send the capture picture to Azure backend. /// </summary> private async Task SendPhotoAsync() { nowTime = DateTime.Now; if (nowTime.Subtract(preTime).TotalSeconds < 10) { return; } if (_isSendingPhoto) { return; } _isSendingPhoto = true; preTime = DateTime.Now; InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream(); try { Debug.WriteLine("Taking photo..."); await _mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), stream); Debug.WriteLine("Photo taken!"); byte[] bytes = new byte[stream.Size]; stream.Seek(0); var buffer = await stream.ReadAsync(bytes.AsBuffer(), (uint)stream.Size, InputStreamOptions.None); bytes = buffer.ToArray(); Debug.WriteLine("bytes ok!"); //Upload the picture using HTTP request HttpResponseMessage response = await httpPostRequest(bytes); string playContent = string.Empty; if (response.IsSuccessStatusCode) { string resultContent = response.Content.ReadAsStringAsync().Result; Stream mStream = new MemoryStream(Encoding.UTF8.GetBytes(resultContent)); DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(VisitorResultObject)); VisitorResultObject result = (VisitorResultObject)ser.ReadObject(mStream); playContent = handleVisitorResult(result); } else { playContent = "Request Error"; } await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { this.infoshowText.Text = playContent; PlayTTS(playContent); }); } catch (Exception ex) { Debug.WriteLine("Exception when taking a photo: {0}", ex.ToString()); } _isSendingPhoto = false; }