public async Task IniciarAnalise(decimal tempo, decimal numCapturas, WebCam webCam, Rectangle retangulo) { Repetições = Convert.ToInt32(tempo * numCapturas); Intervalo = TimeSpan.FromMilliseconds(1000 / Convert.ToInt32(numCapturas)); for (int i = 0; i < Repetições; i++) { Capturas.Add(webCam.Matriz.ToImage <Bgr, byte>().GetSubRect(retangulo).GetAverage()); CalcularSinal(i); await Task.Delay(Intervalo); } }
private async Task Enviar() { VisualFeature[] visualFeatures = { VisualFeature.Tags, VisualFeature.Categories, VisualFeature.Description }; AnalysisResult ret; imageStream.Seek(0, SeekOrigin.Begin); try { Device.BeginInvokeOnMainThread(() => UserDialogs.Instance.ShowLoading("analizando imagen")); ret = await vc.AnalyzeImageAsync(imageStream, visualFeatures.ToList(), null); } catch (ClientException c) { Debug.WriteLine($"error al consultar servicio: {c.Error} - {c.Message} - {c}"); return; } catch (Exception ex) { Debug.WriteLine($"error al consultar servicio: {ex}"); return; } finally { Device.BeginInvokeOnMainThread(() => UserDialogs.Instance.HideLoading()); } if (ret.Description.Captions != null && ret.Description.Captions.Length > 0) { Resultado.Text = ret.Description.Captions[0].Text; try { Device.BeginInvokeOnMainThread(() => UserDialogs.Instance.ShowLoading("traduciendo")); var secret = Constants.TRANSLATOR_SECRET; var clientid = Constants.TRANSLATOR_CLIENTID; var turl = $"http://api.microsofttranslator.com/v2/Http.svc/Translate?text={Uri.EscapeDataString(ret.Description.Captions[0].Text)}&from=en&to=es"; var nurl = new Uri("https://datamarket.accesscontrol.windows.net/v2/OAuth2-13"); var reqDetails = new Dictionary <string, string>() { { "grant_type", "client_credentials" }, { "client_id", clientid }, { "client_secret", secret }, { "scope", "http://api.microsofttranslator.com" } }; var content = new FormUrlEncodedContent(reqDetails); var cl = new HttpClient(); string str; try { var post = await cl.PostAsync(nurl, content); str = await post.Content.ReadAsStringAsync(); } catch (Exception e) { Debug.WriteLine($"excepcion: {e}"); return; } var btoken = JToken.Parse(str).Value <string>("access_token"); var req = new HttpRequestMessage(HttpMethod.Get, turl); req.Headers.Add("Authorization", "Bearer " + btoken); Stream stream; try { var salida = await cl.SendAsync(req); //cl.GetStringAsync(turl))); stream = await salida.Content.ReadAsStreamAsync(); } catch (Exception e) { Debug.WriteLine($"excepcion: {e}"); return; } Debug.WriteLine($"traducido: {str}"); var dcs = new DataContractSerializer(Type.GetType("System.String")); str = (string)dcs.ReadObject(stream); Resultado.Text = str; //ret.Description.Captions[0].Text; CrossTextToSpeech.Current.Speak(str); //ret.Description.Captions[0].Text); } catch (Exception ex) { Debug.WriteLine($"excepcion al traducir: {ex}"); Resultado.Text = ret.Description.Captions[0].Text; } finally { Device.BeginInvokeOnMainThread(() => UserDialogs.Instance.HideLoading()); } } ListaTags.ItemsSource = ret.Description.Tags; // subamos al mobile service var url = Constants.BLOB_CONTAINER; var key = Constants.BLOB_KEY; CloudBlockBlob blob; try { Device.BeginInvokeOnMainThread(() => UserDialogs.Instance.ShowLoading("generando blob")); var bc = new CloudBlobContainer(new Uri(url), new StorageCredentials(Constants.BLOB_ACCOUNT_NAME, key)); blob = bc.GetBlockBlobReference(Guid.NewGuid() + ".jpg"); imageStream.Seek(0, SeekOrigin.Begin); await blob.UploadFromStreamAsync(imageStream); } catch (Exception ex) { Device.BeginInvokeOnMainThread(() => UserDialogs.Instance.ShowError($"excepcion al subir blob: {ex}")); return; } finally { Device.BeginInvokeOnMainThread(() => UserDialogs.Instance.HideLoading()); } Capturas cap; try { Device.BeginInvokeOnMainThread(() => UserDialogs.Instance.ShowLoading("Cargando en App Services")); var ms = new MobileServiceClient(Constants.APP_SERVICE_URI); var tabla = ms.GetTable <Capturas>(); cap = new Capturas(); cap.Descripcion = ret.Description.Captions[0].Text; cap.Url = blob.Uri.ToString(); await tabla.InsertAsync(cap); } catch (Exception ex) { Debug.WriteLine($"excepcion: {ex}"); Device.BeginInvokeOnMainThread(() => UserDialogs.Instance.ShowError($"excepcion al grabar en bd: {ex}")); return; } finally { Device.BeginInvokeOnMainThread(() => UserDialogs.Instance.HideLoading()); } Device.BeginInvokeOnMainThread(() => UserDialogs.Instance.Alert($"Éxito! Registro creado en backend, con ID {cap.Id}")); }